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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <bits/stdc++.h>
using namespace std;
#define int long long

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#pragma once

struct Node {
    Node *l = 0, *r = 0;
    int val, y, c = 1, m, s;
    Node(int val) : val(val), y(rand()), m(val), s(val) {}
    void recalc();
};

int cnt(Node* n) { return n ? n->c : 0; }
int suma(Node* n) { return n ? n->s : 0; }
int maks(Node* n) { return n ? n->m : 0; }
void Node::recalc() { 
    c = cnt(l) + cnt(r) + 1; 
    s = suma(l) + suma(r) + val;
    m = max(maks(l), maks(r));
    m = max(m, val);
}
void erase(Node *n){
    if(!n)return;
    erase(n->l);
    erase(n->r);
    delete n;
}

template<class F> void each(Node* n, F f) {
    if (n) { each(n->l, f); f(n->val); each(n->r, f); }
}
//wycina pierwsze k elementow
pair<Node*, Node*> split(Node* n, int k) {
    if (!n) return {};
    if (cnt(n->l) >= k) { // "n->val >= k" for lower_bound(k)
        auto pa = split(n->l, k);
        n->l = pa.second;
        n->recalc();
        return {pa.first, n};
    } else {
        auto pa = split(n->r, k - cnt(n->l) - 1); // and just "k"
        n->r = pa.first;
        n->recalc();
        return {n, pa.second};
    }
}
//pierwszy o wartosci >= k jest po prawej
pair<Node*, Node*> lb(Node* n, int k) {
    if (!n) return {};
    if (n -> val >= k) { // "n->val >= k" for lower_bound(k)
        auto pa = lb(n->l, k);
        n->l = pa.second;
        n->recalc();
        return {pa.first, n};
    } else {
        auto pa = lb(n->r, k); // and just "k"
        n->r = pa.first;
        n->recalc();
        return {n, pa.second};
    }
}
//suma lewego <= k
pair<Node*, Node*> split_suma(Node* n, int k) {
    if (!n) return {};
    if (suma(n->l) + n->val > k) { // "n->val >= k" for lower_bound(k)
        auto pa = split_suma(n->l, k);
        n->l = pa.second;
        n->recalc();
        return {pa.first, n};
    } else {
        auto pa = split_suma(n->r, k - suma(n->l) - n->val); // and just "k"
        n->r = pa.first;
        n->recalc();
        return {n, pa.second};
    }
}


Node* merge(Node* l, Node* r) {
    if (!l) return r;
    if (!r) return l;
    if (l->y > r->y) {
        l->r = merge(l->r, r);
        l->recalc();
        return l;
    } else {
        r->l = merge(l, r->l);
        r->recalc();
        return r;
    }
}

Node* ins(Node* t, Node* n, int pos) {
    auto pa = split(t, pos);
    return merge(merge(pa.first, n), pa.second);
}

// Example application: move the range [l, r) to index k
void move(Node*& t, int l, int r, int k) {
    Node *a, *b, *c;
    tie(a,b) = split(t, l); tie(b,c) = split(b, r - l);
    if (k <= l) t = merge(ins(a, b, k), c);
    else t = merge(a, ins(c, b, k - r));
}
//)
main() {
    cin.sync_with_stdio(0); cin.tie(0);
    cin.exceptions(cin.failbit);
    int n;
    cin >> n;
    multiset <int> S;
    vector <int> w(n);
    for(int i = 0; i < n; ++i){
        cin >> w[i];
        S.insert(w[i]);
    }
    sort(w.begin(), w.end());
    Node* root = new Node(w[0]);
    for(int i = 1; i < n; ++i){
        Node* asd = new Node(w[i]);
        root = merge(root, asd);
    }
    int q;
    cin >> q;
    for(int i = 0; i < q; ++i){
        int a, b;
        cin >> a >> b;
        if(a == 1){
            int c; 
            cin >> c;
            if(c <= b){
                cout << "0\n";
                continue;
            }
            vector <int> A;
            vector <Node *> B;
            Node *kar = root;
            S.insert(c-1);
            int odp = 0;
            bool ok = 1;
            while(b < c){
                int nv = *S.lower_bound(b);
                ++nv;
                Node *d, *f;
                tie(d,f) = lb(kar, b);
                if(nv - b > suma(d)){
                    kar = merge(d,f);
                    ok = 0;
                    break;
                }
                int s = b+suma(d)-nv;
                Node *e;
                tie(d,e) = split_suma(d,s);
                A.push_back(s);
                B.push_back(e);
                odp += cnt(e);
                b += suma(e);
                kar = merge(d,f);
            }
            while(A.size()){
                int s = A.back();
                Node *e = B.back(), *d, *f;
                tie(d,f) = split_suma(kar, s);
                d = merge(d,e);
                d = merge(d,f);
                kar = d;
                A.pop_back();
                B.pop_back();
            }
            root = kar;
            S.erase(S.find(c-1));
            if(ok){
                cout << odp << '\n';
            }
            else {
                cout <<"-1\n";
            }
        }
        else if(a == 2){
            Node *d, *e;
            tie(d,e) = lb(root,b);
            Node *f = new Node(b);
            d = merge(d,f);
            root = merge(d,e);
            S.insert(b);
        }
        else {
            S.erase(S.find(b));
            Node *d, *e, *f;
            tie(d,e) = lb(root,b);
            tie(e,f) = split(e,1);
            delete e;
            root = merge(d,f);
        }
    }
    erase(root);
}