結果

問題 No.1441 MErGe
ユーザー akakimidoriakakimidori
提出日時 2021-03-26 22:54:53
言語 Rust
(1.77.0)
結果
AC  
実行時間 144 ms / 1,000 ms
コード長 3,717 bytes
コンパイル時間 1,457 ms
コンパイル使用メモリ 160,620 KB
実行使用メモリ 19,688 KB
最終ジャッジ日時 2023-08-19 09:36:21
合計ジャッジ時間 6,427 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 26 ms
6,364 KB
testcase_09 AC 26 ms
6,392 KB
testcase_10 AC 37 ms
7,276 KB
testcase_11 AC 33 ms
7,032 KB
testcase_12 AC 39 ms
7,444 KB
testcase_13 AC 103 ms
18,472 KB
testcase_14 AC 106 ms
18,560 KB
testcase_15 AC 110 ms
18,644 KB
testcase_16 AC 99 ms
14,268 KB
testcase_17 AC 98 ms
14,136 KB
testcase_18 AC 91 ms
13,008 KB
testcase_19 AC 103 ms
13,904 KB
testcase_20 AC 86 ms
12,440 KB
testcase_21 AC 67 ms
11,744 KB
testcase_22 AC 89 ms
13,196 KB
testcase_23 AC 141 ms
19,656 KB
testcase_24 AC 138 ms
19,672 KB
testcase_25 AC 139 ms
19,628 KB
testcase_26 AC 144 ms
19,688 KB
testcase_27 AC 138 ms
19,632 KB
testcase_28 AC 67 ms
19,676 KB
testcase_29 AC 66 ms
19,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// ---------- begin fenwick tree ----------
pub struct Fenwick<T> {
    zero: T,
    a: Box<[T]>,
}

impl<T> Fenwick<T>
where
    T: Copy + std::ops::Add<Output = T>,
{
    pub fn new(size: usize, zero: T) -> Fenwick<T> {
        Fenwick {
            zero: zero,
            a: vec![zero; size + 1].into_boxed_slice(),
        }
    }
    pub fn init(&mut self) {
        for a in self.a.iter_mut() {
            *a = self.zero;
        }
    }
    pub fn add(&mut self, mut x: usize, v: T) {
        assert!(x > 0);
        while let Some(a) = self.a.get_mut(x) {
            *a = *a + v;
            x += x & (!x + 1);
        }
    }
    pub fn sum(&self, mut x: usize) -> T {
        assert!(x < self.a.len());
        let mut res = self.zero;
        while x > 0 {
            res = res + self.a[x];
            x -= x & (!x + 1);
        }
        res
    }
}

impl<T> Fenwick<T>
where
    T: Copy + std::ops::Add<Output = T> + PartialOrd,
{
    pub fn search(&self, s: T) -> usize {
        debug_assert!(self.sum(self.a.len() - 1) >= s);
        let mut k = 1;
        while 2 * k < self.a.len() {
            k *= 2;
        }
        let mut x = 0;
        let mut w = self.zero;
        while k > 0 {
            self.a.get(x + k).map(|a| {
                if w + *a < s {
                    w = w + *a;
                    x += k;
                }
            });
            k >>= 1;
        }
        x + 1
    }
}
// ---------- end fenwick tree ----------
// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, bytes) => {
        read_value!($iter, String).bytes().collect::<Vec<u8>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
// ---------- end input macro ----------

use std::io::Write;

fn run() {
    input! {
        n: usize,
        q: usize,
        a: [i64; n],
        ask: [(u8, usize, usize); q],
    }
    let mut bit = Fenwick::new(n + 1, 0);
    for i in 1..=(n + 1) {
        bit.add(i, 1);
    }
    let mut a = a.clone();
    a.push(0);
    for i in (0..n).rev() {
        a[i] += a[i + 1];
    }
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    for (op, l, r) in ask {
        if op == 1 {
            for _ in 0..(r - l) {
                let x = bit.search(l + 1);
                bit.add(x, !0);
            }
        } else {
            let l = bit.search(l);
            let r = bit.search(r + 1);
            let ans = a[l - 1] - a[r - 1];
            writeln!(out, "{}", ans).ok();
        }
    }
}

fn main() {
    run();
}
0