当做Splay的模板来用比较好?因为蒟蒻不会Splay= =

简直要跪了。。。调个Splay从中午一直写到晚上。。。还能去考NOI嘛%>_<%

我去真的就是裸的数列操作的说啊!

 

  1 /**************************************************************
  2     Problem: 1251
  3     User: rausen
  4     Language: C++
  5     Result: Accepted
  6     Time:7092 ms
  7     Memory:2572 kb
  8 ****************************************************************/
  9  
 10 #include <cstdio>
 11 #include <algorithm>
 12  
 13 #define lson tr[p].son[0]
 14 #define rson tr[p].son[1]
 15 #define Tag tr[p].tag
 16 #define Key tr[p].key
 17 #define Mx tr[p].mx
 18 #define Sz tr[p].sz
 19 #define Rev tr[p].rev
 20 using namespace std;
 21 const int inf = (int) 1e9;
 22 const int N = 50005;
 23  
 24 struct Splay_node {
 25     int key, tag, mx, sz, fa, son[2];
 26     bool rev;
 27 } tr[N];
 28  
 29 int n, a[N];
 30 int tot, root;
 31 int r1, r2;
 32  
 33 inline int read() {
 34     int x = 0, sgn = 1;
 35     char ch = getchar();
 36     while (ch < '0' || '9' < ch) {
 37         if (ch == '-') sgn = -1;
 38         ch = getchar();
 39     }
 40     while ('0' <= ch && ch <= '9') {
 41         x = x * 10 + ch - '0';
 42         ch = getchar();
 43     }
 44     return sgn * x;
 45 }
 46  
 47 inline void pushdown(int p) {
 48     if (!p) return;
 49     if (Rev) {
 50         swap(lson, rson);
 51         tr[lson].rev ^= 1, tr[rson].rev ^= 1;
 52         Rev = 0;
 53     }
 54     if (Tag != 0) {
 55         Key += Tag, Mx += Tag;
 56         tr[lson].tag += Tag, tr[rson].tag += Tag;
 57         Tag = 0;
 58     }
 59 }
 60  
 61 inline void update(int p) {
 62     pushdown(p);
 63     pushdown(lson), pushdown(rson);
 64     Sz = tr[lson].sz + tr[rson].sz + 1;
 65     Mx = max(Key, max(tr[lson].mx, tr[rson].mx));
 66 }
 67  
 68 inline void rotate(int p, int &k) {
 69     int Fa = tr[p].fa, Gr = tr[Fa].fa;
 70     int l = (tr[Fa].son[1] == p), r = !l;
 71     if (Fa == k) k = p; 
 72     else tr[Gr].son[tr[Gr].son[1] == Fa] = p;
 73     tr[p].fa = Gr, tr[Fa].  fa = p, tr[tr[p].son[r]].fa = Fa;
 74     tr[Fa].son[l] = tr[p].son[r], tr[p].son[r] = Fa;
 75     update(Fa), update(p);
 76 }
 77  
 78 void splay(int p, int &k) {
 79     int Fa, Gr;
 80     while (p != k) {
 81         Fa = tr[p].fa, Gr = tr[Fa].fa;
 82         if (Fa != k)
 83             if ((Fa == tr[Gr].son[0]) ^ (p == tr[Fa].son[0]))
 84                 rotate(p, k);
 85             else rotate(Fa, k); 
 86         rotate(p, k);
 87     }
 88 }
 89  
 90 int select(int p, int rank) {
 91     pushdown(p);
 92     if (tr[lson].sz + 1 == rank)
 93         return p;
 94     if (tr[lson].sz >= rank)
 95         return select(lson, rank);
 96     return select(rson, rank - tr[lson].sz - 1);
 97 }
 98  
 99 inline void split(int l, int r) {
100     r1 = select(root, l);
101     r2 = select(root, r);
102     splay(r1, root);
103     splay(r2, tr[r1].son[1]);
104 }
105  
106 void build(int &p, int l, int r) {
107     p = ++tot;
108     int mid = l + r >> 1;
109     Key = a[mid];
110     if (l == r) {
111         Sz = 1, Mx = Key;
112         return;
113     }
114     if (l < mid)
115         build(lson, l, mid - 1);
116     tr[lson].fa = p;
117     if (mid < r)
118         build(rson, mid + 1, r);
119     tr[rson].fa = p;
120     update(p);
121 }
122  
123 int main() {
124     int Q, oper, x, y, z;
125     n = read() + 2, Q = read();
126     a[1] = a[n] = -inf;
127     build(root, 1, n);
128     tr[0].mx = -inf;
129     while (Q--) {
130         oper = read(), x = read(), y = read();
131         if (oper == 1) {
132             z = read();
133             split(x, y + 2);
134             tr[tr[r2].son[0]].tag += z;
135         } else
136         if (oper == 2) {
137             split(x, y + 2);
138             tr[tr[r2].son[0]].rev ^= 1;
139         } else
140         if (oper == 3) {
141             split(x, y + 2);
142             update(r2);
143             printf("%d\n", tr[tr[r2].son[0]].mx);
144         }
145     }
146     return 0;
147 }
View Code

 

posted on 2014-11-25 20:43  Xs酱~  阅读(199)  评论(0编辑  收藏  举报