結果
| 問題 | No.1441 MErGe | 
| コンテスト | |
| ユーザー |  fukafukatani | 
| 提出日時 | 2021-04-03 21:48:48 | 
| 言語 | Rust (1.83.0 + proconio) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 478 ms / 1,000 ms | 
| コード長 | 2,937 bytes | 
| コンパイル時間 | 16,101 ms | 
| コンパイル使用メモリ | 384,016 KB | 
| 実行使用メモリ | 11,424 KB | 
| 最終ジャッジ日時 | 2024-12-25 16:24:52 | 
| 合計ジャッジ時間 | 25,895 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 28 | 
コンパイルメッセージ
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
            
            ソースコード
#![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)
}
            
            
            
        