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
| # include<cstdio> # include<algorithm>
using namespace std;
const int N = 80005; int n, m, q, qt, a[N], pos[N]; char opt[10];
struct BST{ # define lson tr[x].son[0] # define rson tr[x].son[1] struct Splay{ int fa, son[2]; int size, id; }tr[N]; int root = 0, cnt = 0; inline int newNode(){ cnt++; tr[cnt].fa = tr[cnt].son[0] = tr[cnt].son[1] = 0; tr[cnt].size = 1, tr[cnt].id = 0; return cnt; } inline void pushup(int x){ tr[x].size = tr[lson].size + tr[rson].size + 1; } inline int build(int l, int r, int f){ if(l > r) return 0; int mid = (l + r) >> 1; int x = newNode(); tr[x].id = a[mid]; pos[a[mid]] = x; tr[x].fa = f; tr[x].son[0] = build(l, mid-1, x); tr[x].son[1] = build(mid+1, r, x); pushup(x); return x; } inline void rotate(int x, int kind){ int y = tr[x].fa, z = tr[y].fa, B = tr[x].son[kind]; tr[x].son[kind] = y, tr[y].son[!kind] = B, tr[z].son[tr[z].son[1] == y] = x; tr[x].fa = z, tr[y].fa = x, tr[B].fa = y; pushup(y), pushup(x); } inline void splay(int x, int goal){ if(x == goal) return; while(tr[x].fa != goal){ int y = tr[x].fa, z = tr[y].fa; int dir1 = !(tr[y].son[1] == x), dir2 = !(tr[z].son[1] == y); if(z == goal) rotate(x, dir1); else{ if(dir1 == dir2) rotate(y, dir2); else rotate(x, dir1); rotate(x, dir2); } } if(goal == 0) root = x; } inline int find(int k){ int now = root; while(k){ if(k <= tr[tr[now].son[0]].size) now = tr[now].son[0]; else if(k == tr[tr[now].son[0]].size + 1) return now; else{ k -= tr[tr[now].son[0]].size + 1; now = tr[now].son[1]; } } return now; } inline void top(int x){ x = pos[x]; splay(x, 0); if(!tr[x].son[0]) return; if(!tr[x].son[1]) tr[x].son[1] = tr[x].son[0], tr[x].son[0] = 0; else{ int p = find(tr[tr[x].son[0]].size + 2); tr[p].son[0] = tr[x].son[0], tr[tr[x].son[0]].fa = p; tr[x].son[0] = 0; splay(tr[p].son[0], 0); } } inline void bot(int x){ x = pos[x]; splay(x, 0); if(!tr[x].son[1]) return; if(!tr[x].son[0]) tr[x].son[0] = tr[x].son[1], tr[x].son[1] = 0; else{ int p = find(tr[tr[x].son[0]].size); tr[p].son[1] = tr[x].son[1], tr[tr[x].son[1]].fa = p; tr[x].son[1] = 0; splay(tr[p].son[1], 0); } } inline void insert(int x, int t){ if(!t) return; x = pos[x]; splay(x, 0); int p = find(t == -1 ? tr[tr[root].son[0]].size : tr[tr[root].son[0]].size + 2); swap(tr[root].id, tr[p].id); swap(pos[tr[root].id], pos[tr[p].id]); } inline int ask(int x){ x = pos[x]; splay(x, 0); return tr[tr[x].son[0]].size; } inline int query(int k){ return tr[find(k)].id; } inline void print(int x){ if(tr[x].son[0]) print(tr[x].son[0]); printf("%d ", tr[x].id); if(tr[x].son[1]) print(tr[x].son[1]); } }T;
int main(){ scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); T.root = T.build(1, n, 0); while(m--){ scanf("%s%d", opt, &q); switch(opt[0]){ case 'T': T.top(q); break; case 'B': T.bot(q); break; case 'I': scanf("%d", &qt); T.insert(q, qt); break; case 'A': printf("%d\n", T.ask(q)); break; case 'Q': printf("%d\n", T.query(q)); break; default: break; } } return 0; }
|