結果

問題 No.877 Range ReLU Query
ユーザー koba-e964koba-e964
提出日時 2021-09-17 02:04:56
言語 Rust
(1.77.0)
結果
AC  
実行時間 279 ms / 2,000 ms
コード長 3,241 bytes
コンパイル時間 922 ms
コンパイル使用メモリ 169,356 KB
実行使用メモリ 12,872 KB
最終ジャッジ日時 2024-04-25 22:31:05
合計ジャッジ時間 5,169 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 273 ms
11,452 KB
testcase_12 AC 236 ms
10,664 KB
testcase_13 AC 187 ms
9,020 KB
testcase_14 AC 203 ms
8,852 KB
testcase_15 AC 279 ms
12,536 KB
testcase_16 AC 272 ms
12,264 KB
testcase_17 AC 277 ms
12,416 KB
testcase_18 AC 277 ms
12,528 KB
testcase_19 AC 244 ms
12,868 KB
testcase_20 AC 267 ms
12,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::Read;

fn get_word() -> String {
    let stdin = std::io::stdin();
    let mut stdin=stdin.lock();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.unwrap_or(0) == 0 || u8b[0] <= b' ' {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() }

/**
 * Segment Tree. This data structure is useful for fast folding on intervals of an array
 * whose elements are elements of monoid I. Note that constructing this tree requires the identity
 * element of I and the operation of I.
 * Verified by: yukicoder No. 259 (http://yukicoder.me/submissions/100581)
 *              AGC015-E (http://agc015.contest.atcoder.jp/submissions/1461001)
 */
struct SegTree<I, BiOp> {
    n: usize,
    dat: Vec<I>,
    op: BiOp,
    e: I,
}

impl<I, BiOp> SegTree<I, BiOp>
    where BiOp: Fn(I, I) -> I,
          I: Copy {
    pub fn new(n_: usize, op: BiOp, e: I) -> Self {
        let mut n = 1;
        while n < n_ { n *= 2; } // n is a power of 2
        SegTree {n: n, dat: vec![e; 2 * n - 1], op: op, e: e}
    }
    /* ary[k] <- v */
    pub fn update(&mut self, idx: usize, v: I) {
        let mut k = idx + self.n - 1;
        self.dat[k] = v;
        while k > 0 {
            k = (k - 1) / 2;
            self.dat[k] = (self.op)(self.dat[2 * k + 1], self.dat[2 * k + 2]);
        }
    }
    /* [a, b) (note: half-inclusive)
     * http://proc-cpuinfo.fixstars.com/2017/07/optimize-segment-tree/ */
    pub fn query(&self, mut a: usize, mut b: usize) -> I {
        let mut left = self.e;
        let mut right = self.e;
        a += self.n - 1;
        b += self.n - 1;
        while a < b {
            if (a & 1) == 0 {
                left = (self.op)(left, self.dat[a]);
            }
            if (b & 1) == 0 {
                right = (self.op)(self.dat[b - 1], right);
            }
            a = a / 2;
            b = (b - 1) / 2;
        }
        (self.op)(left, right)
    }
}

fn main() {
    let n: usize = get();
    let q: usize = get();
    let a: Vec<i64> = (0..n).map(|_| get()).collect();
    let mut sts = SegTree::new(n, |x, y| x + y, 0i64);
    let mut stc = SegTree::new(n, |x, y| x + y, 0i64);
    let mut ev = vec![];
    let mut ans = vec![0; q];
    for i in 0..q {
        let ty: i32 = get();
        assert_eq!(ty, 1);
        let l = get::<usize>() - 1;
        let r: usize = get();
        let x: i64 = get();
        ev.push((x, l, r, 1 + i));
    }
    for i in 0..n {
        ev.push((a[i], i, i + 1, 0));
    }
    ev.sort(); ev.reverse();
    for (x, l, r, ty) in ev {
        if ty == 0 {
            sts.update(l, a[l]);
            stc.update(l, 1);
        } else {
            ans[ty - 1] = sts.query(l, r) - x * stc.query(l, r);
        }
    }
    for i in 0..q {
        println!("{}", ans[i]);
    }
}
0