#[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() -> Vec where ::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, parent: Vec, sqrt: usize, phantom_i: std::marker::PhantomData, } impl Bucket where I::Parent: Clone, { #[allow(dead_code)] fn new(init_elem: Vec, init_parent: I::Parent) -> Self { let sqrt = (1..).find(|x| x * x >= init_elem.len()).unwrap(); let mut parent = vec![init_parent; sqrt]; for (i, e) in init_elem.iter().enumerate() { I::reduce_parent(&mut parent[i / sqrt], e); } Self { buf: init_elem, parent: parent, sqrt: sqrt, phantom_i: std::marker::PhantomData, } } #[allow(dead_code)] fn ranges( &self, l: usize, r: usize, ) -> ( std::ops::Range, std::ops::Range, std::ops::Range, ) { 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) } #[allow(dead_code)] fn pe(&mut self, i: usize) -> (&mut I::Parent, &mut I::Elem) { (&mut self.parent[i / self.sqrt], &mut self.buf[i]) } #[allow(dead_code)] fn add(&mut self, l: usize, r: usize, delta: &I::A) { let (left, mid, right) = self.ranges(l, r); for i in left.chain(right) { let (p, e) = self.pe(i); I::add(p, e, delta); } for i in mid { I::add_parent(&mut self.parent[i], delta); } } #[allow(dead_code)] fn sum(&mut self, l: usize, r: usize) -> Option { let (left, mid, right) = self.ranges(l, r); let mut iter = left.chain(right) .map(|i| I::elem_to_result(&self.buf[i], &self.parent[i / self.sqrt])) .chain(mid.map(|i| I::parent_to_result(&self.parent[i]))); if let Some(mut r) = iter.next() { for x in iter { I::reduce_result(&mut r, &x); } Some(r) } else { None } } } trait BucketImpl { type Elem; type Parent; type A; type R; fn reduce_parent(&mut Self::Parent, &Self::Elem); fn add(&mut Self::Parent, &mut Self::Elem, &Self::A); fn add_parent(&mut Self::Parent, &Self::A); fn parent_to_result(&Self::Parent) -> Self::R; fn elem_to_result(&Self::Elem, p: &Self::Parent) -> Self::R; fn reduce_result(&mut Self::R, &Self::R); } #[allow(dead_code)] struct RangeAddQueryMax( ); impl BucketImpl for RangeAddQueryMax { type Elem = u64; type Parent = (u64, u64); type A = u64; type R = u64; fn reduce_parent(p: &mut Self::Parent, e: &Self::Elem) { p.0 = max(p.1 + e, p.0); } fn add(p: &mut Self::Parent, e: &mut Self::Elem, v: &Self::A) { *e += v; p.0 = max(p.0, *e + p.1); } fn add_parent(p: &mut Self::Parent, d: &Self::A) { p.0 += d; p.1 += d; } fn parent_to_result(p: &Self::Parent) -> Self::R { p.0 } fn elem_to_result(e: &Self::Elem, p: &Self::Parent) -> Self::R { e + p.1 } fn reduce_result(a: &mut Self::R, b: &Self::R) { *a = max(*a, *b); } } fn main() { let n = get!(usize); let ts = get!(u64;;); let m = get!(usize); let lrd = get!(usize, usize, u64; m); let v: Vec = ts.iter() .enumerate() .map(|(i, &t)| t + 3 * (n - i - 1) as u64) .collect(); let mut bucket: Bucket = Bucket::new(v, (0, 0)); for &(l, r, d) in &lrd { bucket.add(l - 1, r, &d); println!("{}", bucket.sum(0, n - 1).unwrap()); } }