#include #include #include #include using ll = long long; using ull = unsigned long long; using namespace std; int RMQop(int l,int r){ return min(l,r); } int RMQe(){ return 1001001001; } using RMQ = atcoder::segtree; int N; vector A; vector P; vector I; vector SA; vector ans; RMQ G; void solve(int l,int r){ if(l == r) return; int v = G.prod(l,r); int vp = I[v]; ll ansv = 0; if(vp-l > r-vp){ int pl = l; for(int pr=vp+1; pr<=r; pr++){ while(SA[pr] - SA[pl] > A[v]) pl++; if(pl <= vp) ansv += vp - pl + 1; } } else{ int pr = r; for(int pl=vp; pl>=l; pl--){ while(SA[pr] - SA[pl] > A[v]) pr--; if(pr >= vp+1) ansv += pr - vp; } } ans[v] = ansv; solve(l,vp); solve(vp+1,r); } int main(){ cin >> N; P.resize(N); for(int i=0; i>P[i]; P[i]--; } A.resize(N); for(int i=0; i>A[i]; I.resize(N); for(int i=0; i