BZOJ - 3224 Tyvj 1728 普通平衡树 (treap/树状数组)

题目链接

treap及树状数组模板题。

treap版:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 typedef double db;
 5 const int N=1e5+10,inf=0x7fffffff;
 6 int m,ch[N][2],val[N],siz[N],rd[N],tot,rt;
 7 void pu(int u) {siz[u]=siz[ch[u][0]]+siz[ch[u][1]]+1;}
 8 void rot(int& u,int f) {
 9     int v=ch[u][f];
10     ch[u][f]=ch[v][f^1],ch[v][f^1]=u;
11     pu(u),pu(v),u=v;
12 }
13 int newnode(int x) {int u=++tot; ch[u][0]=ch[u][1]=0,val[u]=x,siz[u]=1,rd[u]=rand(); return u;}
14 void ins(int& u,int x) {
15     if(!u) {u=newnode(x); return;}
16     int f=x>val[u];
17     ins(ch[u][f],x);
18     if(rd[ch[u][f]]>rd[u])rot(u,f);
19     if(u)pu(u);
20 }
21 void del(int& u,int x) {
22     if(val[u]==x) {
23         if(!ch[u][0])u=ch[u][1];
24         else if(!ch[u][1])u=ch[u][0];
25         else {
26             int f=rd[ch[u][1]]>rd[ch[u][0]];
27             rot(u,f),del(ch[u][f^1],x);
28         }
29     } else del(ch[u][x>val[u]],x);
30     if(u)pu(u);
31 }
32 int lb(int u,int x) {
33     int ret=0;
34     for(; u; u=ch[u][x>val[u]])if(val[u]<x)ret=val[u];
35     return ret;
36 }
37 int ub(int u,int x) {
38     int ret=0;
39     for(; u; u=ch[u][x>=val[u]])if(val[u]>x)ret=val[u];
40     return ret;
41 }
42 int rnk(int u,int x) {
43     int ret=0;
44     for(; u; u=ch[u][x>val[u]]) {
45         if(x>val[u])ret+=siz[ch[u][0]]+1;
46     }
47     return ret+1;
48 }
49 int kth(int u,int k) {
50     while(k!=siz[ch[u][0]]+1) {
51         if(k<siz[ch[u][0]]+1)u=ch[u][0];
52         else k-=siz[ch[u][0]]+1,u=ch[u][1];
53     }
54     return val[u];
55 }
56 int main() {
57     srand(time(0));
58     ins(rt,~inf),ins(rt,inf);
59     scanf("%d",&m);
60     while(m--) {
61         int f,x;
62         scanf("%d%d",&f,&x);
63         if(f==1)ins(rt,x);
64         else if(f==2)del(rt,x);
65         else if(f==3)printf("%d\n",rnk(rt,x)-1);
66         else if(f==4)printf("%d\n",kth(rt,x+1));
67         else if(f==5)printf("%d\n",lb(rt,x));
68         else if(f==6)printf("%d\n",ub(rt,x));
69     }
70     return 0;
71 }

树状数组版:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int N=1e5+10,inf=0x3f3f3f3f;
 5 int m,n,n2,b[N],c[N],Log[N],hb[N];
 6 int lb(int x) {return x&-x;}
 7 void add(int u,int x) {for(; u<=n2; u+=lb(u))c[u]+=x;}
 8 int get(int u) {int ret=0; for(; u; u-=lb(u))ret+=c[u]; return ret;}
 9 int kth(int k) {int ret=0; for(int i=hb[n2]; i; i>>=1)if(ret+i<=n2&&c[ret+i]<k)k-=c[ret+=i]; return ret+1;}
10 struct Q {int f,x;} qr[N];
11 int main() {
12     hb[1]=1;
13     for(int i=2; i<N; ++i)hb[i]=hb[i>>1]<<1;
14     scanf("%d",&m);
15     for(int i=0; i<m; ++i)scanf("%d%d",&qr[i].f,&qr[i].x);
16     for(int i=0; i<m; ++i)if(qr[i].f!=4)b[n2++]=qr[i].x;
17     sort(b,b+n2),n2=unique(b,b+n2)-b;
18     for(int i=0; i<m; ++i)if(qr[i].f!=4)qr[i].x=lower_bound(b,b+n2,qr[i].x)-b+1;
19     for(int i=0; i<m; ++i) {
20         int f=qr[i].f,x=qr[i].x;
21         if(f==1)add(x,1);
22         else if(f==2)add(x,-1);
23         else if(f==3)printf("%d\n",get(x-1)+1);
24         else if(f==4)printf("%d\n",b[kth(x)-1]);
25         else if(f==5)printf("%d\n",b[kth(get(x-1))-1]);
26         else if(f==6)printf("%d\n",b[kth(get(x)+1)-1]);
27     }
28     return 0;
29 }

 

posted @ 2019-04-11 14:51  jrltx  阅读(173)  评论(0编辑  收藏  举报