zoukankan      html  css  js  c++  java
  • hdu 4006/AvlTree

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4006 

    这道题以前用c语言写的Avltree水过了。。

    现在接触了c++重写一遍。。。

    由于没有删除操作故不带垃圾回收,具体如下:

      1 #include<cstdio>
      2 #include<cstdlib>
      3 #include<iostream>
      4 #define Max_N 1000100
      5 inline int max(int &a, int &b){
      6     return a > b ? a : b;
      7 }
      8 struct Node *null;
      9 struct Node{
     10     int v, s, Height;
     11     Node *ch[2];
     12     inline void
     13         set(int H = 0, int _v = 0, int _s = 0, Node *p = NULL){
     14             v = _v, s = _s, Height = H;
     15             ch[0] = ch[1] = p;
     16         }
     17     inline void push_up(){
     18         s = ch[0]->s + ch[1]->s + 1;
     19         int t1 = !ch[0]->s ? -1 : ch[0]->Height;
     20         int t2 = !ch[1]->s ? -1 : ch[1]->Height;
     21         Height = max(t1, t2) + 1;
     22     }
     23 };
     24 struct AvlTree{
     25     Node *tail, *root;
     26     Node stack[Max_N];
     27     int top;
     28     void init(){
     29         tail = &stack[0];
     30         null = tail++;
     31         null->set();
     32         root = null;
     33     }
     34     Node *newNode(int v){
     35         Node *p = tail++;
     36         p->set(0, v, 1, null);
     37         return p;
     38     }
     39     inline void rotate(Node* &x, int d){
     40         Node *k = x->ch[!d];
     41         x->ch[!d] = k->ch[d];
     42         k->ch[d] = x;
     43         x->push_up();
     44         k->push_up();
     45         x = k;
     46     }
     47     inline void Maintain(Node* &x, int d){
     48         if (x->ch[d]->Height - x->ch[!d]->Height == 2){
     49             if (x->ch[d]->ch[d]->Height - x->ch[d]->ch[!d]->Height == 1){
     50                 rotate(x, !d);
     51             } else if (x->ch[d]->ch[d]->Height - x->ch[d]->ch[!d]->Height == -1){
     52                 rotate(x->ch[d], d), rotate(x, !d);
     53             }
     54         }
     55     }
     56     inline void insert(Node* &x, int v){
     57         if (x == null){
     58             x = newNode(v);
     59             return;
     60         } else {
     61             int d = v > x->v;
     62             insert(x->ch[d], v);
     63             x->push_up();
     64             Maintain(x, d);
     65         }
     66     }
     67     inline int find_kth(Node *x, int k){
     68         int t = 0;
     69         for (; x != null;){
     70             t = x->ch[0]->s;
     71             if (k == t + 1) break;
     72             else if (k <= t) x = x->ch[0];
     73             else k -= t + 1, x = x->ch[1];
     74         }
     75         return x->v;
     76     }
     77     inline void insert(int v){
     78         insert(root, v);
     79     }
     80     inline void find_kth(int k){
     81         printf("%d
    ", find_kth(root, root->s - k + 1));
     82     }
     83 }Avl;
     84 int main(){
     85 #ifdef LOCAL  
     86     freopen("in.txt", "r", stdin);
     87     freopen("out.txt", "w+", stdout);
     88 #endif  
     89     char ch;
     90     int i, n, k, d;
     91     while (~scanf("%d %d", &n, &k)){
     92         Avl.init();
     93         for (i = 0; i < n; i++){
     94             getchar();
     95             scanf("%c", &ch);
     96             if ('I' == ch) scanf("%d", &d), Avl.insert(d);
     97             else Avl.find_kth(k);
     98         }
     99     }
    100     return 0;
    101 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    docker 目录移动到其他磁盘的操作
    linux -- 查看磁盘空间的大小 查看文件夹占用磁盘空间大小
    python flask 反向代理
    shell 脚本换行符的问题
    uniapp使用web-view跳转vue单页面通信,互发消息
    java基础(多线程---lambda)
    SpringBoot开发详解(五)--Controller接收参数以及参数校验
    使用 TypeScript 来开发 React 的注意事项
    video标签通过js实现增加倍速播放功能
    zblog调取置顶文章
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4454541.html
Copyright © 2011-2022 走看看