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
| #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int N = 100210; struct Node { string str; int p, sz, s[2]; } tr[N]; int n, m, q, rt, idx; void pushup(int x) { tr[x].sz = tr[tr[x].s[0]].sz + tr[tr[x].s[1]].sz + 1; } void rotate(int x) { int y = tr[x].p, z = tr[y].p; int k = tr[y].s[1] == x; tr[z].s[tr[z].s[1] == y] = x; tr[x].p = z; tr[y].s[k] = tr[x].s[k ^ 1]; tr[tr[x].s[k ^ 1]].p = y; tr[x].s[k ^ 1] = y; tr[y].p = x; pushup(y); pushup(x); } void splay(int x, int k) { while (tr[x].p != k) { int y = tr[x].p, z = tr[y].p; if (z != k) if ((tr[z].s[1] == y) ^ (tr[y].s[1] == x)) rotate(x); else rotate(y); rotate(x); } if (!k) rt = x; } int build(int l, int r) { if (l > r) return 0; int mid = l + r + 1 >> 1; int t = build(l, mid - 1); tr[mid].s[0] = t; tr[t].p = mid; t = build(mid + 1, r); tr[mid].s[1] = t; tr[t].p = mid; pushup(mid); return mid; } int get(int x) { int u = rt; while (u) { if (tr[tr[u].s[0]].sz + 1 == x) return u; else if (tr[tr[u].s[0]].sz + 1 > x) u = tr[u].s[0]; else { x -= tr[tr[u].s[0]].sz + 1; u = tr[u].s[1]; } } } int main() { scanf("%d", &n); for (int i = 2; i <= n + 1; i++) cin >> tr[i].str; rt = build(1, n + 2); idx = n + 2; scanf("%d", &m); while (m--) { string str; int pos; cin >> str; scanf("%d", &pos); int r = get(pos + 2); splay(r, 0); int l = tr[rt].s[0]; while (tr[l].s[1]) l = tr[l].s[1]; splay(l, r); tr[l].s[1] = ++idx; tr[idx].p = l; tr[idx].str = str; splay(idx, 0); } scanf("%d", &q); for (int pos; q; q--) { scanf("%d", &pos); cout << tr[get(pos + 2)].str << endl; } return 0; }
|