#include using namespace std; typedef long long ll; const int K = 20; const int M = (1<= 0; d--) { int maks = (1< queries; map q_to_pos; kox_set secik; kox_set pstree; int main() { int n, inserts = 0; scanf("%d", &n); for (int i = 0; i < n; ++i) { char c; scanf(" %c", &c); query q; q.type = c; if (c == 'D') { int a; scanf("%d", &a); pstree.add(a); q.info.del = a; } else { int a, b; scanf("%d%d", &a, &b); q.type = c; if (c == 'I') { q.info.insert = {a, b}; inserts++; } else { q.info.sum = {a, b}; } } queries.push_back(q); } secik.fill(1); for (int i = n-1; i >= 0; i--) { query q = queries[i]; if (q.type == 'D') { pstree.del(q.info.del); } if (q.type == 'I') { int offset = pstree.sum(0, q.info.insert.p); int kth = secik.kth_elem(q.info.insert.p + offset + 1); secik.del(kth); q_to_pos[i] = kth; cout << q.info.insert.p << " " << offset << "\n"; cout << i << ": " << q_to_pos[i] << "\n"; } } secik.fill(0); pstree.fill(0); for (int i = 0; i < n; i++) { query q = queries[i]; if (q.type == 'I') { secik.add(q_to_pos[i]); pstree.update(q_to_pos[i], q.info.insert.x); // cout << "Inserting " << q.info.insert.x << " to " << q_to_pos[i] << "\n"; } if (q.type == 'D') { int pos = secik.kth_elem(q.info.del); // cout << "kth: " << q.info.del << " " << pos << "\n"; secik.del(pos); pstree.update(pos, -pstree.tree[pos + M]); } if (q.type == 'S') { int from = secik.kth_elem(q.info.sum.from); int to = secik.kth_elem(q.info.sum.to); printf("%lld\n", pstree.sum(from, to)); } } }