bzoj2453 维护队列
同bzoj2120:http://www.cnblogs.com/HugeGun/p/5350712.html
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<string> 6 #include<cstring> 7 #include<cmath> 8 #define re(i,l,r) for(int i=(l);i<=(r);i++) 9 using namespace std; 10 template <typename Q> 11 void inin(Q &ret) 12 { 13 ret=0;int f=0;char ch=getchar(); 14 while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();} 15 while(ch>='0'&&ch<='9')ret=(ret<<3)+(ret<<1)+ch-'0',ch=getchar(); 16 ret=f?-ret:ret; 17 } 18 int n,q,block,m,a[10010],shang[1000010],sorted[10010],wei[10010],sorting[10010]; 19 int query2(int x,int l) 20 { 21 int L=x*block-block+1,R=x*block;R=min(R,n); 22 int mid,ret=L-1,ll=L; 23 while(L<=R) 24 { 25 mid=(L+R)>>1; 26 if(sorted[mid]<l)ret=mid,L=mid+1; 27 else R=mid-1; 28 } 29 return ret-ll+1; 30 } 31 int query(int l,int r) 32 { 33 if(wei[l]==wei[r]) 34 { 35 int ret=0; 36 re(i,l,r)if(sorting[i]<l)ret++; 37 return ret; 38 } 39 else 40 { 41 int ret=0; 42 re(i,l,block*wei[l])if(sorting[i]<l)ret++; 43 re(i,wei[r]*block-block+1,r)if(sorting[i]<l)ret++; 44 re(i,wei[l]+1,wei[r]-1)ret+=query2(i,l); 45 return ret; 46 } 47 } 48 void change(int x,int xx) 49 { 50 re(i,1,n)shang[a[i]]=0; 51 a[x]=xx; 52 re(i,1,n) 53 { 54 int now=sorting[i]; 55 sorting[i]=shang[a[i]],shang[a[i]]=i; 56 if(sorting[i]!=now) 57 { 58 int L=(wei[i]-1)*block+1,R=min(n,wei[i]*block); 59 re(j,L,R)sorted[j]=sorting[j]; 60 sort(sorted+L,sorted+R+1); 61 } 62 } 63 } 64 char s[10]; 65 int main() 66 { 67 inin(n);inin(q); 68 re(i,1,n)inin(a[i]); 69 block=sqrt(n)+log(2*n)/log(2); 70 if(n%block)m=n/block+1; 71 else m=n/block; 72 re(i,1,n)wei[i]=(i-1)/block+1; 73 re(i,1,n)sorting[i]=sorted[i]=shang[a[i]],shang[a[i]]=i; 74 re(i,1,m)sort(sorted+(i-1)*block+1,sorted+min(i*block,n)+1); 75 re(i,1,q) 76 { 77 scanf("%s",s);int x,y; 78 if(s[0]=='Q') 79 { 80 inin(x),inin(y); 81 printf("%d\n",query(x,y)); 82 } 83 else 84 { 85 inin(x),inin(y); 86 change(x,y); 87 } 88 } 89 return 0; 90 }