結果
問題 | No.631 Noelちゃんと電車旅行 |
ユーザー | hatoo |
提出日時 | 2018-01-10 19:53:02 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 547 ms / 2,000 ms |
コード長 | 4,321 bytes |
コンパイル時間 | 22,943 ms |
コンパイル使用メモリ | 383,448 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-10-02 09:34:25 |
合計ジャッジ時間 | 19,699 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 228 ms
6,816 KB |
testcase_01 | AC | 540 ms
6,816 KB |
testcase_02 | AC | 547 ms
6,816 KB |
testcase_03 | AC | 542 ms
6,816 KB |
testcase_04 | AC | 537 ms
6,820 KB |
testcase_05 | AC | 541 ms
6,816 KB |
testcase_06 | AC | 159 ms
6,820 KB |
testcase_07 | AC | 73 ms
6,816 KB |
testcase_08 | AC | 371 ms
6,816 KB |
testcase_09 | AC | 421 ms
6,816 KB |
testcase_10 | AC | 267 ms
6,816 KB |
testcase_11 | AC | 274 ms
6,820 KB |
testcase_12 | AC | 301 ms
6,820 KB |
testcase_13 | AC | 248 ms
6,816 KB |
testcase_14 | AC | 81 ms
6,816 KB |
testcase_15 | AC | 251 ms
6,820 KB |
testcase_16 | AC | 1 ms
6,820 KB |
testcase_17 | AC | 1 ms
6,816 KB |
testcase_18 | AC | 1 ms
6,820 KB |
testcase_19 | AC | 1 ms
6,816 KB |
testcase_20 | AC | 1 ms
6,816 KB |
ソースコード
#[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); } fn range_add(&mut self, mut l: usize, r: usize, delta: u64) { // TODO more elegant if l % self.sqrt != 0 { for i in l..min((l / self.sqrt + 1) * self.sqrt, r) { self.buf[i] += delta; self.p(i).1 = max(self.buf[i] + self.p(i).0, self.p(i).1); } l = (l / self.sqrt + 1) * self.sqrt; if l > r { return; } } while r - l >= self.sqrt { self.p(l).0 += delta; self.p(l).1 += delta; l += self.sqrt; } for i in l..r { self.buf[i] += delta; self.p(i).1 = max(self.buf[i] + self.p(i).0, self.p(i).1); } } fn range_max(&mut self, mut l: usize, r: usize) -> u64 { // TODO more elegant let mut res = 0; if l % self.sqrt != 0 { for i in l..min((l / self.sqrt + 1) * self.sqrt, r) { res = max(res, self.buf[i] + self.p(i).0); } l = (l / self.sqrt + 1) * self.sqrt; if l > r { return res; } } while r - l >= self.sqrt { res = max(res, self.p(l).1); l += self.sqrt; } for i in l..r { res = max(res, self.buf[i] + self.p(i).0); } 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)); } }