結果

問題 No.631 Noelちゃんと電車旅行
ユーザー hatoohatoo
提出日時 2018-01-10 20:41:10
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 436 ms / 2,000 ms
コード長 4,215 bytes
コンパイル時間 13,954 ms
コンパイル使用メモリ 402,544 KB
実行使用メモリ 5,904 KB
最終ジャッジ日時 2024-10-02 09:33:54
合計ジャッジ時間 20,288 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 202 ms
5,248 KB
testcase_01 AC 433 ms
5,900 KB
testcase_02 AC 431 ms
5,904 KB
testcase_03 AC 436 ms
5,776 KB
testcase_04 AC 430 ms
5,900 KB
testcase_05 AC 430 ms
5,780 KB
testcase_06 AC 131 ms
5,248 KB
testcase_07 AC 59 ms
5,248 KB
testcase_08 AC 297 ms
5,248 KB
testcase_09 AC 346 ms
5,248 KB
testcase_10 AC 211 ms
5,248 KB
testcase_11 AC 225 ms
5,248 KB
testcase_12 AC 233 ms
5,248 KB
testcase_13 AC 208 ms
5,248 KB
testcase_14 AC 76 ms
5,248 KB
testcase_15 AC 212 ms
5,248 KB
testcase_16 AC 1 ms
5,248 KB
testcase_17 AC 1 ms
5,248 KB
testcase_18 AC 1 ms
5,248 KB
testcase_19 AC 1 ms
5,248 KB
testcase_20 AC 1 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::iter::FromIterator;
#[allow(unused_imports)]
use std::io::stdin;
mod util {
    use std::io::stdin;
    use std::str::FromStr;
    use std::fmt::Debug;
    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }
    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }
}
#[allow(unused_macros)]
macro_rules ! get { ( $ t : ty ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . trim ( ) . parse ::<$ t > ( ) . unwrap ( ) } } ; ( $ ( $ t : ty ) ,* ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; let mut iter = line . split_whitespace ( ) ; ( $ ( iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) , ) * ) } } ; ( $ t : ty ; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ ( $ t : ty ) ,*; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ ( $ t ) ,* ) ) . collect ::< Vec < _ >> ( ) } ; ( $ t : ty ;; ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . split_whitespace ( ) . map ( | t | t . parse ::<$ t > ( ) . unwrap ( ) ) . collect ::< Vec < _ >> ( ) } } ; }
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { println ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }

struct Bucket {
    buf: Vec<u64>,
    // (delta, maximum)
    parent: Vec<(u64, u64)>,
    sqrt: usize,
}

impl Bucket {
    fn new(size: usize) -> Bucket {
        let sqrt = (1..).find(|x| x * x >= size).unwrap();

        Bucket {
            buf: vec![0; sqrt * sqrt],
            parent: vec![(0, 0); sqrt],
            sqrt: sqrt,
        }
    }

    fn p(&mut self, i: usize) -> &mut (u64, u64) {
        &mut self.parent[i / self.sqrt]
    }

    fn set(&mut self, i: usize, x: u64) {
        self.buf[i] = x;
        let prev_max = self.p(i).1;
        self.p(i).1 = max(prev_max, x);
    }

    // (left cut, middle, right_cut)
    fn ranges(
        &self,
        l: usize,
        r: usize,
    ) -> (
        std::ops::Range<usize>,
        std::ops::Range<usize>,
        std::ops::Range<usize>,
    ) {
        if l / self.sqrt == r / self.sqrt {
            return (l..r, 0..0, 0..0);
        }
        let left = l..min((l + self.sqrt - 1) / self.sqrt * self.sqrt, r);
        let mid = (l + self.sqrt - 1) / self.sqrt..r / self.sqrt;
        let right = r / self.sqrt * self.sqrt..r;

        (left, mid, right)
    }

    fn range_add(&mut self, l: usize, r: usize, delta: u64) {
        let (left, mid, right) = self.ranges(l, r);

        for i in left.chain(right) {
            self.buf[i] += delta;
            self.p(i).1 = max(self.buf[i] + self.p(i).0, self.p(i).1);
        }

        for i in mid {
            self.parent[i].0 += delta;
            self.parent[i].1 += delta;
        }
    }

    fn range_max(&mut self, l: usize, r: usize) -> u64 {
        let (left, mid, right) = self.ranges(l, r);
        let mut res = 0;

        for i in left.chain(right) {
            res = max(res, self.buf[i] + self.p(i).0);
        }

        for i in mid {
            res = max(res, self.parent[i].1);
        }

        res
    }
}

fn main() {
    let n = get!(usize);
    let ts = get!(u64;;);
    let m = get!(usize);
    let lrd = get!(usize, usize, u64; m);

    let mut bucket = Bucket::new(n);

    for (i, &t) in ts.iter().enumerate() {
        bucket.set(i, t + 3 * (n - i - 1) as u64);
    }

    for &(l, r, d) in &lrd {
        bucket.range_add(l - 1, r, d);

        println!("{}", bucket.range_max(0, n));
    }
}
0