复习一波平衡树!

 

3224: Tyvj 1728 普通平衡树

链接

怎么能少得了这道题呢。

  1 #include<cstdio>
  2 #include<cctype>
  3 
  4 const int N = 1000100;
  5 
  6 int siz[N],ch[N][2],fa[N],cnt[N],data[N];
  7 int Root,tn;
  8 
  9 #define ls ch[p][0]
 10 #define rs ch[p][1]
 11 
 12 int son(int x) {
 13     return x==ch[fa[x]][1];
 14 }
 15 void pushup(int p) {
 16     siz[p] = siz[ls] + siz[rs] + cnt[p];
 17 }
 18 void rotate(int x) {
 19     int y = fa[x],z = fa[y],b = son(x),c = son(y),a = ch[x][!b];
 20     if (z) ch[z][c] = x;else Root = x;fa[x] = z;
 21     ch[x][!b] = y;fa[y] = x;
 22     ch[y][b] = a;if (a) fa[a] = y;
 23     pushup(y),pushup(x);
 24 }
 25 void splay(int x,int rt) {
 26     while (fa[x] != rt) {
 27         int y = fa[x],z = fa[y];
 28         if (z==rt) rotate(x);
 29         else {
 30             if (son(x)==son(y)) rotate(y),rotate(x);
 31             else rotate(x),rotate(x); 
 32         }
 33     }
 34 }
 35 int getrnk(int x) {
 36     int p = Root,ans = 0;
 37     while (true) {
 38         if (!p) return ans + 1;
 39         if (x == data[p]) {ans += siz[ls];splay(p,0);return ans+1;}
 40         if (x < data[p]) p = ls;
 41         else { 
 42             ans += siz[ls] + cnt[p];
 43             p = rs;
 44         }
 45     }
 46 }
 47 int getkth(int k) {
 48     int p = Root;
 49     while (true) {
 50         if (siz[ls] < k && k <= siz[ls] + cnt[p]) return data[p];
 51         if (k <= siz[ls]) p = ls;
 52         else {
 53             k -= siz[ls] + cnt[p]; 
 54             p = rs; 
 55         }
 56     }
 57 }
 58 int Newnode(int pa,int d) {
 59     ++tn;siz[tn] = cnt[tn] = 1;ch[tn][1] = ch[tn][0] = 0;
 60     data[tn] = d;fa[tn] = pa;
 61     return tn;
 62 }
 63 void Insert(int x) {
 64     if (!Root) {Root = Newnode(0,x);return ;}
 65     int p = Root,pa = 0;
 66     while (true) {
 67         if (data[p]==x) {
 68             cnt[p] ++;pushup(p);pushup(pa);splay(p,0);return ; // Splay ????????????????? 
 69         }
 70         pa = p; // ??! 
 71         p = ch[p][x > data[p]];
 72         if (!p) {
 73             p = Newnode(pa,x);
 74             ch[pa][x > data[pa]] = p;
 75             pushup(p),pushup(pa);splay(p,0);
 76             break; 
 77         }
 78     }
 79 }
 80 void Clear(int p) {
 81     siz[p] = cnt[p] = ch[p][0] = ch[p][1] = fa[p] = data[p] = 0;
 82 }
 83 void Delete(int x) {
 84     getrnk(x);
 85     int &p = Root,tmp;
 86     if (cnt[p]) {cnt[p]--;pushup(p);return ;}
 87     if (!ls && !rs) {Clear(p);Root = 0;return ;}
 88     if (!ls || !rs) {tmp = p;p = ls?ls:rs;fa[p] = 0;Clear(tmp);return ;}
 89     int pre = ls;
 90     while (ch[pre][1]) pre = ch[pre][1];
 91     tmp = p;p = pre;
 92     splay(p,0);
 93     ch[p][1] = ch[tmp][1];fa[ch[tmp][1]] =  p;
 94     Clear(tmp);pushup(p);
 95 }
 96 int main() {
 97     int T,opt,x;
 98     scanf("%d",&T);
 99     while (T--) {
100         scanf("%d%d",&opt,&x);
101         if (opt==1) Insert(x);
102         else if (opt==2) Delete(x);
103         else if (opt==3) printf("%d\n",getrnk(x));
104         else if (opt==4) printf("%d\n",getkth(x));
105         else if (opt==5) printf("%d\n",getkth(getrnk(x)-1)); 
106         else printf("%d\n",getkth(getrnk(x+1)));
107     } 
108 }
View Code

相关文章: