1. 程式人生 > >BZOJ3236: [Ahoi2013]作業 樹狀數組維護 莫隊

BZOJ3236: [Ahoi2013]作業 樹狀數組維護 莫隊

樹狀 struct ~~ sca spa fine duang return space

水果~~~~

關於四個while可行性的證明:區間有正確性所以不管那團小東西用沒有duang~反它最終總會由於兩次覆蓋二準確

關於區間種數可行性的證明:他會在0 1間(或兩邊)來回跳動(過程中),最終會停在一個大於等於0的地方由於多次覆蓋,最終也會趨於準確

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define M 1000005
#define N 100005
using namespace std;
int zero[N],a[N],b[N],pos[N],len,l,r,had[N],n,m;
struct Q { int l,r,ans1,ans2,z,y,id; }q[M]; int comp(const Q x,const Q y) { return pos[x.l]<pos[y.l]||(pos[x.l]==pos[y.l]&&x.r<y.r); } int end_comp(const Q x,const Q y) { return x.id<y.id; } inline void update_a(int x,int i) { while(x<=n) { a[x]+=i; x+=x&(-x); } } inline
void update_b(int x,int i) { while(x<=n) { b[x]+=i; x+=x&(-x); } } inline int sum_a(int x) { int ret=0; while(x>0) { ret+=a[x]; x-=x&(-x); } return ret; } inline int sum_b(int x) { int ret=0; while(x>0) { ret+=b[x]; x-=x&(-x); }
return ret; } void pre() { scanf("%d%d",&n,&m); len=(int)(sqrt(n+0.5)); for(int i=1;i<=n;i++) { scanf("%d",&zero[i]); pos[i]=(i-1)/len+1; } l=r=1; update_a(zero[1],1); update_b(zero[1],1); had[zero[1]]=1; for(int i=1;i<=m;i++) { scanf("%d%d%d%d",&q[i].l,&q[i].r,&q[i].z,&q[i].y); q[i].id=i; } sort(q+1,q+m+1,comp); } inline void via(int pl,int i) { if(i==1) { update_a(zero[pl],1); had[zero[pl]]++; if(had[zero[pl]]==1) update_b(zero[pl],1); } else { update_a(zero[pl],-1); had[zero[pl]]--; if(had[zero[pl]]==0) update_b(zero[pl],-1); } } void work() { for(int i=1;i<=m;i++) { while(l<q[i].l)via(l++,-1); while(l>q[i].l)via(--l,1); while(r<q[i].r)via(++r,1); while(r>q[i].r)via(r--,-1); q[i].ans1=sum_a(q[i].y)-sum_a(q[i].z-1); q[i].ans2=sum_b(q[i].y)-sum_b(q[i].z-1); } } void print() { sort(q+1,q+m+1,end_comp); for(int i=1;i<=m;i++) printf("%d %d\n",q[i].ans1,q[i].ans2); } int main() { //freopen("ahoi2013_homework.in","r",stdin); //freopen("ahoi2013_homework.out","w",stdout); pre(); work(); print(); return 0; }

BZOJ3236: [Ahoi2013]作業 樹狀數組維護 莫隊