题面 https://www.luogu.org/problem/P3157 题解 // luogu-judger-enable-o2 #includecstdio #include vector #include algorithm #include iostream using namespace std; const int p= 320 ; int n,m,a[ 100500 ],b[ 100500 ],g[ 100500 ],g0[ 100500 ]
题面
https://www.luogu.org/problem/P3157
题解
// luogu-judger-enable-o2 #include<cstdio> #include<vector> #include<algorithm> #include<iostream> using namespace std; const int p=320; int n,m,a[100500],b[100500],g[100500],g0[100500],oto[100500]; long long ans=0; vector <int> ord[350]; void merge(int l,int r){ if (l==r) return; int mid=(l+r)/2,i,p1,p2; merge(l,mid); merge(mid+1,r); p1=l; p2=mid+1; for (i=l;i<=r;i++) { if (p1==mid+1||(p1<=mid && p2<=r && g[p2]<=g[p1])) { ans+=(mid-p1+1); g0[i]=g[p2++]; } else { g0[i]=g[p1++]; } } for (i=l;i<=r;i++) g[i]=g0[i]; } int main(){ //freopen("wmm.cpp","r",stdin); int i,j,x; scanf("%d %d",&n,&m); for (i=1;i<=n;i++) { scanf("%d",&a[i]); oto[a[i]]=i; g[i]=b[i]=a[i]; ord[i/p].push_back(a[i]); } ans=0; merge(1,n); printf("%lld\n",ans); for (i=0;i<=n/p;i++) sort(ord[i].begin(),ord[i].end()); for (i=1;i<=m-1;i++) { scanf("%d",&x); x=oto[x]; ord[x/p].erase(lower_bound(ord[x/p].begin(),ord[x/p].end(),b[x])); for (j=x-1;j/p==x/p && j>=1;j--) if (b[j]>b[x] && b[j]!=-1) ans--; for (j=x/p-1;j>=0;j--) ans-=(ord[j].end()-upper_bound(ord[j].begin(),ord[j].end(),b[x])); for (j=x+1;j/p==x/p && j<=n;j++) if (b[j]<b[x] && b[j]!=-1) ans--; for (j=x/p+1;j<=n/p;j++) ans-=(lower_bound(ord[j].begin(),ord[j].end(),b[x])-ord[j].begin()); b[x]=-1; printf("%lld\n",ans); } }