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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define int long long
#define st first
#define nd second
#define rd third
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define __builtin_ctz __builtin_ctzll
#define __builtin_clz __builtin_clzll
#define __builtin_popcount __builtin_popcountll
using namespace std;
template<class T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class A, class B, class C> struct Triple { A first; B second; C third;
  bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };
  template<class T> void ResizeVec(T&, vector<int>) {}
  template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
    vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
    for (T& v : vec) { ResizeVec(v, sz); }
  }
  typedef Triple<int, int, int> TIII;
  template<class A, class B, class C>
  ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; }
  template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
  template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
  template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
  
template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
  while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif
typedef long double LD;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<pair<int, int> > VPII;

template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rng(57);
#define rand fgfdgfdgfd

struct Query {
  int drzewo, days, upd_cnt, ind;
};
struct Update {
  int days, val, ind;
};

struct Intv {
  int cnt_rows;
  int wei;
};

const int B = 128;
const int N = B * B * B;
int32_t maska = (1 << 30) - (1 << 7);
// TODO rozwazyc ustawienie B na 128 aby robic operacje bitowe zamiast modulo i dzielenia
int slow_prefs, slow_adds, fast_prefs, fast_adds;
int resets_len;
struct Tree100FastRead {
  int lvl[3][N];
  Tree100FastRead() {
    REP (L, 3) {
      REP (i, N) {
        lvl[L][i] = 0;
      }
    }
  }
  int ReadPref(int c) {
    //fast_prefs++;
    c++;
    return lvl[2][c] + lvl[1][c >> 7] + lvl[0][c >> 14];
  }
  void AddPt(int c, int val) {
    //slow_adds++;
    for (int L = 2; L >= 0; L--) {
      int mic = c + 1;
      int mac = ((c >> 7) + 1) << 7;
      //int mac = (c & maska) + B;
      for (int d = mic; d < mac; d++) {
        lvl[L][d] += val;
      }
      c >>= 7;
    }
  }
  // int brut[N];
  // int ReadPrefBrut(int c) {
  //   int res = 0;
  //   REP (i, c + 1) {
  //     res += brut[i];
  //   }
  //   return res;
  // }
  // void AddPtBrut(int c, int val) {
  //   brut[c] += val;
  // }
  // void Init(VI init) {
  //   REP (L, 3) {
  //     REP (i, SZ(init) + 5) {
  //       lvl[L][i] = 0;
  //     }
  //   }
  //   REP (i, SZ(init)) {
  //     int tmp = i;
  //     for (int L = 2; L >= 0; L--) {
  //       if (tmp % B != B - 1) {
  //         lvl[L][tmp + 1] += init[i];
  //       }
  //       tmp /= B;
  //     }
  //   }
  //   REP (L, 3) {
  //     for (int i = 0; i < SZ(init) + 2; i++) {
  //       if (i % B) {
  //         lvl[L][i] += lvl[L][i - 1];
  //       }
  //     }
  //   }
  // }
  // void InitBrut(VI init) {
  //   REP (i, SZ(init) + 5) {
  //     brut[i] = 0;
  //   }
  //   REP (i, SZ(init)) {
  //     brut[i] = init[i];
  //   }
  // }
};

struct Tree100FastAdd {
  int lvl[3][N];
  Tree100FastAdd() {
    REP (L, 3) {
      REP (i, N) {
        lvl[L][i] = 0;
      }
    }
  }
  int ReadPref(int c) {
    //slow_prefs++;
    c++;
    int res = 0;
    for (int L = 2; L >= 0; L--) {
      for (int d = c / B * B; d < c; d++) {
        res += lvl[L][d];
      }
      c /= B;
    }
    return res;
  }
  void AddPt(int c, int val) {
    //fast_adds++;
    lvl[2][c] += val;
    lvl[1][c >> 7] += val;
    lvl[0][c >> 14] += val;
  }
  void Reset(int n) {
    resets_len += n;
    REP (L, 3) {
      REP (i, n + 3) {
        lvl[L][i] = 0;
      }
    }
  }
};

//#define TEST
struct Sol {
  void Test() {
    int n, m, z;
    #ifdef TEST
    n = m = z = pow(1e16, 1./3);
    n = 1e6;
    m = 1e4;
    z = 1e6;
    int R = 1e6;
    #else
    cin>>n>>m>>z;
    #endif
    debug(n, m, z);
    int c = round(sqrt(n * m / z)) + 1;
    c *= 3;
    // paczkujemy po c
    VPII evs;
    VI a(n + 2);
    RE (i, n) {
      #ifdef TEST
      a[i] = rng() % n + 1;
      #else
      cin>>a[i];
      #endif
      evs.PB({a[i], i});
    }
    VI ile_w_dniu(n + 2);
    vector<Update> upds_by_ind(m + 2);
    vector<Update> upds_by_time;
    vector<Query> all_qs(z + 2);
    int cnt_upd = 0;
    int cnt_qs = 0;
    RE (i, m + z) {
      int typ;
      #ifdef TEST
      if (cnt_upd == m) {
        typ = 2;
      } else if (cnt_qs == z) {
        typ = 1;
      } else {
        typ = 1 + rng() % 2;
      }
      #else
      cin>>typ;
      #endif
      //debug(typ);
      if (typ == 1) {
        cnt_upd++;
        int p, w;
        #ifdef TEST
        p = rng() % n + 1;
        w = rng() % R + 1;
        #else
        cin>>p>>w;
        #endif
        upds_by_ind[cnt_upd] = {p, w, cnt_upd};
      } else {
        cnt_qs++;
        int p, d;
        #ifdef TEST
        p = rng() % n + 1;
        d = rng() % n + 1;
        #else
        cin>>p>>d;
        #endif
        all_qs[cnt_qs] = {d, p, cnt_upd, cnt_qs};
        evs.PB({d, -cnt_qs});
      }
    }
    // int n, m, z;
    // cin>>n>>m>>z;
    // int c = round(sqrt(n * m / z)) + 1;
    // debug(c);
    // VPII evs;
    // VI a(n + 2);
    // RE (i, n) {
    //   cin>>a[i];
    //   evs.PB({a[i], i});
    // }
    // int cnt_upd = 0;
    // int cnt_qs = 0;
    // vector<Update> upds_by_ind(m + 2);
    // //vector<Update> upds_by_time;
    // vector<Query> all_qs(z + 2);
    // RE (i, m + z) {
    //   int typ;
    //   cin>>typ;
    //   if (typ == 1) {
    //     cnt_upd++;
    //     int p, w;
    //     cin>>p>>w;
    //     upds_by_ind[cnt_upd] = {p, w, cnt_upd};
    //     //upds_by_time.PB(upds_by_ind[cnt_upd]);
    //   } else {
    //     cnt_qs++;
    //     int p, d;
    //     cin>>p>>d;
    //     all_qs[cnt_qs] = {d, p, cnt_upd, cnt_qs};
    //     evs.PB({d, -cnt_qs});
    //   }
    // }
    sort(ALL(evs));
    //sort(ALL(upds_by_time), [&](Update& u1, Update& u2) { return u1.ind < u2.ind; });
    int col_ind = 1;
    VI wh_a(n + 3);
    for (auto& q : evs) {
      if (q.nd > 0) {
        a[q.nd] = col_ind;
        wh_a[col_ind] = q.nd;
        col_ind++;
      } else {
        all_qs[-q.nd].drzewo = col_ind;
      }
    }
    VI ans(z + 2);
    VI zbieraj_do_rozsmarowania(n + 2);
    VI rozsmarowane(n + 2);
    int nxt_upd = 1;
    //vector<vector<Query>> paczki_queriesow(m / c + 3);
    
    Tree100FastAdd fast_add;
    int nxt_q = 1;
// #ifdef LOCAL
//     c = 2;
// #endif
    vector<PII> nieprzetworzone_updaty(z + 2);
    int cnt_paczki = 0;
    int suma_paczka_sz = 0;
    while (nxt_upd <= m) {
      cnt_paczki++;
      int fir_upd = nxt_upd;
      int last_upd = min(m, nxt_upd + c - 1);
      //debug(fir_upd, last_upd);
      vector<Query> paczka;
      while (nxt_q <= z && all_qs[nxt_q].upd_cnt <= last_upd) {
        paczka.PB(all_qs[nxt_q]);
        nxt_q++;
      }
      sort(ALL(paczka), [&](Query q1, Query q2) { return q1.drzewo > q2.drzewo; });
      //debug(SZ(paczka));
      //suma_paczka_sz += 
      int nxt_drzewo = n;
      for (auto q : paczka) {
        while (nxt_drzewo >= q.drzewo) {
          //debug(wh_a[nxt_drzewo], rozsmarowane[wh_a[nxt_drzewo]]);
          fast_add.AddPt(wh_a[nxt_drzewo], rozsmarowane[wh_a[nxt_drzewo]]);
          nxt_drzewo--;
        }
        int f = fast_add.ReadPref(q.days);
        ans[q.ind] += f;
        //debug(q.ind, q.days, f);
        //assert(q.upd_cnt >= fir_upd || q.upd_cnt == 0);
        nieprzetworzone_updaty[q.ind] = {fir_upd, q.upd_cnt};
        // FOR (ii, fir_upd, q.upd_cnt) {
          
      }
      
      FOR (ii, fir_upd, last_upd) {
        Update& u = upds_by_ind[ii];
        zbieraj_do_rozsmarowania[u.days] += u.val;
      }
      // RE (i, n) {
      //   cerr<<zbieraj_do_rozsmarowania[i]<<" ";
      // }
      // cerr<<endl;
      FORD (i, n, 1) {
        rozsmarowane[i] = rozsmarowane[i + 1] + zbieraj_do_rozsmarowania[i];
      }
      // RE (i, n) {
      //   cerr<<rozsmarowane[i]<<" ";
      // }
      // cerr<<endl;
      nxt_upd = last_upd + 1;
      //debug("RESET");
      fast_add.Reset(n);
    }
    debug(cnt_paczki * n);
    Tree100FastRead fast_read;
    vector<VI> by_drzewo(n + 2);
    RE (i, z) {
      by_drzewo[all_qs[i].drzewo].PB(i);
    }
    FORD (drzewo, n, 1) {
      fast_read.AddPt(wh_a[drzewo], 1);
      for (auto i : by_drzewo[drzewo]) {
        int moje_niezera = fast_read.ReadPref(all_qs[i].days);
        int bon = 0;
        FOR (u_ind, nieprzetworzone_updaty[i].st, nieprzetworzone_updaty[i].nd) {
          int jego_niezera = fast_read.ReadPref(upds_by_ind[u_ind].days);
          bon += min(moje_niezera, jego_niezera) * upds_by_ind[u_ind].val;
        }
        ans[i] += bon;
        //debug(i, bon);
      }
    }
    RE (i, z) {
      cout<<ans[i]<<"\n";
    }
  }
};

// O(sqrt(nmz) + z sqrt(n))
// m/c paczek po c updatow
// kolumna++ to znika nam jeden wiersz, czyli roznica symetryczna to jedna komorka -> O(1) w sqrt decomp
// restricted czas++ to uwzglednienie jednego updata dla jednego _zapytania_ -> EZ O(1)
// paczka c updatow w gore to O(n) - sumy prefiksowe
// odczytanie 

int32_t main() {

  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  cerr << fixed << setprecision(10);
  cin.tie(0);
  //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;
  
  
  // Tree100FastAdd fast_add;
  // Tree100FastRead fast_read;
  // int R = 1e5 - 2;
  // RE (i, 100000) {
  //   int tysiunc = 1000;
  //   int rr = rng() % tysiunc;
  //   if (rr == 0) {
  //     // debug("IIINIIIIITTT");
  //     // VI init;
  //     // REP (i, (int)1e5) {
  //     //   init.PB(rng() % (int)1e6);
  //     // }
  //     // fast_read.Init(init);
  //     // fast_read.InitBrut(init);
  //   } else if (rr <= tysiunc / 2) {
  //     int x = rng() % R;
  //     int v = rng() % R;
  //     fast_read.AddPt(x, v);
  //     fast_read.AddPtBrut(x, v);
  //     fast_add.AddPt(x, v);
  //     debug(x, v);
  //   } else {
  //     int x = rng() % R;
  //     int r1 = fast_read.ReadPref(x);
  //     int r2 = fast_read.ReadPrefBrut(x);
  //     int r3 = fast_add.ReadPref(x);
  //     debug(x);
  //     debug(r1, r2);
  //     assert(r1 == r2);
  //     assert(r1 == r3);
  //   }
  // }
    
  
  int t = 1;
  RE (i, t) {
    Sol sol;
    sol.Test();
  }
  debug(slow_adds, slow_prefs, fast_adds, fast_prefs, resets_len);
  
  
  
  // BEZSENS

  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  return 0;
}