# BZOJ3295：[CQOI2011]动态逆序对——题解

http://www.lydsy.com/JudgeOnline/problem.php?id=3295

5 4
1
5
3
4
2
5
1
4
2

## Sample Output

5
2
2
1

——————————————————————————————

#include<cstdio>
#include<queue>
#include<cctype>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=100001;
int X=0,w=0; char ch=0;
while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
return w?-X:X;
}
struct del{
int t;
int p;
int n;
}q[N],tmp[N];
ll m,n,ans[N],pos[N],tree[N];
inline int lowbit(int t){return t&(-t);}
for(int i=x;i<=n;i+=lowbit(i))tree[i]+=y;
return;
}
ll query(int x){//1-x区间和
ll res=0;
for(int i=x;i>0;i-=lowbit(i))res+=tree[i];
return res;
}
void cdq(int l,int r){
if(l>=r)return;
int mid=(l+r)>>1;
cdq(l,mid);cdq(mid+1,r);
for(int i=l,j=l,p=mid+1;i<=r;i++){
if(j<=mid&&(p>r||q[j].n>q[p].n))tmp[i]=q[j++];
else tmp[i]=q[p++];
}
for(int i=l;i<=r;i++){
q[i]=tmp[i];
else ans[q[i].t]+=query(q[i].p);
}
for(int i=r;i>=l;i--){
else ans[q[i].t]+=query(n)-query(q[i].p);
}
return;
}
bool vis[N];
int main(){
for(int i=n;i>=n-m+1;i--){
q[i].t=i;
q[i].p=pos[q[i].n];
vis[q[i].n]=1;
}
int cnt=n-m;
for(int i=1;i<=n;i++){
if(!vis[i]){
q[cnt].t=cnt;
q[cnt].n=i;
q[cnt--].p=pos[i];
}
}
cdq(1,n);
for(int i=1;i<=n;i++)ans[i]+=ans[i-1];
for(int i=n;i>=n-m+1;i--)printf("%lld\n",ans[i]);
return 0;
}
posted @ 2017-12-15 17:39  luyouqi233  阅读(198)  评论(0编辑  收藏  举报