• 九度oj 1541 二叉树


    原题链接:http://ac.jobdu.com/problem.php?pid=1541

    简答题如下:

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<cstdio>
     6 const int Max_N = 1100;
     7 struct Node{
     8     int v, s;
     9     Node *pre, *ch[2];
    10     inline void set(int _v = 0, int _s = 0, Node *p = NULL){
    11         v = _v, s = _s;
    12         pre = ch[0] = ch[1] = p;
    13     }
    14     inline  void push_up(){
    15         s = ch[0]->s + ch[1]->s + 1;
    16     }
    17     inline void link(Node *x, bool d){
    18         ch[d] = x;
    19         x->pre = this;
    20     }
    21 };
    22 struct SplayTree{
    23     Node *tail, *root, *null;
    24     Node stack[Max_N], *ptr[Max_N];
    25     void init(){
    26         tail = &stack[0];
    27         null = tail++;
    28         null->set();
    29         root = null;
    30     }
    31     inline  Node *newNode(int v){
    32         Node *p = tail++;
    33         p->set(v, 1, null);
    34         return p;
    35     }
    36     inline void rotate(Node *x, int d){
    37         Node *y = x->pre;
    38         y->ch[!d] = x->ch[d];
    39         if (x->ch[d] != null) x->ch[d]->pre = y;
    40         x->pre = y->pre;
    41         if (y->pre != null) y->pre->ch[y->pre->ch[0] != y] = x;
    42         x->ch[d] = y;
    43         y->pre = x;
    44         y->push_up(), x->push_up();
    45         if (y == root) root = x;
    46     }
    47     inline void update(Node *x){
    48         if (x != null){
    49             update(x->ch[0]);
    50             update(x->ch[1]);
    51             x->push_up();
    52         }
    53     }
    54     void gogo(int n){
    55         int i, a, b;
    56         for (i = 0; i <= n; i++) ptr[i] = newNode(i);
    57         for (i = 1; i <= n; i++){
    58             scanf("%d %d", &a, &b);
    59             if (-1 == a || -1 == b){
    60                 if (-1 == a && b != -1) ptr[i]->link(ptr[b], 1);
    61                 if (-1 == b && a != -1) ptr[i]->link(ptr[a], 0);
    62             } else {
    63                 ptr[i]->link(ptr[a], 0);
    64                 ptr[i]->link(ptr[b], 1);
    65             }
    66             ptr[i]->push_up();
    67             if (ptr[i]->pre == null) root = ptr[i];
    68         }
    69         update(root);
    70     }
    71     inline void work(){
    72         int i, t;
    73         char buf[50];
    74         scanf("%d", &t);
    75         while (t--){
    76             scanf("%s %d", buf, &i);
    77             if ('s' == buf[0]){
    78                 printf("%d
    ", ptr[i]->s);
    79             } else if ('r' == buf[0]){
    80                 if (ptr[i] == root) continue;
    81                 rotate(ptr[i], ptr[i]->pre->ch[0] == ptr[i]);
    82             } else {
    83                 if (ptr[i] == root) printf("-1
    ");
    84                 else printf("%d
    ", ptr[i]->pre->v);
    85             }
    86         }
    87     }
    88 }spt;
    89 int main(){
    90 #ifdef LOCAL
    91     freopen("in.txt", "r", stdin);
    92     freopen("out.txt", "w+", stdout);
    93 #endif
    94     int n;
    95     while (~scanf("%d", &n)){
    96         spt.init(), spt.gogo(n), spt.work();
    97     }
    98     return 0;
    99 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    Apache Beam的特点
    Apache Beam是什么?
    Kudu1.1.0 、 Kudu1.2.0 Kudu1.3.0的版本信息异同比较
    Kudu compaction design
    [转]Oracle trunc()函数的用法
    [转]Charts (Report Builder and SSRS)
    [转]表变量和临时表的比较
    [转]MONTHS_BETWEEN Function
    [转]Grunt 新手一日入门
    [转]Format a ui-grid grid column as currency
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4477592.html
Copyright © 2020-2023  润新知