hdu 4585 Shaolin

原题链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=46093

  1 #include<algorithm>
  2 #include<iostream>
  3 #include<cstdlib>
  4 #include<cstdio>
  5 const int Max_N = 101000;
  6 struct Node {
  7     int v, s;
  8     Node *ch[2];
  9     inline void set(int _v, int _s , Node *p) {
 10             ch[0] = ch[1] = p;
 11             v = _v, s = _s;
 12         }
 13     inline void push_up() {
 14         s = ch[0]->s + ch[1]->s + 1;
 15     }
 16     inline int cmp(int x) const {
 17         return x > v;
 18     }
 19 };
 20 struct SizeBalanceTree {
 21     Node *tail, *root, *null;
 22     Node stack[Max_N];
 23     void init(){
 24         tail = &stack[0];
 25         null = tail++;
 26         null->set(0, 0, NULL);
 27         root = null;
 28     }
 29     inline Node *newNode(int v) {
 30         Node *p = tail++;
 31         p->set(v, 1, null);
 32         return p;
 33     }
 34     inline void rotate(Node* &x, int d) {
 35         Node *k = x->ch[!d];
 36         x->ch[!d] = k->ch[d];
 37         k->ch[d] = x;
 38         k->s = x->s;
 39         x->push_up();
 40         x = k;
 41     }
 42     inline void Maintain(Node* &x, int d) {
 43         if (x->ch[d] == null) return;
 44         if (x->ch[d]->ch[d]->s > x->ch[!d]->s) {
 45             rotate(x, !d);
 46         } else if (x->ch[d]->ch[!d]->s > x->ch[!d]->s) {
 47             rotate(x->ch[d], d), rotate(x, !d);
 48         } else {
 49             return;
 50         }
 51         Maintain(x, 0), Maintain(x, 1);
 52     }
 53     inline void insert(Node* &x, int v) {
 54         if (x == null) {
 55             x = newNode(v);
 56             return;
 57         } else {
 58             x->s++;
 59             int d = x->cmp(v);
 60             insert(x->ch[d], v);
 61             x->push_up();
 62             Maintain(x, d);
 63         }
 64     }
 65     inline void insert(int v) {
 66         insert(root, v);
 67     }
 68     inline int count(int v) {
 69         Node *x = root;
 70         int res = 0, t = 0;
 71         for (; x->s;) {
 72             t = x->ch[0]->s;
 73             if (v < x->v) x = x->ch[0];
 74             else res += t + 1, x = x->ch[1];
 75         }
 76         return res;
 77      }
 78     inline int kth(int k) {
 79         int t = 0;
 80         Node *x = root;
 81         if (x->s < k) return -1;
 82         for (; x->s;) {
 83             t = x->ch[0]->s;
 84             if (k == t + 1) break;
 85             else if (k <= t) x = x->ch[0];
 86             else k -= t + 1, x = x->ch[1];
 87         }
 88         return x->v;
 89     }
 90     inline int operator[](int k) {
 91         return kth(k);
 92     }
 93 }sbt;
 94 int id[5000010];
 95 int main() {
 96 #ifdef LOCAL
 97     freopen("in.txt", "r", stdin);
 98     freopen("out.txt", "w+", stdout);
 99 #endif
100     int n, t, x, a, b, k;
101     while (~scanf("%d", &n) && n) {
102         sbt.init();
103         scanf("%d%d", &t, &x);
104         sbt.insert(x), id[x] = t;
105         printf("%d 1
", t);
106         for (int i = 2; i <= n; i++) {
107             scanf("%d%d", &t, &x);
108             id[x] = t, k = sbt.count(x);
109             if (!k) a = sbt[1];
110             else if (k == i - 1) a = sbt[i - 1];
111             else {
112                 a = sbt[k];
113                 b = sbt[k + 1];
114                 if (x - a > b - x) a = b;
115             }
116             printf("%d %d
", t, id[a]);
117             sbt.insert(x);
118         }
119     }
120     return 0;
121 }
View Code
By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
原文地址:https://www.cnblogs.com/GadyPu/p/4470897.html