Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- /// from HUNG MINE with love <3
- int t[1000001][3], cc = 1, fin[1000001], par[1000001], cnt[1000001], q, x;
- const int add = 1e9;
- void ins (int x) {
- int r = 1;
- for (int k = 30; k >= 0; -- k) {
- int i = ((x >> k) & 1);
- if (t[r][i] == 0) {
- ++ cc;
- t[r][i] = cc;
- par[cc] = r;
- }
- r = t[r][i];
- }
- if (fin[r] == 1) return;
- fin[r] = 1;
- ++ cnt[r];
- while (r != 1) {
- r = par[r];
- ++ cnt[r];
- }
- }
- void del (int x) {
- int r = 1;
- for (int k = 30; k >= 0; -- k) {
- int i = (x >> k) & 1;
- if (t[r][i] == 0) return;
- r = t[r][i];
- }
- if (fin[r] == 0) return;
- fin[r] = 0;
- -- cnt[r];
- while (r != 1) {
- r = par[r];
- -- cnt[r];
- }
- }
- int pth (int p) {
- int r = 1;
- if (cnt[r] < p) return -1e9;
- int x = 0;
- for (int k = 30; k >= 0; -- k) {
- int i = t[r][0];
- if (p > cnt[i]) {
- x |= (1 << k);
- p -= cnt[i];
- r = t[r][1];
- }
- else {
- r = t[r][0];
- }
- }
- return x;
- }
- int cou (int x) {
- int r = 1;
- int res = 0;
- for (int k = 30; k >= 0; -- k) {
- int i = ((x >> k) & 1);
- if (i == 1) res += cnt[t[r][0]];
- if (t[r][i] == 0) return res;
- r = t[r][i];
- }
- return res;
- }
- int main () {
- if (fopen ("test.inp", "r")) {
- freopen ("test.inp", "r", stdin);
- }
- // else {
- // freopen ("ORDSET.inp", "r", stdin);
- // freopen ("ORDSET.out", "w", stdout);
- // }
- ios_base :: sync_with_stdio (0);
- cin.tie (0);
- cout.tie (0);
- cin >> q;
- while (q --) {
- char c;
- cin >> c;
- if (c == 'I') {
- cin >> x;
- ins (x + add);
- }
- else if (c == 'D') {
- cin >> x;
- del (x + add);
- }
- else if (c == 'K') {
- cin >> x;
- if (pth (x) == -1e9) {
- cout << "invalid\n";
- }
- else {
- cout << pth (x) - add<< "\n";
- }
- }
- else {
- cin >> x;
- cout << cou (x + add) << "\n";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement