BZOJ3744 Gty的妹子序列(分块+树状数组)

题意

询问区间内逆序对数  强制在线

1<=n<=50000 1<=m<=50000

题解

两个预处理f[i][j]为块i到j的逆序对数,s[i][j]前i块≤j的有多少个
边角余料用个树状数组就行了

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=;
int n,Block,a[N],b[N],block[N],R[],L[],f[][],tr[][N],m,ans;
int lowbit(int x){
return x&-x;
}
void add(int id,int x,int w){
for(int i=x;i<=n;i+=lowbit(i)){
tr[id][i]+=w;
}
}
int getsum(int id,int x){
int tmp=;
for(int i=x;i;i-=lowbit(i)){
tmp+=tr[id][i];
}
return tmp;
}
int main(){
scanf("%d",&n);
Block=sqrt(n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
block[i]=(i-)/Block+;
if(!L[block[i]])L[block[i]]=i;
R[block[i]]=i;
}
sort(b,b++n);
int tot=unique(b+,b++n)-b-;
for(int i=;i<=n;i++){
a[i]=lower_bound(b+,b++tot,a[i])-b;
}
for(int i=;i<=block[n];i++){
for(int j=L[i];j<=n;j++){
add(i,a[j],);
}
}
for(int i=;i<=block[n];i++){
int tot=;
for(int j=L[i];j<=n;j++){
add(,a[j],);
tot+=getsum(,n)-getsum(,a[j]);
f[i][block[j]]=tot;
}
for(int j=L[i];j<=n;j++){
add(,a[j],-);
}
}
scanf("%d",&m);
while(m--){
int l,r;
scanf("%d%d",&l,&r);
l=l^ans;r=r^ans;
if(l>r)swap(l,r);
if(l==||r==||l>n||r>n)continue;
if(block[l]+>=block[r]){
ans=;
for(int i=l;i<=r;i++){
add(,a[i],);
ans+=getsum(,n)-getsum(,a[i]);
}
for(int i=l;i<=r;i++){
add(,a[i],-);
}
}
else{
ans=f[block[l]+][block[r]-];
int size=L[block[r]]-R[block[l]]-;
// cout<<ans<<" "<<size<<"sdjksd"<<endl;
for(int i=l;i<=R[block[l]];i++){
ans+=getsum(block[l]+,a[i]-)-getsum(block[r],a[i]-);
add(,a[i],);
ans+=getsum(,n)-getsum(,a[i]);
}
for(int i=L[block[r]];i<=r;i++){
ans+=size-(getsum(block[l]+,a[i])-getsum(block[r],a[i]));
add(,a[i],);
ans+=getsum(,n)-getsum(,a[i]);
}
for(int i=l;i<=R[block[l]];i++){
add(,a[i],-);
}
for(int i=L[block[r]];i<=r;i++){
add(,a[i],-);
}
}
printf("%d\n",ans);
}
return ;
}
上一篇:Visual Studio Code 快捷键大全(Windows)


下一篇:visual studio 2013快捷键与2012不同