首页 > 代码库 > zoj 2112 Dynamic Rankings

zoj 2112 Dynamic Rankings

bzoj1901

  1 #include<bits/stdc++.h>
  2 #define N 200005
  3 #define LL long long
  4 #define inf 0x3f3f3f3f
  5 using namespace std;
  6 inline int ra()
  7 {
  8     int x=0,f=1; char ch=getchar();
  9     while (ch<0 || ch>9) {if (ch==-) f=-1; ch=getchar();}
 10     while (ch>=0 && ch<=9) {x=x*10+ch-0; ch=getchar();}
 11     return x*f;
 12 }
 13 const int M=1300001;
 14 int T,n,m,tmp;
 15 int a[50001],root[N];
 16 int sz,s[M],ls[M],rs[M],v[M],w[M],rnd[M];
 17 void update(int k){s[k]=s[ls[k]]+s[rs[k]]+w[k];}
 18 void rturn(int &k){int t=ls[k]; ls[k]=rs[t]; rs[t]=k; s[t]=s[k]; update(k); k=t;}
 19 void lturn(int &k){int t=rs[k]; rs[k]=ls[t]; ls[t]=k; s[t]=s[k]; update(k); k=t;}
 20 void insert(int &k, int num)
 21 {
 22     if (!k){
 23         k=++sz; s[k]=w[k]=1; rnd[k]=rand(); ls[k]=rs[k]=0; v[k]=num; return;
 24     }
 25     s[k]++;
 26     if (v[k]==num) w[k]++;
 27     else if (num<v[k]) {insert(ls[k],num); if (rnd[ls[k]]<rnd[k]) rturn(k);}
 28     else {insert(rs[k],num); if (rnd[rs[k]]<rnd[k]) lturn(k);}
 29 }
 30 void del(int &k, int num)
 31 {
 32     if (v[k]==num)
 33     {
 34         if (w[k]>=1) {w[k]--; s[k]--; return;}
 35         if (ls[k]*rs[k]==0) k=ls[k]+rs[k];
 36         else if (rnd[ls[k]]<rnd[rs[k]]) {rturn(k); del(k,num);}
 37         else {lturn(k); del(k,num);}
 38     }
 39     else if (num<v[k]) del(ls[k],num),s[k]--;
 40     else del(rs[k],num),s[k]--;
 41 }
 42 void change(int k, int l, int r, int x, int num, int y)
 43 {
 44     del(root[k],y);
 45     insert(root[k],num);
 46     if (l==r) return;
 47     int mid=l+r>>1;
 48     if (x<=mid) change(k<<1,l,mid,x,num,y);
 49     else change(k<<1|1,mid+1,r,x,num,y);
 50 }
 51 void build(int k, int l, int r, int x, int num)
 52 {
 53     insert(root[k],num);
 54     if (l==r) return;
 55     int mid=l+r>>1;
 56     if (x<=mid) build(k<<1,l,mid,x,num);
 57     else  build(k<<1|1,mid+1,r,x,num);
 58 }
 59 void find(int k, int num)
 60 {
 61     if (!k) return ;
 62     if (v[k]<=num) {tmp+=s[ls[k]]+w[k]; find(rs[k],num);}
 63     else find(ls[k],num);
 64 }
 65 void query(int k, int l, int r, int x, int y, int num)
 66 {
 67     if (l==x && y==r) {
 68         find(root[k],num);
 69         return;
 70     }
 71     int mid=l+r>>1;
 72     if (y<=mid) query(k<<1,l,mid,x,y,num);
 73     else if (x>mid) query(k<<1|1,mid+1,r,x,y,num);
 74     else query(k<<1,l,mid,x,mid,num),query(k<<1|1,mid+1,r,mid+1,y,num);
 75 }
 76 int main()
 77 {
 78     int T=ra();
 79     while (T--)
 80     {
 81         memset(root,0,sizeof(root)); sz=0;
 82         n=ra(); m=ra();
 83         for (int i=1; i<=n; i++) a[i]=ra();
 84         for (int i=1; i<=n; i++) build(1,1,n,i,a[i]);
 85         for (int i=1; i<=m; i++)
 86         {
 87             char s[3]; scanf("%s",s);
 88             if (s[0]==C)
 89             {
 90                 int x=ra(),y=ra();
 91                 change(1,1,n,x,y,a[x]);
 92                 a[x]=y;
 93             }
 94             else 
 95             {
 96                 int x=ra(),y=ra(),z=ra();
 97                 int l=0,r=1000000000;
 98                 while (l<=r)
 99                 {
100                     int mid=(l+r)>>1;
101                     tmp=0; query(1,1,n,x,y,mid);
102                     if (tmp>=z) r=mid-1;
103                     else l=mid+1;
104                 }
105                 printf("%d\n",l);
106             }
107             //system("pause");
108         }
109     }
110     return 0;
111 }

 

zoj 2112 Dynamic Rankings