use std::collections::BTreeSet; #[allow(unused_imports)] use proconio::{ input, marker::{Bytes, Chars, Usize1}, }; fn main() { input! { n: usize, mut p: [usize; n], a: [usize; n], } p.insert(0, 0); p.push(0); let mut q = vec![0; n + 1]; for i in 1..=n { q[p[i]] = i; } let mut csum = vec![0; p.len() + 1]; for i in 0..p.len() { csum[i + 1] = csum[i] + p[i]; } let mut st = BTreeSet::from([0, n + 1]); for i in 1..=n { let m = q[i]; let mut s = a[i - 1]; let l = *st.range(..m).next_back().unwrap(); let r = *st.range(m..).next().unwrap(); st.insert(m); let mut res = 0; if m - l <= r - m { for &x in p[l + 1..=m].iter().rev() { if s < x { break; } s -= x; res += csum[m + 1..=r].partition_point(|&c| c - csum[m + 1] <= s); } } else { for &x in &p[m..r] { if s < x { break; } s -= x; let t = (m - l) - csum[l + 1..=m].partition_point(|&c| csum[m] - c > s); res += t; } } println!("{}", res); } }