// ---------- begin SegmentTree Point update Range query ---------- mod segment_tree { pub struct PURQ { size: usize, data: Vec, e: T, op: F, } #[allow(dead_code)] impl PURQ where T: Clone, F: Fn(&T, &T) -> T, { pub fn new(size: usize, e: T, op: F) -> PURQ { let size = size.next_power_of_two(); PURQ { size, data: vec![e.clone(); 2 * size], e: e, op: op, } } pub fn update(&mut self, x: usize, v: T) { assert!(x < self.size); let mut x = x + self.size; let data = &mut self.data; data[x] = v; x >>= 1; while x > 0 { data[x] = (self.op)(&data[2 * x], &data[2 * x + 1]); x >>= 1; } } pub fn update_tmp(&mut self, x: usize, v: T) { assert!(x < self.size); self.data[x + self.size] = v; } pub fn update_all(&mut self) { let data = &mut self.data; for k in (1..self.size).rev() { data[k] = (self.op)(&data[2 * k], &data[2 * k + 1]); } } pub fn find(&self, l: usize, r: usize) -> T { assert!(l <= r && r <= self.size); if l == r { return self.e.clone(); } let mut p = self.e.clone(); let mut q = self.e.clone(); let mut l = l + self.size; let mut r = r + self.size; let data = &self.data; while l < r { if l & 1 == 1 { p = (self.op)(&p, &data[l]); l += 1; } if r & 1 == 1 { r -= 1; q = (self.op)(&data[r], &q); } l >>= 1; r >>= 1; } (self.op)(&p, &q) } } } // ---------- end SegmentTree Point update Range query ---------- // ---------- begin chmin, chmax ---------- pub trait ChangeMinMax { fn chmin(&mut self, x: Self) -> bool; fn chmax(&mut self, x: Self) -> bool; } impl ChangeMinMax for T { fn chmin(&mut self, x: Self) -> bool { *self > x && { *self = x; true } } fn chmax(&mut self, x: Self) -> bool { *self < x && { *self = x; true } } } // ---------- end chmin, chmax ---------- // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, bytes) => { read_value!($iter, String).bytes().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ---------- fn run() { input! { n: usize, p: [usize; n], a: [usize; n], } let mut a = a; a.insert(0, 0); let mut s = p.clone(); s.push(0); for i in (0..n).rev() { s[i] += s[i + 1]; } let mut seg = segment_tree::PURQ::new(n, (n + 1, n), |a, b| std::cmp::min(*a, *b)); for (i, p) in p.iter().enumerate() { seg.update_tmp(i, (*p, i)); } seg.update_all(); let mut dfs = vec![(0, n)]; let mut ans = vec![0; n + 1]; while let Some((l, r)) = dfs.pop() { if l >= r { continue; } let (val, x) = seg.find(l, r); dfs.push((l, x)); dfs.push((x + 1, r)); let left = x - l; let right = r - x - 1; if left <= right { for l in (l..=x).rev() { let mut ok = x; let mut ng = n + 1; while ng - ok > 1 { let mid = (ok + ng) / 2; if s[l] - s[mid] > a[val] { ng = mid; } else { ok = mid; } } if ok > x { ans[val] += ok - x; } } } else { for r in (x + 1)..=r { let mut ok = x; let mut ng = 0; while ok - ng > 1 { let mid = (ok + ng) / 2; if s[mid] - s[r] > a[val] { ng = mid; } else { ok = mid; } } if ng == 0 && s[0] - s[r] <= a[val] { ok = 0; } if ok <= x { ans[val] += x - ok + 1; } } } } use std::io::Write; let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); for a in ans[1..].iter() { writeln!(out, "{}", *a).ok(); } } fn main() { run(); }