http://acm.hdu.edu.cn/showproblem.php?pid=2688 #include<iostream> using namespace std; #define N 3000005 int c[10001]; int a[N]; int lowbit(int x) { return x&(-x); } void update(int x,int k) { while(x<=10000) { c[x]+=k; x+=lowbit(x); } } int getsum(int x) { int ans=0; while(x>0) { ans+=c[x]; x-=lowbit(x); } return ans; } int main(void) { int n; while(~scanf("%d",&n)) { __int64 ans=0; int i; memset(c,0,sizeof(c)); for(i=1;i<=n;i++) { scanf("%d",&a[i]); update(a[i],1); ans+=getsum(a[i]-1); } int m; scanf("%d",&m); while(m--) { char ch[3]; scanf("%s",ch); if(ch[0]=='Q') printf("%I64d/n",ans); else { int x,y; scanf("%d%d",&x,&y); x++;y++; int temp=a[x]; for(i=x;i<y;i++) { a[i]=a[i+1]; if(a[i]>temp) ans--; else if(a[i]<temp) ans++; } a[y]=temp; } } } } 先用树状数组求出正序对数,然后根据之后的操作模拟即可