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<iostream> #include<cstdio> #include<cstring> #include<algorithm>
const int MAXN = 8e4 + 10;
struct Node { int ls, rs, f; int val; int siz; int rnd; }p[MAXN << 5];
int n, m, tot, root; int idx[MAXN];
void _fullvis(int u) { if (p[u].ls) _fullvis(p[u].ls); printf(" node #%d: val=%d, son=#%d/#%d, f=#%d, siz=%d, rnd=%d\n", u, p[u].val, p[u].ls, p[u].rs, p[u].f, p[u].siz, p[u].rnd); if (p[u].rs) _fullvis(p[u].rs); }
void _fulldisplaytree(int u, std::string s) { printf("full display of tree '%s[%d]' {\n", s.c_str(), u); _fullvis(u); puts("}\n"); }
void _middlevis(int u) { if (p[u].ls) _middlevis(p[u].ls); printf("%d ", p[u].val); if (p[u].rs) _middlevis(p[u].rs); }
void _displaytree(int u, std::string s) { printf("display of tree '%s[%d]' { ", s.c_str(), u); _middlevis(u); printf("}\n"); }
int newnode(int val) { tot++; p[tot].val = val; p[tot].ls = p[tot].rs = 0; p[tot].siz = 1; p[tot].rnd = rand(); return tot; }
void pushup(int u) { int ls = p[u].ls, rs = p[u].rs; if (ls) p[ls].f = u; if (rs) p[rs].f = u; p[u].siz = p[ls].siz + p[rs].siz + 1; }
void sizsplit(int rt, int &a, int &b, int rank) { if (rt == 0) { a = b = 0; return; } int siz = p[p[rt].ls].siz; if (siz + 1 <= rank) { a = rt; sizsplit(p[rt].rs, p[a].rs, b, rank - siz - 1); pushup(a); } else { b = rt; sizsplit(p[rt].ls, a, p[b].ls, rank); pushup(b); } }
void merge(int a, int b, int &rt) { if (a == 0 || b == 0) { rt = a + b; return; } if (p[a].rnd <= p[b].rnd) { rt = a; merge(p[a].rs, b, p[rt].rs); pushup(rt); } else { rt = b; merge(a, p[b].ls, p[rt].ls); pushup(rt); } }
void insert(int pos, int val) { int x, y, z; sizsplit(root, x, y, pos - 1); int u = newnode(val); idx[val] = u; merge(x, u, z); merge(z, y, root); }
int getval(int rt, int pos) { int siz = p[p[rt].ls].siz; if (siz + 1 < pos) { return getval(p[rt].rs, pos - siz - 1); } else if (siz + 1 > pos){ return getval(p[rt].ls, pos); } else { return p[rt].val; } }
int getpos(int id) { int f, cnt = p[p[id].ls].siz + 1; while ((f = p[id].f) != 0) { if (id == p[f].rs) { cnt += p[p[f].ls].siz + 1; } id = f; } return cnt; }
void putup(int val) { int pos = getpos(idx[val]); int x, y, a, b; sizsplit(root, x, y, pos - 1); sizsplit(y, a, b, 1); merge(a, x, y); merge(y, b, root); }
void putdown(int val) { int pos = getpos(idx[val]); int x, y, a, b; sizsplit(root, x, y, pos - 1); sizsplit(y, a, b, 1); merge(b, a, y); merge(x, y, root); }
void move(int val, int to) { if (to == 0) return; int pos = getpos(idx[val]), u = idx[val]; int nextval = getval(root, pos + to); int v = idx[nextval]; p[u].val = nextval; p[v].val = val; idx[val] = v; idx[nextval] = u; }
int ask(int s) { return getpos(idx[s]) - 1; }
int query(int s) { return getval(root, s); }
int main() { scanf("%d%d", &n, &m); for (int i = 1, p; i <= n; i++) { scanf("%d", &p); insert(i, p); } char opt[10]; for (int i = 1, s, t; i <= m; i++ ) { scanf("%s", opt); switch (opt[0]) { case 'T': scanf("%d", &s); putup(s); break; case 'B': scanf("%d", &s); putdown(s); break; case 'I': scanf("%d%d", &s, &t); move(s, t); break; case 'A': scanf("%d", &s); printf("%d\n", ask(s)); break; case 'Q': scanf("%d", &s); printf("%d\n", query(s)); break; } } return 0; }
|