#include #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using std::cin; using std::cout; using std::endl; using std::vector; using ll = long long; #define rep(i,j,k) for(int i=int(j); i=int(k); i--) int main(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); ll N; cin >> N; vector P(N); rep(i,0,N) cin >> P[i]; vector sum(N+1), rev(N); rep(i,0,N){ sum[i+1] = sum[i]+P[i]; rev[--P[i]] = i; } std::set set; set.insert(-1); set.insert(N); rep(i,0,N){ ll A; cin >> A; auto itr = set.lower_bound(rev[i]); ll right = *itr; itr--; ll left = *itr; ll ans = 0; if(false){ rep(r,rev[i],right){ //右端固定 ll l = std::lower_bound(sum.begin(),sum.end(),sum[r+1]-A)-sum.begin(); //left < l ≤ rev[i] を満たす必要がある if(rev[i] < l) break; l = std::max(l, left+1); l = std::min(l, rev[i]+1); ans += rev[i]-l+1; } } else{ per(l,rev[i],left+1){ //左端固定 ll r = std::upper_bound(sum.begin(),sum.end(),sum[l]+A)-sum.begin()-2; //rev[i] ≤ r < right を満たす必要がある if(r < rev[i]) break; r = std::min(r, right-1); r = std::max(r, rev[i]-1); ans += r-rev[i]+1; } } cout << ans << "\n"; set.insert(rev[i]); } }