1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
| #include <bits/stdc++.h>
using namespace std; const int Maxn = 80000 + 100;
int N, M, cnt, root; struct node { int ch[2], size, fa, val, val2; }Tree[Maxn];
int Pos[Maxn]; inline void Insert(int x, int f) { Tree[++cnt].val = x; Pos[x] = cnt; Tree[cnt].fa = f; Tree[cnt].size = 1; return ; }
inline int judge_dir (int x) { return Tree[Tree[x].fa].ch[1] == x; }
inline void Connect(int x, int f, int dir) { Tree[x].fa = f; Tree[f].ch[dir] = x; return ; }
inline void Push_up(int x) { Tree[x].size = Tree[Tree[x].ch[0]].size + Tree[Tree[x].ch[1]].size + 1; Pos[Tree[Tree[x].ch[0]].val] = Tree[x].ch[0]; Pos[Tree[x].val] = x; Pos[Tree[Tree[x].ch[1]].val] = Tree[x].ch[1]; return ; }
inline void rotate(int x) { int dirx = judge_dir(x), f = Tree[x].fa, anc = Tree[f].fa, dirf = judge_dir(f); Connect(Tree[x].ch[dirx ^ 1], f, dirx); Connect(x, anc, dirf); Connect(f, x, dirx ^ 1); Push_up(f); Push_up(x); }
inline void Splay(int x, int y) { while (Tree[x].fa != y) { int dirx = judge_dir(x), f = Tree[x].fa, dirf = judge_dir(f); if (Tree[f].fa == y) rotate(x); else if (dirx == dirf) rotate(f), rotate(x); else rotate(x), rotate(x); } Pos[Tree[x].val] = x; if (!y) root = x; }
inline void Create(int x) { if (!root) { root = 1; Tree[root].val2 = 1; Insert(x, 0); return ; }
int now = root; while (1) { Tree[now].size ++; int dir = x > Tree[now].val2; if (!Tree[now].ch[dir]) { Insert(x, now); Tree[now].ch[dir] = cnt; Tree[now].val2 = cnt; Splay(cnt, 0); return ; } now = Tree[now].ch[dir]; } }
char s[10];
inline int Find(int x) { return Pos[x]; }
inline void Put_top(int x) { x = Find(x);
Splay(x, 0); if (!Tree[x].ch[0]) return ; if (!Tree[x].ch[1]) { Tree[x].ch[1] = Tree[x].ch[0]; Tree[x].ch[0] = 0; Push_up(x); return ; } int now = Tree[x].ch[1]; while (Tree[now].ch[0]) now = Tree[now].ch[0]; Splay(now, x); Connect(Tree[x].ch[0], now, 0); Tree[x].ch[0] = 0; Push_up(now); Push_up(x); }
inline void Put_bottom(int x) { x = Find(x);
Splay(x, 0); if (!Tree[x].ch[1]) return ; if (!Tree[x].ch[0]) { Tree[x].ch[0] = Tree[x].ch[1]; Tree[x].ch[1] = 0; Push_up(x); return ; } int now = Tree[x].ch[0]; while (Tree[now].ch[1]) now = Tree[now].ch[1]; Splay(now, x); Connect(Tree[x].ch[1], now, 1); Tree[x].ch[1] = 0; Push_up(now); Push_up(x); }
inline void Change(int x, int y) { if (!y) return ; x = Find(x); Splay(x, 0); int dir = (y == 1); if (!Tree[x].ch[dir]) return ; int now = Tree[root].ch[dir];
while (Tree[now].ch[dir ^ 1]) now = Tree[now].ch[dir ^ 1];
swap(Tree[x].val, Tree[now].val); Pos[Tree[x].val] = x; Pos[Tree[now].val] = now;
}
inline int Rank(int x) { x = Find(x); Splay(x, 0); return Tree[Tree[x].ch[0]].size; }
inline int Rerank(int x) { int now = root; while (1) { int Sum = Tree[Tree[now].ch[0]].size + 1; if (Sum == x) return Tree[now].val; if (x <= (Sum - 1)) now = Tree[now].ch[0]; else x -= (Tree[Tree[now].ch[0]].size + 1), now = Tree[now].ch[1]; } }
inline void Print(int x) { if (Tree[x].ch[0]) Print(Tree[x].ch[0]); cout<<Tree[x].val<<" "; if (Tree[x].ch[1]) Print(Tree[x].ch[1]); } int main() { #ifdef hk_cnyali freopen("A.in", "r", stdin); freopen("A.out", "w", stdout); #endif scanf("%d%d", &N, &M); int x, y; for (int i = 1; i <= N; ++i) { scanf("%d", &x); Create(x); } while (M--) { scanf("%s", s); scanf("%d", &x); if (s[0] == 'T') Put_top(x); if (s[0] == 'B') Put_bottom(x); if (s[0] == 'I') scanf("%d", &y), Change(x, y); if (s[0] == 'A') cout<<Rank(x)<<endl; if (s[0] == 'Q') cout<<Rerank(x)<<endl; } return 0; }
|