QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#886324#9974. After godhhoppitreeAC ✓1409ms224900kbC++202.7kb2025-02-06 22:06:272025-02-06 22:06:28

Judging History

This is the latest submission verdict.

  • [2025-02-06 22:06:28]
  • Judged
  • Verdict: AC
  • Time: 1409ms
  • Memory: 224900kb
  • [2025-02-06 22:06:27]
  • Submitted

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5, inf = 1e9;

int a[N], lst[N];
vector< array<int, 3> > mdf[N];
vector<int> qry[N];
long long res[N];

struct Info {
    int mn, mnc, sec, lz;
    long long k, lzk, b, lzb;

    void apply(int x, long long tk, long long tb) {
        if (x <= mn) return;
        assert(x < sec);
        mn = lz = x, k += tk * mnc, b += tb * mnc, lzk += tk, lzb += tb;
    }
} p[1 << 21];

void build(int k, int l, int r) {
    p[k].mnc = r - l + 1, p[k].sec = inf, p[k].lz = -inf;
    if (l == r) return;
    int mid = (l + r) >> 1;
    build(k << 1, l, mid), build(k << 1 | 1, mid + 1, r);
}

void pushdown(int k) {
    if (p[k].lz != -inf) {
        p[k << 1].apply(p[k].lz, p[k].lzk, p[k].lzb);
        p[k << 1 | 1].apply(p[k].lz, p[k].lzk, p[k].lzb);
        p[k].lz = -inf, p[k].lzk = p[k].lzb = 0;
    }
}

void pushup(int k) {
    if (p[k << 1].mn == p[k << 1 | 1].mn) {
        p[k].mn = p[k << 1].mn;
        p[k].mnc = p[k << 1].mnc + p[k << 1 | 1].mnc;
        p[k].sec = min(p[k << 1].sec, p[k << 1 | 1].sec);
    } else {
        int t = (p[k << 1].mn < p[k << 1 | 1].mn ? k << 1 : k << 1 | 1);
        p[k].mn = p[t].mn, p[k].mnc = p[t].mnc;
        p[k].sec = min(p[t].sec, p[t ^ 1].mn);
    }
    p[k].k = p[k << 1].k + p[k << 1 | 1].k, p[k].b = p[k << 1].b + p[k << 1 | 1].b;
}

void modify(int k, int l, int r, int x, int y, int z, int w) {
    if (l > y || r < x || p[k].mn >= z) return;
    if (l >= x && r <= y && p[k].sec > z) {
        p[k].apply(z, z - p[k].mn, -1ll * (z - p[k].mn) * w);
        return;
    }
    pushdown(k);
    int mid = (l + r) >> 1;
    modify(k << 1, l, mid, x, y, z, w);
    modify(k << 1 | 1, mid + 1, r, x, y, z, w);
    pushup(k);
}

long long query(int k, int l, int r, int x, int y, int z) {
    if (l > y || r < x) return 0;
    if (l >= x && r <= y) return p[k].k * z + p[k].b;
    pushdown(k);
    int mid = (l + r) >> 1;
    return query(k << 1, l, mid, x, y, z) + query(k << 1 | 1, mid + 1, r, x, y, z);
}

signed main() {
    int n, q; scanf("%d%d", &n, &q);
    for (int i = 1; i <= q; ++i) {
        int x, y; scanf("%d%d", &x, &y);
        mdf[x].push_back({lst[x] + 1, i - 1, a[x]}), a[x] = y;
        lst[x] = i - 1;
        qry[x].push_back(i);
    }
    for (int i = 1; i <= n; ++i) {
        mdf[i].push_back({lst[i] + 1, q, a[i]});
    }
    build(1, 1, q);
    for (int i = 1; i <= n; ++i) {
        for (auto [x, y, z] : mdf[i]) modify(1, 1, q, x, y, z, i - 1);
        for (auto x : qry[i]) res[x] = query(1, 1, q, 1, x, i);
    }
    for (int i = 1; i <= q; ++i) printf("%lld\n", res[i]);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1042ms
memory: 224824kb

input:

1000000 1000000
671688 1
193619 1
70068 1
729448 2
832062 64939
205871 1
749767 4
710152 4
993304 117427
157247 1
721584 4
6827 1
369183 2
476643 4
852855 753355
508841 6
245181 3
54307 1
137064 2
115503 2
66950 1
291862 1
770657 976136
191696 4
202434 3
529922 4
231829 2
589065 8
782988 799136
2784...

output:

1
1
1
1912354
3354971
555469
4114011
4395512
52363093869
697440
6578712
1
3592324
5566584
14870965144
7526096
3057721
332367
1644878
1387018
601240
6287130
35959319
3933576
4568550
22006682
6440571
31174109
84319349640
10571127
2675341
6175225
7634985
44934887
18882319
49073254
60939115
371152295314...

result:

ok 1000000 numbers

Test #2:

score: 0
Accepted
time: 1305ms
memory: 224900kb

input:

999999 1000000
312875 3
381493 3
4668 3
87981 7
957493 991572
469162 6
768472 655464
512257 11
367677 9
319351 11
87719 13
34575 12
867648 942096
61897 7
911419 971532
851326 933538
206975 20
350094 20
305931 22
902570 519994
300850 18
315911 22
631720 23
416480 24
27940 26
399456 26
143760 30
47191...

output:

3
411714
3
499888
20390817
11085852
25733992
18818462
14664758
14069670
2242414
897249
455124441246
2798001
881010758320
543188700046
23653493
50607816
47010729
1333494094056
54355931
62374548
159666754
102788888
1605860
116901866
34049196
169383936
150762668
301601687
103750858
387508164
258662566
...

result:

ok 1000000 numbers

Test #3:

score: 0
Accepted
time: 1218ms
memory: 224808kb

input:

999999 999997
740892 265847
964824 840967
108870 3
469770 4
679463 624417
273008 6
759585 949748
89991 8
419655 9
936798 464191
22772 11
583215 12
544455 13
308829 14
817308 377378
232839 16
85851 17
762553 118364
336401 19
483141 20
54366 21
885647 794497
121171 23
43183 24
864400 918735
561528 26
...

output:

265847
119064407622
3
2165407
6179147
1969671
169981637490
8
10816319
1403074336882
11
33697621
36684186
21053023
1197148046225
19152419
4857166
761090314280
45949751
79524268
3823005
3127690905142
17855960
3143461
3204474949575
167026493
5512039397761
2835773
4858226096061
1445199
28110298
36768891...

result:

ok 999997 numbers

Test #4:

score: 0
Accepted
time: 1409ms
memory: 224872kb

input:

1000000 1000000
873969 917538
258617 391
679471 55
143506 613
39586 955
173282 118
921617 564545
562228 874
365474 291
716851 20
300357 978
647771 56
833571 898026
419227 827
832060 514628
88905 343
455537 539
874818 340877
302139 648
212710 590
231473 196
639889 730
772833 42741
283901 403
388827 6...

output:

917538
391
329108610
613
955
273614571
309357863512
2490598043
1775749928
4590535048
1872052581
5275956398
7734623821
3931132620
9257126908
565207200
5534296059
246766727025
3892732759
2687772665
3169226064
11015375746
14233993104
4772271218
7286778193
6197751348
13755186665
2853447720
17778477456
5...

result:

ok 1000000 numbers

Test #5:

score: 0
Accepted
time: 950ms
memory: 224792kb

input:

999998 999998
191273 300198
355220 564306
135156 705567
770148 940716
729867 406412
549451 631912
550138 316237
83502 883979
990317 926853
660641 464732
435671 430720
505827 771840
734203 783120
828323 928019
262696 569119
527150 684021
684018 118641
682853 962304
799516 965104
149803 956890
980924 ...

output:

300198
98433987516
705567
1353201350439
1681145302716
1435601807268
1730932929993
883979
5499635512302
3746838486690
2473398599400
3402940572334
5990650227576
7639490036754
1760063222879
5159549435400
7627423446743
8141822676391
10484155201209
813600283545
15219819043967
8479587926397
12888513525800...

result:

ok 999998 numbers