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
| #include<bits/stdc++.h> using namespace std; #ifdef LOCAL #define debug(...) fprintf(stdout, ##__VA_ARGS__)
#else #define debug(...) void(0)
#endif #define int long long
inline int read(){int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+ (x<<3)+(ch^48);ch=getchar();}return x*f;} #define Z(x) (x)*(x)
#define pb push_back
#define fi first
#define se second
#define N 100010
int n, m, i, j, k; int ans, bas, Miv; char op[2];
struct Splay { int a[N], son[N][2], fa[N], root, cnt[N], w[N], tot; int ls(int x) { return son[x][0]; } int rs(int x) { return son[x][1]; } int zh(int x) { return son[fa[x]][1] == x; } void push_up(int x) { w[x] = cnt[x] + w[son[x][0]] + w[son[x][1]]; } void print(int x) { if(!x) return ; printf("%d[%d] -> [%d %d]\n", a[x] + bas, w[x], a[son[x][0]] + bas, a[son[x][1]] + bas); print(son[x][0]); print(son[x][1]); if(x == root) printf("---------------------\n"); } void rotate(int x) { int y = fa[x], z = fa[y]; int k = zh(x), w = son[x][k ^ 1]; if(z) son[z][zh(y)] = x, fa[x] = z; else fa[x] = 0; if(w) son[y][k] = w, fa[w] = y; else son[y][k] = 0; son[x][k ^ 1] = y; fa[y] = x; push_up(y); push_up(x); } void splay(int x, int target = 0) { while(fa[x] != target) { int y = fa[x], z = fa[y]; if(z != target) { if(zh(x) == zh(y)) rotate(y); else rotate(x); } rotate(x); } if(!target) root = x; } void insert(int x) { int now = root, las = 0; while(now && a[now] != x) las = now, now = son[now][x > a[now]];
if(!now) { now = ++tot; if(las) son[las][x > a[las]] = now; fa[now] = las; son[now][0] = son[now][1] = 0; a[now] = x; } if(x != 1e12) ++cnt[now]; if(!root) root = now; splay(now); } int find1(int x, int k) { int las = 0; while(x) { if(w[son[x][0]] + cnt[x] >= k && k > w[son[x][0]]) return x; las = x; if(w[son[x][0]] >= k) x = son[x][0]; else k -= (w[son[x][0]] + cnt[x]), x = son[x][1]; } return las; }
int qry(int k) { if(k > w[root]) return -1 - bas; k = w[root] - k + 1; int u = find1(root, k); splay(u); return a[u]; } int find2(int x, int k) { int las = 0, ans = 0; while(x) { if(a[x] == k) return x; if(a[x] >= k) ans = x; las = x; if(a[x] < k) x = son[x][1]; else if(a[x] > k) x = son[x][0]; } return ans; }
int check(int miv) { int u = find2(root, miv); splay(u); int d = w[son[u][0]]; son[u][0] = 0; push_up(u); return d; } }T;
signed main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif
n = read(); Miv = read(); T.insert(1e12); for(i = 1; i <= n; ++i) { scanf("%s", op); k = read();
if(op[0] == 'I') { k -= bas; if(k < Miv) continue; T.insert(k); }
if(op[0] == 'A') bas += k, Miv -= k; if(op[0] == 'S') bas -= k, Miv += k, ans += T.check(Miv); if(op[0] == 'F') printf("%lld\n", T.qry(k) + bas); } printf("%lld\n", ans); return 0; }
|