【BZOJ 3289】Mato的文件管理 【莫队+BIT】

来源:互联网 发布:优化一个网站多少钱 编辑:程序博客网 时间:2024/05/20 11:24

输出优化写wa了QAQ
害我在bzoj上wa了一次。
我去。

#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<iostream>#include<cctype>#include<vector>using namespace std;#define N 50010#define g() getchar()#define p(x) putchar(x)#define d(x) isdigit(x)char ch;template<class T>inline void F(T& x){    for(ch=g();!d(ch);ch=g());    for(x=0;d(ch);x=x*10+ch-'0',ch=g());}char otmp[20];int ocnt;template<class T>inlinevoid P(T x){    if(x==0){p('0');return;}    while(x){otmp[++ocnt]=x%10,x/=10;}    while(ocnt){p('0'+otmp[ocnt--]);}}#define pb(x) push_back(x)#define rep(i,s,t) for(int i=(s);i<=(t);i++)int n,a[N],q,pos[N];vector<int> hs;inline int getrank(int x){return lower_bound(hs.begin(),hs.end(),x)-hs.begin()+1;}struct Qst{int l,r,id;}e[N];bool cmp(Qst x,Qst y){return pos[x.l]==pos[y.l]?x.r<y.r:x.l<y.l;}int ans = 0,tr[N],aws[N];#define lowbit(x) (x&(-x))void update(int x,int add){    for(;x<=n;x+=lowbit(x))        tr[x]+=add;}int query(int x){    int ret=0;    for(;x;x-=lowbit(x))        ret+=tr[x];    return ret;}int main(){    F(n);rep(i,1,n)F(a[i]),hs.pb(a[i]);    sort(hs.begin(),hs.end());    hs.erase(unique(hs.begin(),hs.end()),hs.end());    rep(i,1,n)a[i]=getrank(a[i]);    int block = (int)sqrt(n);    rep(i,1,n)pos[i]=(i-1)/block+1;    F(q);rep(i,1,q)F(e[i].l),F(e[i].r),e[i].id=i;    sort(e+1,e+1+q,cmp);    int l=1,r=0;    rep(i,1,q){        for(;r<e[i].r;r++){            ans+=(r-l+1-query(a[r+1]));            update(a[r+1],1);        }for(;r>e[i].r;r--){            ans-=(r-l+1-query(a[r]));            update(a[r],-1);        }for(;l<e[i].l;l++){            ans-=query(a[l]-1);            update(a[l],-1);        }for(;l>e[i].l;l--){            ans+=query(a[l-1]-1);            update(a[l-1],1);        }        aws[e[i].id]=ans;    }       rep(i,1,q)P(aws[i]),p('\n');    return 0;}
1 0