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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//#ifdef DEBUG
//#define _GLIBCXX_DEBUG
//#endif
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;

#ifdef DEBUG
#include "lib/debug.h"
#else
#define debug(...) 228
#endif

#define pb push_back

typedef long long ll;
typedef long double ld;

const int BUF_SZ = 1 << 15;

inline namespace Input {
    char buf[BUF_SZ];
    int pos;
    int len;
    char next_char() {
        if (pos == len) {
            pos = 0;
            len = (int)fread(buf, 1, BUF_SZ, stdin);
            if (!len) { return EOF; }
        }
        return buf[pos++];
    }

    int read_int() {
        int x;
        char ch;
        int sgn = 1;
        while (!isdigit(ch = next_char())) {
            if (ch == '-') { sgn *= -1; }
        }
        x = ch - '0';
        while (isdigit(ch = next_char())) { x = x * 10 + (ch - '0'); }
        return x * sgn;
    }
}  // namespace Input
inline namespace Output {
    char buf[BUF_SZ];
    int pos;

    void flush_out() {
        fwrite(buf, 1, pos, stdout);
        pos = 0;
    }

    void write_char(char c) {
        if (pos == BUF_SZ) { flush_out(); }
        buf[pos++] = c;
    }

    void write_int(ll x) {
        static char num_buf[100];
        if (x < 0) {
            write_char('-');
            x *= -1;
        }
        int len = 0;
        for (; x >= 10; x /= 10) { num_buf[len++] = (char)('0' + (x % 10)); }
        write_char((char)('0' + x));
        while (len) { write_char(num_buf[--len]); }
        write_char('\n');
    }

// auto-flush output when program exits
    void init_output() { assert(atexit(flush_out) == 0); }
}  // namespace Output

int n, m, z;
const int maxN = 1e6 + 10;
int tp[2 * maxN], p[2 * maxN], w[2 * maxN];
int a[maxN];
bool GEN = 0;
void read_input() {
//    cin >> n >> m >> z;
    mt19937 rnd(228);
    if (GEN) {
        n = 1000'000;
        m = 10'001;
        z = 999'999;
    }
    else {
//        cin >> n >> m >> z;
        n = read_int();
        m = read_int();
        z = read_int();
    }
    for (int i = 0; i < n; i++) {
//        cin >> a[i];
        if (GEN) {
            a[i] = rnd() % 1000'000 + 1;
        }
        else {
            a[i] = read_int();
//            cin >> a[i];
        }
    }
    int mm = m;
    int zz = z;
    for (int i = 0; i < m + z; i++) {
        if (GEN) {
            if ((rnd() % 2 && mm > 0) || (zz == 0)) {
                tp[i] = 1;
                p[i] = rnd() % n + 1;
                w[i] = rnd() % 1000'000 + 1;
                --mm;
            }
            else {
                tp[i] = 2;
                p[i] = rnd() % n + 1;
                w[i] = rnd() % 1000'000 + 1;
                --zz;
            }
        }
        else {
            tp[i] = read_int();
            p[i] = read_int();
            w[i] = read_int();
//            cin >> tp[i] >> p[i] >> w[i];
        }
        --p[i];
    }
    if (GEN) {
        assert(mm == 0 && zz == 0);
    }
}

//int msk[maxN];
//int id[maxN];
//int idR[maxN];
int SZ;
void compress() {
    vector<pair<int,int>> S;
    for (int i = 0; i < n; i++) {
        S.emplace_back(a[i], i);
    }
    int qqq = 0;
    for (int i = 0; i < m + z; i++) {
        if (tp[i] == 2) {
            S.emplace_back(w[i], ~i);
        }
    }
    sort(S.begin(), S.end());
    int prv_type = -1;
    int sz = -1;
    for (auto& it : S) {
        int our_type = (it.second < 0 ? 0 : 1);
        if ((our_type == 0 && prv_type == 1) || (prv_type == -1)) {
            sz++;
        }
        if (it.second < 0) {
            w[~it.second] = sz;
        }
        else {
            a[it.second] = sz;
        }
        prv_type = our_type;
    }
    SZ = sz + 2;
    assert(sz <= min(n, z) + 2);
//    for (int i = 0; i < n; i++) cout << a[i] << " ";
//    cout << endl;
//    for (int i = 0; i < m + z; i++) {
//        if (tp[i] == 2) cout << w[i] << " ";
//    }
//    cout << endl;
}

//int prep_cnt[maxS][maxS / B + 2];
//ll prep_sum[maxS / B + 2][B + 3];
//ll sum_block[maxS / B + 2];
//ll sum_solo[maxS];
//int sz_block[maxS / B + 2];
//int his_id[maxN];
//int id_in_block[maxS][maxS / B + 2];
void solve_small_n() {
    const int B = sqrt(n) + 1;
    const int cnt_blocks = (n + B - 1) / B;
    vector<vector<int>> prep_cnt(SZ + 1, vector<int>(cnt_blocks));
    vector<vector<int>> id_in_block(SZ + 1, vector<int>(cnt_blocks));
    vector<int> his_id(n);
    vector<int> sz_block(cnt_blocks);
    vector<vector<ll>> prep_sum(cnt_blocks, vector<ll>(B + 2));
    vector<ll> sum_solo(n);
    vector<ll> sum_block(cnt_blocks);
    for (int bl = 0; bl * B < n; bl++) {
        int L = bl * B;
        int R = min(L + B, n);
        for (int z = L; z < R; z++) {
            prep_cnt[a[z]][bl] += 1;
        }
        for (int z = SZ - 1; z >= 0; z--) {
            prep_cnt[z][bl] += prep_cnt[z + 1][bl];
        }
        vector<int> id(R - L);
        iota(id.begin(), id.end(), L);
        sort(id.begin(), id.end(), [&](int x, int y) {
            return a[x] < a[y];
        });
        int prv = -1;

        for (int x : id) {
            for (int c = prv + 1; c <= a[x]; c++) {
                id_in_block[c][bl] = sz_block[bl];
            }
            his_id[x] = sz_block[bl];
            sz_block[bl]++;
            prv = a[x];
        }
        for (int c = prv + 1; c <= SZ; c++) {
            id_in_block[c][bl] = sz_block[bl];
        }
        sz_block[bl]++;
    }
    for (int i = 0; i < m + z; i++) {
        if (tp[i] == 1) {
            int from = 0;
            int to = p[i] / B;
            for (int j = from; j < to; j++) {
                sum_block[j] += w[i];
            }
            for (int j = to * B; j <= p[i]; j++) {
                sum_solo[j] += w[i];
            }
            int L = to * B;
            int R = min(L + B, n);
            for (int j = 0; j <= sz_block[to]; j++) prep_sum[to][j] = 0;
            for (int j = L; j < R; j++) {
                prep_sum[to][his_id[j]] += sum_solo[j];
            }
            for (int j = sz_block[to] - 1; j >= 0; j--) prep_sum[to][j] += prep_sum[to][j + 1];
        }
        else {
            int from = 0;
            int to = p[i] / B;
            ll ans = 0;
            for (int j = from; j < to; j++) {
                ans += prep_sum[j][id_in_block[w[i]][j]] + prep_cnt[w[i]][j] * sum_block[j];
            }
            for (int j = to * B; j <= p[i]; j++) {
                if (a[j] >= w[i]) {
                    ans += sum_solo[j] + sum_block[to];
                }
            }
            write_int(ans);
        }
    }
}

int ask_L[2 * maxN];
int ask_R[2 * maxN];
int only_z_p[maxN];
int only_z_w[maxN];
ll ans[maxN];

int queries[maxN];
ll pref[maxN];
ll v[maxN];
ll D[maxN];

ll solo_add[maxN];
ll full[maxN];
ll solo[maxN];

int mmap[maxN];

int sz_z;
int tot_sz_z;
int sz_queries;

void process_block_smart() {
    for (int j = 0; j < sz_queries; j++) {
        ask_R[queries[j]] = tot_sz_z;
    }
    vector<int> inds(sz_z);
    iota(inds.begin(), inds.end(), tot_sz_z - sz_z);
    sort(inds.begin(), inds.end(), [&](int x, int y) {
        return only_z_p[x] < only_z_p[y];
    });
    memset(mmap, 0, (SZ + 2) * sizeof(int));
    for (int& v : inds) {
        mmap[only_z_w[v]] = 1;
    }
    mmap[0] = 1;
    int cur_sz = 0;
    for (int v = 0; v <= SZ; v++) {
        if (mmap[v]) {
            mmap[v] = cur_sz++;
        }
        else {
            mmap[v] = mmap[v - 1];
        }
    }
    //can be optimized to sqrt(z) by compressing once again, currently Z^2
    memset(D, 0, (cur_sz + 1) * sizeof(ll));
    for (int p = 0; p < (int)inds.size(); p++) {
        int c = inds[p];
        for (int L = ((p == 0) ? 0 : (only_z_p[inds[p - 1]] + 1)); L <= only_z_p[c]; L++) {
            D[mmap[a[L]]] += v[L];
        }
        for (int x = mmap[only_z_w[c]]; x <= cur_sz; x++) {
            ans[c] += D[x];
        }
    }
    sz_z = 0;
    //apply queries
    for (int j = 0; j < sz_queries; j++) {
        pref[0] += w[queries[j]];
        pref[p[queries[j]] + 1] -= w[queries[j]];
    }
    sz_queries = 0;
    for (int j = 0; j < n; j++) {
        pref[j + 1] += pref[j];
        v[j] += pref[j];
        pref[j] = 0;
    }
}


ll fenw[maxN];
int FOR_FEN = 0;
void upd(int v, ll by) {
    v = FOR_FEN - v;
    while (v <= FOR_FEN) {
        fenw[v] += by;
        v = (v | (v - 1)) + 1;
    }
}
ll get(int r) {
    r = FOR_FEN - r;
    ll ans = 0;
    while (r > 0) {
        ans += fenw[r];
        r &= (r - 1);
    }
    return ans;
}

void process_block_fenw() {
    for (int j = 0; j < sz_queries; j++) {
        ask_R[queries[j]] = tot_sz_z;
    }

    //answer for current queries
    vector<int> inds(sz_z);
    iota(inds.begin(), inds.end(), tot_sz_z - sz_z);
    sort(inds.begin(), inds.end(), [&](int x, int y) {
        return only_z_p[x] < only_z_p[y];
    });
    //can be optimized to sqrt(z) by compressing once again, currently Z^2
//        memset(D, 0, sizeof D);
    for (int p = 0; p <= FOR_FEN; p++) {
        fenw[p] = 0;
    }
    for (int p = 0; p < (int)inds.size(); p++) {
        int c = inds[p];
        for (int L = ((p == 0) ? 0 : (only_z_p[inds[p - 1]] + 1)); L <= only_z_p[c]; L++) {
            upd(a[L], v[L]);
        }
        ans[c] += get(only_z_w[c]);
    }
    sz_z = 0;
    //apply queries
    for (int j = 0; j < sz_queries; j++) {
        pref[0] += w[queries[j]];
        pref[p[queries[j]] + 1] -= w[queries[j]];
    }
    sz_queries = 0;
    for (int j = 0; j < n; j++) {
        pref[j + 1] += pref[j];
        v[j] += pref[j];
        pref[j] = 0;
    }
}

ll tot_pairs = 0;
void rebuild() {
    ll estimate_fenw = n * 100 + sz_z * 100;
    ll estimate_smart = 1LL * sz_z * sz_z + n;
    if (estimate_smart <= estimate_fenw) {
        process_block_smart();
    }
    else {
        process_block_fenw();
    }
    tot_pairs = 0;
}

void finish_smart() {
    vector<pair<int,int>> ques;
    ques.reserve(m + z);
    for (int i = 0; i < m + z; i++) {
        if (tp[i] == 1) {
            for (int x = ask_L[i]; x < ask_R[i]; x++) {
                if (only_z_p[x] <= p[i]) {
                    solo_add[x] += w[i];
                }
            }
            ques.emplace_back(p[i], i);
        }
    }
    for (int i = 0; i < z; i++) {
        ques.emplace_back(only_z_p[i], ~i);
    }
    sort(ques.begin(), ques.end());
    const int B = sqrt(SZ + 1) + 1;
    for (int i = 0; i < ques.size(); i++) {
        for (int x = (i == 0 ? 0 : ques[i - 1].first + 1); x <= ques[i].first; x++) {
            //upd a[x]
            int from = 0;
            int to = a[x] / B;
            for (int c = from; c < to; c++) {
                full[c] += 1;
            }
            for (int c = to * B; c <= a[x]; c++) {
                solo[c] += 1;
            }
        }
        int id = ques[i].second;
        if (id < 0) {
            id = ~id;
            ans[id] += (full[only_z_w[id] / B] + solo[only_z_w[id]]) * solo_add[id];
        }
        else {
            for (int x = ask_L[id]; x < ask_R[id]; x++) {
                if (only_z_p[x] > p[id]) {
                    ans[x] += (full[only_z_w[x] / B] + solo[only_z_w[x]]) * w[id];
                }
            }
        }
    }
    for (int i = 0; i < z; i++) {
        cout << ans[i] << '\n';
    }
}

void finish_fenwick() {
    vector<pair<int,int>> ques;
    ques.reserve(m + z);
    for (int i = 0; i < m + z; i++) {
        if (tp[i] == 1) {
            for (int x = ask_L[i]; x < ask_R[i]; x++) {
                if (only_z_p[x] <= p[i]) {
                    solo_add[x] += w[i];
                }
            }
            ques.emplace_back(p[i], i);
        }
    }
    for (int i = 0; i < z; i++) {
        ques.emplace_back(only_z_p[i], ~i);
    }
    sort(ques.begin(), ques.end());
    memset(fenw, 0, sizeof fenw);
    for (int i = 0; i < (int)ques.size(); i++) {
        for (int x = (i == 0 ? 0 : ques[i - 1].first + 1); x <= ques[i].first; x++) {
            upd(a[x], 1);
        }
        int id = ques[i].second;
        if (id < 0) {
            id = ~id;
            ans[id] += get(only_z_w[id]) * solo_add[id];
        }
        else {
            for (int x = ask_L[id]; x < ask_R[id]; x++) {
                if (only_z_p[x] > p[id]) {
                    ans[x] += get(only_z_w[x]) * w[id];
                }
            }
        }
    }
    for (int i = 0; i < z; i++) {
        cout << ans[i] << '\n';
    }
}

void finish() {
    ll upds = n;
    ll ask = 0;
    for (int i = 0; i < m + z; i++) {
        if (tp[i] == 1) {
            ask += ask_R[i] - ask_L[i];
        }
    }
    const int B = sqrt(SZ + 1);
    if (((ask + upds * B <= upds * 75 + ask * 75)  || z <= 10'000)) {
        finish_smart();
    }
    else {
        finish_fenwick();
    }
}

void solve_small_mz() {
    FOR_FEN = SZ + 1;
    const int est = (int)sqrt(z);
    const int est2 = (int)sqrt(1LL * m * z);
    for (int i = 0; i < m + z; i++) {
        if (tp[i] == 1) {
            if (m == 10'000 && sz_queries >= 102) {
                rebuild();
                assert(sz_z == 0 && sz_queries == 0);
            }
            ask_L[i] = tot_sz_z;
            ask_R[i] = tot_sz_z;
            queries[sz_queries++] = i;
        }
        else {
            tot_pairs += sz_queries;
            if ((z == 10'000 && sz_z >= 102) || (z != 10'000 && sz_z >= 5 * est) || (z != 10'000 && tot_pairs >= 4 * est2)) {
                rebuild();
                assert(sz_z == 0 && sz_queries == 0);
            }
            only_z_p[tot_sz_z] = p[i];
            only_z_w[tot_sz_z] = w[i];
            tot_sz_z++;
            sz_z++;
        }
    }
    if (sz_z) {
        rebuild();
    }
    finish();
}


int main() {
    init_output();
//    ios_base::sync_with_stdio(false);
//    cin.tie(nullptr);
    auto start = std::chrono::high_resolution_clock::now();
#ifdef DEBUG
    freopen("2.in", "r", stdin);
#endif
    read_input();
    compress();
    if (n == 10'000) solve_small_n();
    else if ((m == 10'000) || (z == 10'000)) solve_small_mz();
    else {
        if (n <= 350'000) solve_small_n();
        else solve_small_mz();
    }
    auto end = std::chrono::high_resolution_clock::now();
    std::cerr << "Execution Time: "
              << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()
              << " ms" << std::endl;
    return 0;
}