【模板】文艺平衡树

如题,是一个模板。。。

Splay:

  1 #include <algorithm>
  2 #include <iostream>
  3 #include <cstring>
  4 #include <cstdio>
  5 #include <cctype>
  6 
  7 inline void read(int & x)
  8 {
  9     x = 0;
 10     int k = 1;
 11     char c = getchar();
 12     while (!isdigit(c))
 13         if (c == '-') c = getchar(), k = -1;
 14         else c = getchar();
 15     while (isdigit(c))
 16         x = (x << 1) + (x << 3) + (c ^ 48),
 17         c = getchar();
 18     x *= k;
 19 }
 20 
 21 const int MAXN = 101015;
 22 int n, m, x, y;
 23 
 24 int a[101010];
 25 
 26 struct SplayHello
 27 {
 28     int rt = 0, tot = 0;
 29     int cnt[MAXN];
 30     int siz[MAXN];
 31     int tag[MAXN];
 32     int val[MAXN]; 
 33     int faz[MAXN];
 34     int son[MAXN][2];
 35     
 36     int Getson(int u)
 37     {
 38         return son[faz[u]][1] == u;
 39     }
 40     
 41     void Update(int u)
 42     {
 43         siz[u] = siz[son[u][0]] + siz[son[u][1]] + cnt[u];
 44     }
 45     
 46     void Pushdown(int u)  //ok
 47     {
 48         if (!tag[u] || !u) return;
 49         if (son[u][0]) tag[son[u][0]] ^= 1;
 50         if (son[u][1]) tag[son[u][1]] ^= 1;
 51         son[u][0] ^= son[u][1],
 52         son[u][1] ^= son[u][0],
 53         son[u][0] ^= son[u][1];
 54         tag[u] = 0;
 55     }
 56     
 57     int Build(int dad, int l, int r)  //ok
 58     {
 59         if (l > r) return 0;
 60         int mid = (l + r) >> 1;
 61         int now = ++tot;
 62         tag[now] = 0;
 63         faz[now] = dad;
 64         val[now] = a[mid];
 65         cnt[now] = siz[now] = 1;
 66         son[now][0] = Build(now, l, mid - 1);
 67         son[now][1] = Build(now, mid + 1, r);
 68         //²»ÄÜÓÃtotµ±¸¸Ç×£¬»á¸Ä±ä 
 69         Update(now);
 70         return now;
 71     }
 72     
 73     void Rotate(int u)   //ok
 74     {
 75         int y = faz[u], z = faz[y], ch = Getson(u);
 76         Pushdown(y), Pushdown(u);
 77         int b = son[u][ch ^ 1];
 78         int d = Getson(y);
 79         son[u][ch ^ 1] = y, son[y][ch] = b;
 80         faz[y] = u, faz[b] = y, faz[u] = z;
 81         if (z) son[z][d] = u;
 82         Update(y), Update(u);
 83     }
 84     
 85     void Splay(int u, int tar)  //ok
 86     {
 87         while (faz[u] != tar)
 88         {
 89             if (faz[faz[u]] != tar)
 90                 if (Getson(u) == Getson(faz[u])) Rotate(faz[u]);
 91                 else Rotate(u);
 92             Rotate(u);
 93         }
 94         if (!tar) rt = u;
 95     }
 96     
 97     int GetKth(int k)
 98     {
 99         int now = rt;
100         while (true)
101         {
102             Pushdown(now);
103             if (siz[son[now][0]] >= k) now = son[now][0];
104             else if (siz[son[now][0]] + cnt[now] >= k) return now;
105             else k -= siz[son[now][0]] + cnt[now], now = son[now][1];
106         }
107     }
108     
109     void Rever(int l, int r)
110     {
111         int L = GetKth(l - 1);
112         int R = GetKth(r + 2);
113         Splay(L, 0); Splay(R, L);
114         tag[son[son[rt][1]][0]] ^= 1;
115         Update(L), Update(R), Update(son[R][0]);
116     }
117     
118     void print(int u)
119     {
120         Pushdown(u);
121         if (son[u][0]) print(son[u][0]);
122         if (val[u] != -2147483600 && val[u] != 2147483600) 
123             printf("%d ", val[u]);
124         if (son[u][1]) print(son[u][1]);
125     }
126     
127     void BuildT(int l, int r)
128     {
129         rt = Build(0, l, r);
130     }
131     
132     void printT()
133     {
134         print(rt);
135     }
136 }splay;
137 
138 signed main()
139 {
140     read(n), read(m);
141     for (int i = 1; i <= n; ++i) a[i] = i;
142     a[n + 1] = 2147483600;
143     a[0] = -2147483600;
144     splay.BuildT(0, n + 1);
145     for (int i = 1; i <= m; ++i)
146     {
147         read(x), read(y);
148         splay.Rever(x, y);
149     }
150     splay.printT();
151     return 0;
152 }
FHQ::
  1 #include <algorithm>
  2 #include <iostream>
  3 #include <cstring>
  4 #include <cstdio>
  5 #include <cctype>
  6 
  7 inline void read(int & x)
  8 {
  9     x = 0;
 10     int k = 1;
 11     char c = getchar();
 12     while (!isdigit(c))
 13         if (c == '-') c = getchar(), k = -1;
 14         else c = getchar();
 15     while (isdigit(c))
 16         x = (x << 1) + (x << 3) + (c ^ 48),
 17         c = getchar();
 18     x *= k;
 19 }
 20 
 21 const int MAXN = 1e5 + 999;
 22 int seed = 1024, n, m, l, r, rt = 0, tot = 0, rnd[MAXN], tag[MAXN], val[MAXN], siz[MAXN], son[MAXN][2]; 
 23 
 24 inline int Rand(void)
 25 {
 26     return seed = (int)seed * 462711LL % 2147483647;
 27 }
 28 
 29 inline int New(int x)
 30 {
 31     ++tot;
 32     siz[tot] = 1;
 33     val[tot] = x;
 34     rnd[tot] = Rand();
 35     tag[tot] = son[tot][1] = son[tot][0] = 0;
 36     return tot;    
 37 }
 38 
 39 inline void Swap(int &a, int &b) 
 40 {
 41     a ^= b, b ^= a, a ^= b;
 42 }
 43 
 44 inline void Pushup(int u)
 45 {
 46     siz[u] = siz[son[u][0]] + siz[son[u][1]] + 1;
 47 }
 48 
 49 inline void Pushdown(int u)
 50 {
 51     if (!tag[u]) return;
 52     tag[son[u][0]] ^= 1;
 53     tag[son[u][1]] ^= 1;
 54     Swap(son[u][0], son[u][1]);
 55     tag[u] = 0;
 56 }
 57 
 58 inline void Split(int u, int k, int &a, int &b)
 59 {
 60     if (!u) { a = b = 0; return; }
 61     Pushdown(u);
 62     if (k <= siz[son[u][0]]) b = u, Split(son[u][0], k, a, son[b][0]);
 63     else a = u, Split(son[u][1], k - siz[son[u][0]] - 1, son[a][1], b);
 64     Pushup(u);
 65 }
 66 
 67 inline void Merge(int & u, int a, int b)
 68 {
 69     if (!a || !b) { u = a | b; return; } 
 70     if (rnd[a] < rnd[b]) u = a, Pushdown(a), Merge(son[u][1], son[a][1], b), Pushup(a);
 71     else u = b, Pushdown(b), Merge(son[u][0], a, son[b][0]), Pushup(b);
 72 }
 73 
 74 inline void Insert(int x)
 75 {
 76     int u = New(x);
 77     Merge(rt, rt, u);
 78 }
 79 
 80 inline void Print(int u)
 81 {
 82     if (!u) return;
 83     Pushdown(u);
 84     Print(son[u][0]);
 85     printf("%d ", val[u]);
 86     Print(son[u][1]);
 87 }
 88 
 89 int a, b, c;
 90 
 91 signed main()
 92 {
 93     read(n), read(m);
 94     for (int i = 1; i <= n; ++i) Insert(i);
 95     for (int i = 1; i <= m; ++i)
 96     {
 97         a = b = c = 0;
 98         read(l), read(r);
 99         Split(rt, r, a, b);
100         Split(a, l - 1, a, c);
101         tag[c] ^= 1;
102         Merge(a, a, c);
103         Merge(rt, a, b);
104     }
105     Print(rt);
106     return 0;
107 }

猜你喜欢

转载自www.cnblogs.com/yanyiming10243247/p/10057820.html