結果

問題 No.1441 MErGe
ユーザー fukafukatanifukafukatani
提出日時 2021-04-03 21:48:48
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 427 ms / 1,000 ms
コード長 2,937 bytes
コンパイル時間 18,122 ms
コンパイル使用メモリ 388,604 KB
実行使用メモリ 11,424 KB
最終ジャッジ日時 2024-06-07 12:54:49
合計ジャッジ時間 30,296 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 10 ms
5,376 KB
testcase_06 AC 10 ms
5,376 KB
testcase_07 AC 11 ms
5,376 KB
testcase_08 AC 111 ms
5,376 KB
testcase_09 AC 114 ms
5,376 KB
testcase_10 AC 185 ms
5,376 KB
testcase_11 AC 176 ms
5,376 KB
testcase_12 AC 187 ms
5,376 KB
testcase_13 AC 271 ms
10,192 KB
testcase_14 AC 271 ms
10,448 KB
testcase_15 AC 292 ms
10,672 KB
testcase_16 AC 262 ms
10,312 KB
testcase_17 AC 261 ms
9,760 KB
testcase_18 AC 355 ms
8,920 KB
testcase_19 AC 388 ms
9,780 KB
testcase_20 AC 305 ms
8,352 KB
testcase_21 AC 291 ms
7,296 KB
testcase_22 AC 427 ms
8,852 KB
testcase_23 AC 386 ms
11,296 KB
testcase_24 AC 380 ms
11,176 KB
testcase_25 AC 377 ms
11,292 KB
testcase_26 AC 386 ms
11,424 KB
testcase_27 AC 373 ms
11,420 KB
testcase_28 AC 116 ms
11,424 KB
testcase_29 AC 114 ms
11,296 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `ok`
  --> src/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

ソースコード

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