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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include<bits/stdc++.h>
using namespace std;


const long long INF = 9e18;



struct item{
    item *l, *r;
    int prior, cnt;
    long long key, mx, sum;
    item(){}
    item(long long x){
        key = mx = sum = x;
        cnt = 1;
        prior = rand() ^ (rand() << 15);
        l = r = nullptr;
    }
};
typedef item *pitem;
pitem rt = nullptr;


inline int cnt(pitem v){
    return v ? v->cnt : 0;
}
inline long long sum(pitem v){
    return v ? v->sum : 0;
}
inline long long mx(pitem v){
    return v ? v->mx : -INF;
}
inline void upd_cnt(pitem v){ /// ???? &v czy v
    if(v){
        v->cnt = cnt(v->l) + 1 + cnt(v->r);
        v->sum = sum(v->l) + v->key + sum(v->r);
        v->mx = max({mx(v->l), v->key - sum(v->l), mx(v->r) - sum(v->l) - v->key});
    }
}
void Merge(pitem &v, pitem l, pitem r){
    if(!l || !r){
        v = l ? l : r;
        return;
    }
    if(l->prior > r->prior){
        Merge(l->r, l->r, r);
        v = l;
    }
    else{
        Merge(r->l, l, r->l);
        v = r;
    }
    upd_cnt(v);
}
void Split(pitem v, long long key, pitem &l, pitem &r){
    if(!v){
        l = r = nullptr;
        return;
    }
    if(key < v->key){
        Split(v->l, key, l, v->l);
        r = v;
    }
    else{
        Split(v->r, key, v->r, r);
        l = v;
    }
    upd_cnt(v);
}
void Splits(pitem v, int key, pitem &l, pitem &r, int add = 0){
    if(!v){
        l = r = nullptr;
        return;
    }
    int cur_key = add + cnt(v->l);
    if(key <= cur_key){
        Splits(v->l, key, l, v->l, add);
        r = v;
    }
    else{
        Splits(v->r, key, v->r, r, add + cnt(v->l) + 1);
        l = v;
    }
    upd_cnt(v);
}
inline void Insert(long long key){
    pitem tr;
    Split(rt, key, rt, tr);
    Merge(rt, rt, new item(key));
    Merge(rt, rt, tr);
}
void Erase(pitem &v, long long key){
    if(v->key == key){
        pitem to_del = v;
        Merge(v, v->l, v->r);
        delete to_del;
    }
    else{
        Erase(key < v->key ? v->l : v->r, key);
    }
    upd_cnt(v);
}

inline long long get_sum(int l, int r){
    if(l > r){
        return 0;
    }
    pitem tl, tr;
    Splits(rt, r, rt, tr);
    Splits(rt, l - 1, tl, rt);
    long long res = sum(rt);
    Merge(rt, tl, rt);
    Merge(rt, rt, tr);
    return res;
}

int Get_pos_max(pitem v, long long key, long long cur = 0){
    if(v == nullptr){
        return 0;
    }
    int res = 0;
    if(mx(v->l) - cur >= key){
        return Get_pos_max(v->l, key, cur);
    }
    else{
        res += cnt(v->l);
        if(v->key - sum(v->l) - cur < key){
            res += 1;
        }
        return res + Get_pos_max(v->r, key, cur + sum(v->l) + v->key);
    }
}

inline long long Max(int l, int r){
    pitem tl, tr;
    Splits(rt, r, rt, tr);
    Splits(rt, l - 1, tl, rt);
    long long res = mx(rt);
    Merge(rt, tl, rt);
    Merge(rt, rt, tr);
    return res;
}
inline int get_max(int pos, long long val){
    pitem tl;
    Splits(rt, pos - 1, tl, rt);
    int res = Get_pos_max(rt, val);
    ///cout << "WTF: " << pos << " " << val << " " << res << "\n";
    Merge(rt, tl, rt);
    return pos + res - 1;
}
int Get_pos_sum(pitem v, long long key){
    if(v == nullptr){
        return 0;
    }
    int res = 0;
    if(sum(v->l) >= key){
        return Get_pos_sum(v->l, key);
    }
    else{
        res += cnt(v->l);
        key -= sum(v->l);
        if(v->key >= key){
            return res + 1;
        }
        else{
            key -= v->key;
            return res + 1 + Get_pos_sum(v->r, key);
        }
    }
}
inline int get_pos_sum(int l, int r, long long val){
    pitem tl, tr;
    Splits(rt, r, rt, tr);
    Splits(rt, l - 1, tl, rt);
    int res = Get_pos_sum(rt, val);
    Merge(rt, tl, rt);
    Merge(rt, rt, tr);
    return l + res - 1;
}
inline int check(int l, long long s, long long k){
    if(Max(l, l) >= s){
        return -1;
    }
    int r = get_max(l, s);
    //cout << "LOL: " << l << " " << r << " " << s << " " << k << " " << "SUM: " << get_sum(l, r) << "\n";
    if(get_sum(l, r) < k - s){
        return -1;
    }
    int pos = get_pos_sum(l, r, k - s);
    //cout << "DURAK BLYA?: " << pos << "\n";
    return pos - l + 1;
}
/**
4
1 4 8 1
15
2 2
3 4
1 3 9
*/
int main(){
    srand(time(nullptr));
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++){
        long long x;
        cin >> x;
        Insert(x);
    }
    int q;
    cin >> q;
    while(q--){
        int type;
        long long s, k;
        cin >> type;
        if(type == 1){
            cin >> s >> k;
            if(s >= k){
                cout << "0\n";
                continue;
            }
            int l = 1, r = cnt(rt);
            while(r - l > 1){
                int mid = (r + l) >> 1;
                if(check(mid, s, k) == -1){
                    r = mid;
                }
                else{
                    l = mid;
                }
            }
            if(check(r, s, k) != -1){
                cout << check(r, s, k) << "\n";
            }
            else{
                cout << check(l, s, k) << "\n";
            }
        }
        else if(type == 2){
            long long w;
            cin >> w;
            Insert(w);
        }
        else{
            long long w;
            cin >> w;
            Erase(rt, w);
        }
    }

}