結果

問題 No.1982 [Cherry 4th Tune B] 絶険
ユーザー akakimidori
提出日時 2022-04-04 21:22:07
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 264 ms / 3,000 ms
コード長 3,063 bytes
コンパイル時間 24,619 ms
コンパイル使用メモリ 374,996 KB
実行使用メモリ 46,432 KB
最終ジャッジ日時 2024-10-09 06:32:33
合計ジャッジ時間 24,714 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    let (_, p, ask) = read();
    #[derive(Debug, Ord, PartialOrd, Eq, PartialEq)]
    enum Event {
        Add(usize, i64),
        Query(usize, i64),
    }
    let mut event = vec![];
    for (i, &(l, r, _, h)) in p.iter().enumerate() {
        event.push((l, Event::Add(i + 1, h)));
        event.push((r + 1, Event::Add(i + 1, -h)));
    }
    for (i, &(a, x)) in ask.iter().enumerate() {
        event.push((a, Event::Query(i, x)));
    }
    event.sort();
    let mut bit = Fenwick::new(p.len(), 0);
    let mut ans = vec![-1; ask.len()];
    for (_, e) in event {
        match e {
            Event::Add(x, v) => bit.add(x, v),
            Event::Query(k, s) => {
                if bit.sum(p.len()) >= s {
                    let pos = bit.search(s);
                    ans[k] = p[pos - 1].2;
                }
            },
        }
    }
    let mut out = String::new();
    for ans in ans {
        use std::fmt::*;
        writeln!(&mut out, "{}", ans).ok();
    }
    print!("{}", out);
}

fn read() -> (usize, Vec<(usize, usize, i32, i64)>, Vec<(usize, i64)>) {
    let mut s = String::new();
    use std::io::Read;
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace().flat_map(|s| s.parse::<usize>());
    let mut next = || it.next().unwrap();
    let n = next();
    let k = next();
    let q = next();
    let p = (0..k)
        .map(|_| (next(), next(), next() as i32, next() as i64))
        .collect::<Vec<_>>();
    let ask = (0..q).map(|_| (next(), next() as i64)).collect::<Vec<_>>();
    (n, p, ask)
}

// ---------- 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 ----------
0