Splay初步【bzoj1503】

做了一道水题,把bzoj1503用Splay重新写了一下。

  1 #include <bits/stdc++.h>
  2 #define rep(i, a, b) for (int i = a; i <= b; i++)
  3 #define REP(i, a, b) for (int i = a; i < b; i++)
  4 #define drep(i, a, b) for (int i = a; i >= b; i--)
  5 #define mp make_pair
  6 #define pb push_back
  7 #define clr(x) memset(x, 0, sizeof(x))
  8 #define xx first
  9 #define yy second
 10 using namespace std;
 11 typedef long long i64;
 12 typedef pair<int, int> pii;
 13 const int inf = ~0U >> 1;
 14 const i64 INF = ~0ULL >> 1;
 15 //***************************
 16  
 17 struct node {
 18     node *pre, *s[2];
 19     int key, size, mul;
 20     node() { pre = s[0] = s[1] = 0; size = mul = 1; }
 21     node(int _key) :key(_key) { pre = s[0] = s[1] = 0; size = mul = 1; }
 22     bool getlr() { return pre->s[1] == this; }
 23     node *link(int w, node *p) { s[w] = p; if (p) p->pre = this; return this; }
 24     void update() { size = mul + (s[0] ? s[0]->size : 0) + (s[1] ? s[1]->size : 0); }
 25 } *root;
 26 void rot(node* p) {
 27     node *q = p->pre->pre;
 28     p->getlr() ? p->link(0, p->pre->link(1, p->s[0])) : p->link(1, p->pre->link(0, p->s[1]));
 29     p->pre->update();
 30     if (q) q->link(q->s[1] == p->pre, p);
 31     else { p->pre = 0; root = p; }
 32 }
 33 void splay(node *p, node *tar) {
 34     while (p->pre != tar && p->pre->pre != tar)
 35         p->getlr() == p->pre->getlr() ? (rot(p->pre), rot(p)) : (rot(p), rot(p));
 36     if (p->pre) rot(p);
 37     p->update();
 38 }
 39 void insrt(int k) {
 40     node *p = root, *q = NULL;
 41     while (p) {
 42         q = p; 
 43         if (k > p->key) p = p->s[1];
 44         else if (k < p->key) p = p->s[0];
 45         else break;
 46     }
 47     if (!p) {p = new node(k);
 48         if (!q) { root = p; return; }
 49         q->link(q->key < k, p); q->update(); splay(p, 0);
 50     }
 51     else { p->mul++; splay(p, 0); }
 52 }
 53 node *findkth(int x) {
 54     node *p = root;
 55     node *t;
 56     while (1) {
 57         int w = (p->s[0] ? p->s[0]->size : 0);
 58         if (x <= w) t = p->s[0];
 59         else if (x > w + p->mul) { x -= w + p->mul; t = p->s[1]; }
 60         else break;
 61         p = t;
 62     }
 63     splay(p, 0); return p;
 64 }
 65  
 66 node *find(node *p, int x) {
 67     if (p->key < x) return find(p->s[1], x);
 68     else if (p->key > x) return find(p->s[0], x);
 69     else return p;
 70 }
 71  
 72 int main() {
 73     int ori(0);
 74     int n, m;
 75     scanf("%d%d", &n, &m);
 76     int tot(0);
 77     char op[5]; int x;
 78     while (n--) {
 79         scanf("%s%d", op, &x);
 80         if (op[0] == 'I') {
 81             if (x >= m) {
 82                 ori++;
 83                 insrt(x - tot);
 84             }
 85         }
 86         else if (op[0] == 'A') tot += x;
 87         else if (op[0] == 'S') {
 88             tot -= x;
 89             insrt(m - tot - 1);
 90             splay(find(root, m - tot - 1), 0);
 91             root = root->s[1];
 92             if (root) root->pre = 0;
 93         }
 94         else if (op[0] == 'F') {
 95             if (!root || x > root->size) puts("-1");
 96             else printf("%d\n", findkth(root->size - x + 1)->key + tot);
 97         }
 98     }
 99     printf("%d\n", ori - (root ? root->size : 0));
100     return 0;
101 }
View Code

 

posted @ 2015-12-04 12:40  y7070  阅读(183)  评论(0编辑  收藏  举报