Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
45061 lgh [Cqoi2011]动态逆序对 C++ 通过 100 446 MS 41600 KB 1379 2022-02-12 12:50:35

Tests(10/10):


#include<bits/stdc++.h> #define il inline using namespace std; typedef long long ll; const int N=100005,M=105; int gi(){ int str=0;char ch=getchar(); while(ch>'9' || ch<'0')ch=getchar(); while(ch>='0' && ch<='9')str=(str<<1)+(str<<3)+ch-48,ch=getchar(); return str; } int n,m,a[N],del[N],w[N],bol=1000,bel[N],tot,Tree[M][N],s[M],ed[M]; bool d[N];int sz[M]; il void add(int t,int sta,int to){ for(int i=sta;i<=n;i+=(i&(-i)))Tree[t][i]+=to; } il int query(int t,int sta){ int ret=0; for(int i=sta;i>=1;i-=(i&(-i)))ret+=Tree[t][i]; return ret; } void work() { n=gi();m=gi();int cnt=0,tot=1;s[1]=1; for(int i=1;i<=n;i++){ a[i]=gi(),w[a[i]]=i; if(cnt==bol)cnt=0,tot++,s[tot]=i,ed[tot-1]=i-1;bel[i]=tot;cnt++; sz[tot]++; } ed[tot]=n; for(int i=1;i<=tot;i++) for(int j=s[i];j<=ed[i];j++) add(i,a[j],1); for(int i=1;i<=m;i++)del[i]=gi(); ll ans=0; for(int i=1;i<=n;i++) ans+=i-1-query(tot+1,a[i]),add(tot+1,a[i],1); int t,bl; for(int i=1;i<=m;i++){ t=w[del[i]];bl=bel[t]; printf("%lld\n",ans); for(int j=t-1;j>=s[bl];j--) if(!d[j] && a[j]>del[i])ans--; for(int j=t+1;j<=ed[bl];j++) if(!d[j] && a[j]<del[i])ans--; d[t]=true; for(int j=1;j<bl;j++) ans-=sz[j]-query(j,del[i]); for(int j=bl+1;j<=tot;j++) ans-=query(j,del[i]); add(bl,del[i],-1);sz[bl]--; } } int main() { work(); return 0; }


测评信息: