intquery(int q, int p, int l, int r, int k) { if (l == r) return r; // 叶子结点直接返回 int cnt = tr[tr[q].l].cnt - tr[tr[p].l].cnt; int mid = l + r >> 1; // k小于cnt说明要找的点在左半边,否则在右半边 if (k <= cnt) returnquery(tr[q].l, tr[p].l, l, mid, k); elsereturnquery(tr[q].r, tr[p].r, mid + 1, r, k - cnt); }
Jamie is getting very busy with his school life. He starts to forget the assignments that he has to do. He decided to write the things down on a to-do list. He assigns a value priority for each of his assignment (lower value means more important) so he can decide which he needs to spend more time on.
After a few days, Jamie finds out the list is too large that he can’t even manage the list by himself! As you are a good friend of Jamie, help him write a program to support the following operations on the to-do list:
seta**ix**i — Add assignment a**i to the to-do list if it is not present, and set its priority to x**i. If assignment a**i is already in the to-do list, its priority is changed to x**i.
removea**i — Remove assignment a**i from the to-do list if it is present in it.
querya**i — Output the number of assignments that are more important (have a smaller priority value) than assignment a**i, so Jamie can decide a better schedule. Output - 1 if a**i is not in the to-do list.
undod**i — Undo all changes that have been made in the previous d**i days (not including the day of this operation)
At day 0, the to-do list is empty. In each of the following q days, Jamie will do exactly one out of the four operations. If the operation is a query, you should output the result of the query before proceeding to the next day, or poor Jamie cannot make appropriate decisions.
Input
The first line consists of a single integer $q (1 ≤ q ≤ 10^5)$ — the number of operations.
The following q lines consists of the description of the operations. The i-th line consists of the operation that Jamie has done in the i-th day. The query has the following format:
The first word in the line indicates the type of operation. It must be one of the following four: set, remove, query, undo.
If it is a set operation, a string a**i and an integer $x_i$ follows $(1 ≤ x_i ≤ 109)$. a**i is the assignment that need to be set to priority $x_i$.
If it is a remove operation, a string $a_i$ follows. $a_i$ is the assignment that need to be removed.
If it is a query operation, a string $a_i$ follows. $a_i$ is the assignment that needs to be queried.
If it is a undo operation, an integer $d_i$ follows $(0 ≤ d_i < i)$. $d_i$ is the number of days that changes needed to be undone.
All assignment names $a_i$ only consists of lowercase English letters and have a length $1 ≤ |a_i| ≤ 15$.
It is guaranteed that the last operation is a query operation.
Output
For each query operation, output a single integer — the number of assignments that have a priority lower than assignment $a_i$, or $- 1$ if $a_i$ is not in the to-do list.
Interaction
If the operation is a query, you should output the result of the query and flush the output stream before proceeding to the next operation. Otherwise, you may get the verdict Idleness Limit Exceed.
For flushing the output stream, please refer to the documentation of your chosen programming language. The flush functions of some common programming languages are listed below:
C: fflush(stdout);
C++: cout « flush;
Java: System.out.flush();
Examples
input
8 set chemlabreport 1 set physicsexercise 2 set chinesemockexam 3 query physicsexercise query chinesemockexam remove physicsexercise query physicsexercise query chinesemockexam
output
1 2 -1 1
code
一棵线段树无法维护时可以同时维护两个线段树
#include<bits/stdc++.h>
usingnamespace std;
constint N = 11; constint INF = 1e9 + 10;
int n, tot; map<int, int> nums; // 离散化 map<string, int> m;
structOperation { string op; string test; int x; };
structNode { int l, r; int cnt; }tr[N * 50];
int root1[N], root2[N], idx;
intinsert(int p, int l, int r, int pos, int op, string s) { int q = ++ idx; tr[q] = tr[p]; if (l == r) { tr[q].cnt += op; return q; } int mid = l + r >> 1; if (pos <= mid) tr[q].l = insert(tr[q].l, l, mid, pos, op, s); else tr[q].r = insert(tr[q].r, mid + 1, r, pos, op, s); if (tr[q].l == tr[q].r) tr[q].cnt = tr[tr[q].l].cnt; else tr[q].cnt = tr[tr[q].l].cnt + tr[tr[q].r].cnt; return q; }
intquery(int q, int l, int r, int nl, int nr) { if (r == -1) return0; if (nl >= l && nr <= r) return tr[q].cnt; int mid = nl + nr >> 1; int res = 0; if (l <= mid) res += query(tr[q].l, l, r, nl, mid); if (r > mid) res += query(tr[q].r, l, r, mid + 1, nr); return res; }
for (int i = 1; i <= n; i ++ ) { cin >> ope[i].op; string op = ope[i].op; if (op == "set") { cin >> ope[i].test >> ope[i].x; nums[ope[i].x] = tot; string s = ope[i].test; int x = ope[i].x;