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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include <bits/stdc++.h>
using namespace std;
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define VI vector<int>
#define VPII vector<PII>
#define LL long long
#define LD long double
#define f first
#define s second
#define MP make_pair
#define PB push_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
#define SIZ(c) (int)(c).size()
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i)
#define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i)
#define sim template<class n
sim, class s> ostream & operator << (ostream &p, pair<n, s> x)
{return p << "<" << x.f << ", " << x.s << ">";}
sim> auto operator << (ostream &p, n y) ->
typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type 
{int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";}
void dor() {cerr << endl;}
sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);}
sim, class s> void mini(n &p, s y) {if(p>y) p = y;}
sim, class s> void maxi(n &p, s y) {if(p<y) p = y;}
#ifdef DEB
#define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__)
#else
#define debug(...)
#endif 
#define PLL pair<LL, LL>

#define I(x) #x " =", (x), " "
#define A(a, i) #a "[" #i " =", i, "] =", a[i], " "

// ******************************************************************************

const int MXN = 4e5+5;

int type[MXN];
PLL que[MXN];

LL value[MXN];
map<LL, VI> where; // do usuwania, gdzie są już dodane wartości

const int BASE = 1 << 19;
LL mx[BASE*2];
LL mi[BASE*2];
LL sum[BASE*2];
int cnt[BASE*2];

bool is_on[BASE*2];

void insert(int i, LL val)
  {
  i += BASE;
  mx[i] = val;
  mi[i] = val == 0 ? 1e18 : val;
  sum[i] = val;
  cnt[i] = val != 0;
  i /= 2;
  while(i)
    {
    mx[i] = max(mx[i*2], mx[i*2+1]);
    mi[i] = min(mi[i*2], mi[i*2+1]);
    sum[i] = sum[i*2] + sum[i*2+1];
    cnt[i] = cnt[i*2] + cnt[i*2+1];

    i /= 2;
    }
  }

void turn_on(int i)
  {
  insert(i, value[i]);
  }
void turn_off(int i)
  {
  insert(i, 0);
  }

LL mi_val(int i)
  {
  return is_on[i] ? mi[i] : 1e18;
  }

int first_bigger(LL val, int p = 0, int k = BASE-1, int id = 1)
  {
  if(p == k)return p;

  int mid = (p+k)/2;
  if(mx[id * 2] * is_on[id * 2] > val)return first_bigger(val, p, mid, id*2);
  return first_bigger(val, mid+1, k, id*2+1);
  }

int last_less_eq(LL val, int p = 0, int k = BASE-1, int id = 1)
  {
  if(p == k)return p;

  int mid = (p+k)/2;
  if(mi_val(id * 2 + 1) <= val)return last_less_eq(val, mid+1, k, id*2+1);
  return last_less_eq(val, p, mid, id*2);
  }

LL left_sum(int k) // suma wszystkich do k włącznie
  {
  k += BASE + 1;
  
  LL res = 0;
  while(k)
    {
    if(k % 2 == 1)res += sum[k-1] * is_on[k-1];
    k /= 2;
    }
  return res;
  }

VI to_process;
VI zgaszone;
VI do_poprawienia;


void add_zgas(int i) // nigdy nie będę pytał o to co jest pod zgaszonym gdy jest zgaszony
  {
  to_process.PB(i);
  }

void popraw(int i)
  {
  i /= 2;
  while(i)
    {
    mx[i] = max(is_on[i*2] * mx[i*2], is_on[i*2+1] * mx[i*2+1]);
    mi[i] = min(mi_val(i*2), mi_val(i*2+1));
    sum[i] = is_on[i*2] * sum[i*2] + is_on[i*2+1] * sum[i*2+1];
    cnt[i] = is_on[i*2] * cnt[i*2] + is_on[i*2+1] * cnt[i*2+1];
    i /= 2;
    }
  }

void do_work(int a, int b)
  {
  for(auto i : to_process)
    {
    is_on[i] = 0;
    zgaszone.PB(i);
    }
  popraw(a); popraw(b);
  do_poprawienia.PB(a);
  do_poprawienia.PB(b);
  
  to_process = {};
  }

void rollback()
  {
  for(auto i : zgaszone)
    is_on[i] = 1;

  for(auto i: do_poprawienia)
    {
    popraw(i);
    }

  do_poprawienia.clear();
  zgaszone.clear();
  }



PLL get_segment(int k, LL val) // znajdź maksymalne a t. że suma([a, b]) >= val
  {                            // zwraca (suma([a, b]), b-a+1)
  if(left_sum(k) < val)
    {
    return MP(-1, -1);
    }
  int i = k + BASE + 1;
  LL mam = 0;
  int ile = 0;
  while(1)
    {
    if(i % 2 == 1)
      {
      mam += sum[i-1] * is_on[i-1];
      ile += cnt[i-1] * is_on[i-1];

      if(mam >= val)
        {
        i--;
        break;
        }
      add_zgas(i-1);
      }
    i /= 2;
    }

  while(i < BASE)
    {
    if(mam - sum[i*2] * is_on[i*2] >= val)
      {
      mam -= sum[i*2] * is_on[i*2];
      ile -= cnt[i*2] * is_on[i*2];
      i = i * 2 + 1;
      }
    else 
      {
      add_zgas(i*2+1);
      i *= 2;
      }
    }
  add_zgas(i);
  do_work(i, k+BASE+1);

  return MP(mam, ile);
  }


int main()
  {
  int n;
  scanf("%d", &n);

  REP(i, BASE*2)
    {
    mi[i] = 1e18;
    is_on[i] = 1;
    }

  FOR(i, 1, n)
    {
    scanf("%lld", &que[i].f);
    type[i] = 2;
    }
  int q;
  scanf("%d", &q);
  FOR(i, n+1, n+q)
    {
    scanf("%d", &type[i]);
    if(type[i] == 1)scanf("%lld%lld", &que[i].f, &que[i].s);
    else scanf("%lld", &que[i].f);
    }

  vector<PLL> adds;
  type[0] = 2; que[0].f = 1.1e18;
  q = q+n; // traktujemy wszystko jako zapytania
  FOR(i, 0, q)
    if(type[i] == 2)
      adds.PB(MP(que[i].f, i));

  sort(ALL(adds));
  REP(i, adds.size())
    {
    value[i+1] = adds[i].f;
    que[adds[i].s].f = i+1;
    }

  FOR(i, 0, q)
    {
    if(type[i] == 2)
      {
      int idx = que[i].f;
      where[value[idx]].PB(idx);
      
      turn_on(idx);
      }
    if(type[i] == 3)
      {
      LL val = que[i].f;
//      assert(where[val].size());

      int idx = where[val].back(); where[val].pop_back();
      turn_off(idx);
      }
    if(type[i] == 1)
      {
      LL start = que[i].f;
      LL end = que[i].s;
      int res = 0;
      if(start >= end){puts("0"); continue;}

      while(1)
        {
        if(mi[1] >= start){puts("-1"); break;}

        int next_pos = first_bigger(start-1);
        LL next_value = min(value[next_pos]+1, end);

        int last_add_pos = last_less_eq(start-1);

        auto to_add = get_segment(last_add_pos, next_value - start);

        if(to_add.f == -1)
          {
          puts("-1");
          break;
          }

        start += to_add.f;
        res += to_add.s;
        
        if(start >= end)
          {
          printf("%d\n", res);
          break;
          }
        }
      rollback();
      }

    }
  }