【题目链接】
同【BZOJ1862】
/* Footprints In The Blood Soaked Snow */ #include <cstdio> #include <cstring> #include <map> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const int maxn = 1000005; const LL inf = 1LL << 60; int son[maxn][2], pre[maxn], size[maxn]; string id[maxn]; LL val[maxn]; int tot1, tot2, sta[maxn], root; map<string, int> no; inline int iread() { int f = 1, x = 0; char ch = getchar(); for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1; for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; return f * x; } inline void newnode(int &x, int f, LL c, string &s) { x = tot2 ? sta[tot2--] : ++tot1; son[x][0] = son[x][1] = 0; pre[x] = f; size[x] = 1; val[x] = c; id[x] = s; no[s] = x; } inline void pushup(int x) { int l = son[x][0], r = son[x][1]; size[x] = size[l] + size[r] + 1; } inline void init() { tot1 = tot2 = root = 0; son[0][0] = son[0][1] = pre[0] = size[0] = val[0] = 0; string s; newnode(root, 0, -inf, s); newnode(son[root][1], root, inf, s); pushup(son[root][1]); pushup(root); } inline void rotate(int x) { int y = pre[x], z = pre[y], type = son[y][1] == x; pre[son[y][type] = son[x][!type]] = y; pre[x] = z; if(z) son[z][son[z][1] == y] = x; pre[son[x][!type] = y] = x; pushup(y); pushup(x); } inline void splay(int x, int goal) { while(pre[x] ^ goal) { int y = pre[x], z = pre[y]; if(z == goal) rotate(x); else if(son[z][1] == y ^ son[y][1] == x) rotate(x), rotate(x); else rotate(y), rotate(x); } if(!goal) root = x; } inline int getpre(int x) { for(; son[x][1]; x = son[x][1]); return x; } inline int getsuf(int x) { for(; son[x][0]; x = son[x][0]); return x; } inline int find(int k) { int x = root; while(x) { if(k == size[son[x][0]]) return x; else if(k < size[son[x][0]]) x = son[x][0]; else k -= size[son[x][0]] + 1, x = son[x][1]; } } inline void del(int x) { splay(x, 0); int a = getpre(son[x][0]), b = getsuf(son[x][1]); splay(a, 0); splay(b, a); sta[++tot2] = son[b][0]; pre[son[b][0]] = son[b][0] = 0; pushup(b); pushup(a); } inline void insert(string &s, LL c) { int x = root; for(; son[x][c > val[x]]; x = son[x][c > val[x]]); newnode(son[x][c > val[x]], x, c, s); splay(son[x][c > val[x]], 0); } inline int query(string &s) { int t = no[s]; splay(t, 0); return size[son[t][1]]; } int cnt; inline void dfs(int x) { if(!x) return; dfs(son[x][1]); cnt--; cout << id[x]; if(cnt) cout << " "; dfs(son[x][0]); } inline void print(int st) { st = size[root] - 2 - st + 1; int ed = max(st - 9, 1); swap(st, ed); int x = find(st - 1), y = find(ed + 1); splay(x, 0); splay(y, x); cnt = ed - st + 1; dfs(son[y][0]); printf("\n"); } int main() { init(); for(int T = iread(); T; T--) { char opt[20]; scanf("%s", opt); if(opt[0] == '+') { LL c; scanf("%lld", &c); string s = opt + 1; int t = no[s]; if(t) del(t); insert(s, c); } else if(opt[0] == '?' && opt[1] >= 'A' && opt[1] <= 'Z') { string s = opt + 1; printf("%d\n", query(s)); } else if(opt[0] == '?' && opt[1] >= '0' && opt[1] <= '9') { int st; sscanf(opt + 1, "%d", &st); print(st); } } return 0; }