Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define NguyenDangQuan the_author
- #define my_heart love_you_to_the_moon_and_back
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- using ld = long double;
- using ull = unsigned long long;
- bool typetest;
- inline void fastIOfileinput(){
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- typetest = 0;
- }
- /// I HATE MY BRAIN
- const int N = 2e5 + 1;
- int n, q;
- int have(0);
- struct Trie{
- Trie *child[2];
- int v = 0;
- Trie() {child[0] = child[1] = NULL;}
- void upd(int id, int lim, int &x, int val){
- if(id == lim){
- v += val;
- return;
- }
- if(!child[(x >> id) & 1])
- child[(x >> id) & 1] = new Trie;
- child[(x >> id) & 1] -> upd(id - 1, lim, x, val);
- if(child[(x >> id) & 1] -> v == 0)
- delete child[(x >> id) & 1],
- child[(x >> id) & 1] = NULL;
- v += val;
- }
- int found(int id, int lim, int &k, int now){
- if(id == lim)
- return now;
- if(child[0] && child[0] -> v + have >= k)
- return child[0] -> found(id - 1, lim, k, now);
- if(child[0])
- have += child[0] ->v;
- now |= 1 << id;
- return child[1] -> found(id - 1, lim, k, now);
- }
- };
- Trie beg;
- void Read(){
- cin >> n;
- for(int i = 1; i <= n; ++i){
- int v;
- cin >> v;
- beg.upd(30, -1, v, 1);
- }
- }
- void Solve(){
- cin >> q;
- while(q--){
- int type;
- cin >> type;
- if(type == 1){
- int k;
- cin >> k;
- if(k > n){
- cout << -1 << "\n";
- continue;
- }
- have = 0;
- cout << beg.found(30, -1, k, 0) << "\n";
- }
- else if(type == 2){
- int k;
- cin >> k;
- if(k > n)
- continue;
- have = 0;
- int v = beg.found(30, -1, k, 0);
- beg.upd(30, -1, v, -1);
- --n;
- }
- else{
- int k;
- cin >> k;
- beg.upd(30, -1, k, 1);
- ++n;
- }
- }
- }
- main(){
- fastIOfileinput();
- if(typetest){
- int t;
- cin >> t;
- while(t--){
- Read();
- Solve();
- }
- }
- else{
- Read();
- Solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment