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
| #include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f #define LC(x) (x<<1) #define RC(x) ((x<<1)+1) #define MID(x,y) ((x+y)>>1) #define fin(name) freopen(name,"r",stdin) #define fout(name) freopen(name,"w",stdout) #define CLR(arr,val) memset(arr,val,sizeof(arr)) #define FAST_IO ios::sync_with_stdio(false);cin.tie(0); typedef pair<int, int> pii; typedef long long LL; const double PI = acos(-1.0); const int N = 80010; struct node { int ch[2], sz, f, id; } T[N]; int rt, sz, ID[N], pos[N], n, m;
void init() { rt = sz = 0; } inline void newnode(int k, int f, int id) { T[k].ch[0] = T[k].ch[1] = 0; T[k].f = f; T[k].id = id; T[k].sz = 1; } inline int getson(int x) { return T[T[x].f].ch[1] == x; } inline void pushup(int x) { T[x].sz = T[T[x].ch[0]].sz + T[T[x].ch[1]].sz + 1; } void rotate(int x, int &k) { int f = T[x].f, ff = T[f].f, l = getson(x), r = l ^ 1; if (f == k) k = x; else T[ff].ch[getson(f)] = x; T[x].f = ff; T[f].ch[l] = T[x].ch[r]; if (T[x].ch[r]) T[T[x].ch[r]].f = f; T[x].ch[r] = f; T[f].f = x; pushup(f); pushup(x); } inline void splay(int x, int &k) { while (x != k) { int f = T[x].f; if (f != k) { if (getson(x)^getson(f)) rotate(x, k); else rotate(f, k); } rotate(x, k); } } void build(int &k, int l, int r, int f) { int mid = MID(l, r); newnode(k = ++sz, f, ID[mid]); pos[T[k].id] = k; if (l < mid) build(T[k].ch[0], l, mid - 1, k); if (mid < r) build(T[k].ch[1], mid + 1, r, k); pushup(k); } int Find_kth(int x, int k) { int w = T[T[x].ch[0]].sz + 1; if (k == w) return x; if (k < w) return Find_kth(T[x].ch[0], k); return Find_kth(T[x].ch[1], k - w); } void del(int k) { int x = Find_kth(rt, k - 1), y = Find_kth(rt, k + 1); splay(x, rt); splay(y, T[rt].ch[1]); int z = T[y].ch[0]; T[y].ch[0] = 0; T[z].f = 0; pushup(y); pushup(x); } void Move(int id, int val) { int x, y, z = pos[id]; splay(z, rt); int ran = T[T[z].ch[0]].sz + 1; del(ran); if (val == INF) x = Find_kth(rt, n), y = Find_kth(rt, n + 1); else if (val == -INF) x = Find_kth(rt, 1), y = Find_kth(rt, 2); else x = Find_kth(rt, ran + val - 1), y = Find_kth(rt, ran + val); splay(x, rt); splay(y, T[rt].ch[1]); newnode(z, y, id); T[y].ch[0] = z; pushup(y); pushup(x); } int main(void) { int i, id; char ops[4]; while (~scanf("%d%d", &n, &m)) { init(); for (i = 2; i <= n + 1; ++i) scanf("%d", &ID[i]); build(rt, 1, n + 2, 0); while (m--) { scanf("%s", ops); if (ops[0] == 'T') { scanf("%d", &id); Move(id, -INF); } else if (ops[0] == 'B') { scanf("%d", &id); Move(id, INF); } else if (ops[0] == 'I') { int S, T; scanf("%d%d", &S, &T); Move(S, T); } else if (ops[0] == 'A') { scanf("%d", &id); splay(pos[id], rt); printf("%d\n", T[T[rt].ch[0]].sz - 1); } else if (ops[0] == 'Q') { int S; scanf("%d", &S); printf("%d\n", T[Find_kth(rt, S + 1)].id); } } } return 0; }
|