#include <bits/stdc++.h> using namespace std; #define endl '\n' #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define ll long long #define ld long double ll mod=1000000007; int inf=1000000007; ll infl=1000000000000000007; const int N=200007; int p[N]; ll ans[N]; int A[N],B[N]; int n; vector<pair<pair<int,int>,int>>V[2*N]; int id[N]; vector<ll>sum; vector<int>nx; vector<pair<int,int>>Q[N]; int BIT[N]; int que(int x) { int s=0; for (;x;x-=x&-x) s+=BIT[x]; return s; } void ins(int x,int s) { for (;x<=n;x+=x&-x) BIT[x]+=s; } void solve(vector<int>P,int of,bool rev) { n=sz(P); for(int i=0;i<n;i++) p[i+1]=P[i]; for(int i=1;i<=n;i++) { A[i]=p[i]; B[p[i]]=i; } for(int i=1;i<=n;i++) { A[i]=max(A[i],A[i-1]); B[i]=max(B[i],B[i-1]); } for(int i=1;i<=n;i++) V[i+p[i]].pb({{i,p[i]},-1}); sum.clear(),nx.clear(); sum.pb(0),nx.pb(0); int it=0; for(int i=1;i<=2*n;i++) { sort(all(V[i])); for(int j=0;j<sz(V[i]);j++) { if(j>0&&V[i][j].st==V[i][j-1].st) nx[V[i][j].nd]=it; else { it++; sum.pb(0),nx.pb(0); int x=V[i][j].st.st,y=V[i][j].st.nd; Q[x].pb({y,it}); if(V[i][j].nd==-1) id[x]=it; else nx[V[i][j].nd]=it; if(B[y]+A[x]>x+y) V[B[y]+A[x]].pb({{B[y],A[x]},it}); } } V[i].clear(); } for(int i=1;i<=n;i++) BIT[i]=0; for(int i=n;i>0;i--) { for(auto q:Q[i]) sum[q.nd]=que(n-q.st+1); Q[i].clear(); ins(n-p[i]+1,1); } for(int i=it;i>0;i--) if(nx[i]!=0) sum[i]+=sum[nx[i]]; for(int i=1;i<=n;i++) { if(rev) ans[n-i+1+of]+=sum[id[i]]; else ans[i+of]+=sum[id[i]]; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int m,x; cin>>m; int mx=0,cnt=0; vector<int>P; for(int i=1;i<=m;i++) { cin>>x; P.pb(x); mx=max(mx,x); if(mx==i) { for(auto x:P) ans[x]+=(ll)(sz(P)-1); for(int j=0;j<sz(P);j++) P[j]-=cnt; solve(P,cnt,0); reverse(all(P)); for(int j=0;j<sz(P);j++) P[j]=sz(P)-P[j]+1; solve(P,cnt,1); cnt+=sz(P); P.clear(); } } for(int i=1;i<=m;i++) cout<<ans[i]<<" "; cout<<endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | #include <bits/stdc++.h> using namespace std; #define endl '\n' #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define ll long long #define ld long double ll mod=1000000007; int inf=1000000007; ll infl=1000000000000000007; const int N=200007; int p[N]; ll ans[N]; int A[N],B[N]; int n; vector<pair<pair<int,int>,int>>V[2*N]; int id[N]; vector<ll>sum; vector<int>nx; vector<pair<int,int>>Q[N]; int BIT[N]; int que(int x) { int s=0; for (;x;x-=x&-x) s+=BIT[x]; return s; } void ins(int x,int s) { for (;x<=n;x+=x&-x) BIT[x]+=s; } void solve(vector<int>P,int of,bool rev) { n=sz(P); for(int i=0;i<n;i++) p[i+1]=P[i]; for(int i=1;i<=n;i++) { A[i]=p[i]; B[p[i]]=i; } for(int i=1;i<=n;i++) { A[i]=max(A[i],A[i-1]); B[i]=max(B[i],B[i-1]); } for(int i=1;i<=n;i++) V[i+p[i]].pb({{i,p[i]},-1}); sum.clear(),nx.clear(); sum.pb(0),nx.pb(0); int it=0; for(int i=1;i<=2*n;i++) { sort(all(V[i])); for(int j=0;j<sz(V[i]);j++) { if(j>0&&V[i][j].st==V[i][j-1].st) nx[V[i][j].nd]=it; else { it++; sum.pb(0),nx.pb(0); int x=V[i][j].st.st,y=V[i][j].st.nd; Q[x].pb({y,it}); if(V[i][j].nd==-1) id[x]=it; else nx[V[i][j].nd]=it; if(B[y]+A[x]>x+y) V[B[y]+A[x]].pb({{B[y],A[x]},it}); } } V[i].clear(); } for(int i=1;i<=n;i++) BIT[i]=0; for(int i=n;i>0;i--) { for(auto q:Q[i]) sum[q.nd]=que(n-q.st+1); Q[i].clear(); ins(n-p[i]+1,1); } for(int i=it;i>0;i--) if(nx[i]!=0) sum[i]+=sum[nx[i]]; for(int i=1;i<=n;i++) { if(rev) ans[n-i+1+of]+=sum[id[i]]; else ans[i+of]+=sum[id[i]]; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int m,x; cin>>m; int mx=0,cnt=0; vector<int>P; for(int i=1;i<=m;i++) { cin>>x; P.pb(x); mx=max(mx,x); if(mx==i) { for(auto x:P) ans[x]+=(ll)(sz(P)-1); for(int j=0;j<sz(P);j++) P[j]-=cnt; solve(P,cnt,0); reverse(all(P)); for(int j=0;j<sz(P);j++) P[j]=sz(P)-P[j]+1; solve(P,cnt,1); cnt+=sz(P); P.clear(); } } for(int i=1;i<=m;i++) cout<<ans[i]<<" "; cout<<endl; return 0; } |