結果

問題 No.1441 MErGe
ユーザー fukafukatanifukafukatani
提出日時 2021-04-03 21:48:48
言語 Rust
(1.77.0)
結果
AC  
実行時間 452 ms / 1,000 ms
コード長 2,937 bytes
コンパイル時間 6,125 ms
コンパイル使用メモリ 151,348 KB
実行使用メモリ 12,416 KB
最終ジャッジ日時 2023-08-26 17:20:51
合計ジャッジ時間 13,942 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 11 ms
4,376 KB
testcase_06 AC 11 ms
4,376 KB
testcase_07 AC 11 ms
4,376 KB
testcase_08 AC 119 ms
4,380 KB
testcase_09 AC 120 ms
4,376 KB
testcase_10 AC 196 ms
5,064 KB
testcase_11 AC 186 ms
4,824 KB
testcase_12 AC 197 ms
5,092 KB
testcase_13 AC 297 ms
10,112 KB
testcase_14 AC 302 ms
10,432 KB
testcase_15 AC 320 ms
10,564 KB
testcase_16 AC 288 ms
10,344 KB
testcase_17 AC 282 ms
9,880 KB
testcase_18 AC 382 ms
9,204 KB
testcase_19 AC 442 ms
9,676 KB
testcase_20 AC 334 ms
8,320 KB
testcase_21 AC 309 ms
7,620 KB
testcase_22 AC 452 ms
8,704 KB
testcase_23 AC 418 ms
11,412 KB
testcase_24 AC 423 ms
11,764 KB
testcase_25 AC 417 ms
11,864 KB
testcase_26 AC 419 ms
11,560 KB
testcase_27 AC 420 ms
11,424 KB
testcase_28 AC 121 ms
11,400 KB
testcase_29 AC 120 ms
12,416 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `ok`
  --> Main.rs:55:10
   |
55 |     let (ok, ng) = binary_search(0, n + 1, |mid: usize| {
   |          ^^ help: if this is intentional, prefix it with an underscore: `_ok`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: 1 warning emitted

ソースコード

diff #

#![allow(unused_imports)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;
use std::ops::Bound::*;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<usize>();
    let (n, q) = (v[0], v[1]);
    let a = read_vec::<i64>();
    let mut queries = vec![];
    for _ in 0..q {
        let v = read_vec::<usize>();
        let (t, l, r) = (v[0], v[1], v[2]);
        queries.push((t, l, r));
    }

    let mut accum = vec![0; n + 1];
    for i in 0..n {
        accum[i + 1] = accum[i] + a[i];
    }

    let mut bit = BinaryIndexTree::new(n + 2);
    for i in 1..=n {
        bit.add(i, 1);
    }

    for (t, l, r) in queries {
        if t == 1 {
            for _ in 0..r - l {
                let i = get_index(l + 1, n, &mut bit);
                bit.add(i, -1);
            }
        } else {
            let l = get_index(l, n, &mut bit);
            let r = get_index(r + 1, n, &mut bit) - 1;
            let ans = accum[r] - accum[l - 1];
            println!("{}", ans);
        }
    }
}

fn get_index(x: usize, n: usize, bit: &BinaryIndexTree) -> usize {
    let (ok, ng) = binary_search(0, n + 1, |mid: usize| {
        if mid == n + 1 {
            false
        } else {
            bit.sum(mid) < x as i64
        }
    });
    ng
}

struct BinaryIndexTree {
    bit: Vec<i64>,
    n: usize,
}

impl BinaryIndexTree {
    fn new(n: usize) -> BinaryIndexTree {
        BinaryIndexTree {
            bit: vec![0; n + 1],
            n: n,
        }
    }

    fn sum(&self, i: usize) -> i64 {
        // assert!(i > 0);
        let mut i = i;
        let mut s = 0i64;
        while i > 0 {
            s += self.bit[i];
            i -= (i as i64 & -(i as i64)) as usize;
        }
        s
    }

    fn add(&mut self, i: usize, x: i64) {
        assert!(i > 0);
        let mut i = i;
        while i <= self.n {
            self.bit[i as usize] += x;
            i += (i as i64 & -(i as i64)) as usize;
        }
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

type Input = usize;
fn binary_search<F>(lb: Input, ub: Input, mut criterion: F) -> (Input, Input)
where
    F: FnMut(Input) -> bool,
{
    assert_eq!(criterion(lb), true);
    assert_eq!(criterion(ub), false);
    let mut ok = lb;
    let mut ng = ub;
    while ng - ok > 1 {
        let mid = (ng + ok) / 2;
        if criterion(mid) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    (ok, ng)
}
0