BZOJ3289:Mato的文件管理
浅谈莫队:https://www.cnblogs.com/AKMer/p/10374756.html
题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=3289
用树状数组维护区间内元素的个数,动态删/加逆序对即可。
时间复杂度:\(O(n\sqrt{n}logn)\)
空间复杂度:$ O(n)$
代码如下:
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
#define low(i) ((i)&(-(i)))
const int maxn=5e4+5;
ll ans;
int n,m,block,val,cnt;
int tmp[maxn],a[maxn],bel[maxn];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
struct query {
ll res;
int l,r,id;
bool operator<(const query &a)const {
if(bel[l]!=bel[a.l])return bel[l]<bel[a.l];
if(bel[l]&1)return r<a.r;return r>a.r;
}
}q[maxn];
struct TreeArray {
int c[maxn];
void add(int pos,int v) {
for(int i=pos;i<=n;i+=low(i))
c[i]+=v;
}
int query(int pos) {
int res=0;
for(int i=pos;i;i-=low(i))
res+=c[i];
return res;
}
}T;
bool cmp(query a,query b) {
return a.id<b.id;
}
void change(int siz,int v,bool big) {
if(!big)val=T.query(siz-1);
else val=T.query(n)-T.query(siz);
ans+=val*v;T.add(siz,v);
}
int main() {
n=read(),block=sqrt(n);
for(int i=1;i<=n;i++)
tmp[i]=a[i]=read(),bel[i]=(i-1)/block+1;
sort(tmp+1,tmp+n+1),m=read();
cnt=unique(tmp+1,tmp+n+1)-tmp-1;
for(int i=1;i<=m;i++)
q[i].l=read(),q[i].r=read(),q[i].id=i;
for(int i=1;i<=n;i++)
a[i]=lower_bound(tmp+1,tmp+cnt+1,a[i])-tmp;
sort(q+1,q+m+1);
int nowl=1,nowr=0;
for(int i=1;i<=m;i++) {
while(nowl<q[i].l)change(a[nowl++],-1,0);
while(nowl>q[i].l)change(a[--nowl],1,0);
while(nowr<q[i].r)change(a[++nowr],1,1);
while(nowr>q[i].r)change(a[nowr--],-1,1);
q[i].res=ans;
}
sort(q+1,q+m+1,cmp);
for(int i=1;i<=m;i++)
printf("%lld\n",q[i].res);
return 0;
}