CSES - Aalto Competitive Programming 2024 - wk4 - Wed - Results
Submission details
Task:Box Stack II
Sender:odanobunaga8199
Submission time:2024-09-25 16:52:50 +0300
Language:C++ (C++20)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails
#7ACCEPTED0.00 sdetails
#8ACCEPTED0.00 sdetails
#9ACCEPTED0.00 sdetails
#10ACCEPTED0.00 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.00 sdetails
#14ACCEPTED0.00 sdetails
#15ACCEPTED0.00 sdetails
#16ACCEPTED0.00 sdetails
#17ACCEPTED0.00 sdetails
#18ACCEPTED0.00 sdetails
#19ACCEPTED0.00 sdetails
#20ACCEPTED0.00 sdetails
#21ACCEPTED0.00 sdetails
#22ACCEPTED0.00 sdetails
#23ACCEPTED0.00 sdetails
#24ACCEPTED0.00 sdetails
#25ACCEPTED0.00 sdetails
#26ACCEPTED0.00 sdetails
#27ACCEPTED0.00 sdetails
#28ACCEPTED0.00 sdetails
#29ACCEPTED0.00 sdetails
#30ACCEPTED0.00 sdetails
#31ACCEPTED0.00 sdetails
#32ACCEPTED0.00 sdetails
#33ACCEPTED0.00 sdetails
#34ACCEPTED0.00 sdetails
#35ACCEPTED0.00 sdetails
#36ACCEPTED0.00 sdetails
#37ACCEPTED0.00 sdetails
#38ACCEPTED0.00 sdetails
#39ACCEPTED0.00 sdetails
#40ACCEPTED0.00 sdetails
#41ACCEPTED0.00 sdetails
#42ACCEPTED0.00 sdetails
#43ACCEPTED0.00 sdetails
#44ACCEPTED0.00 sdetails
#45ACCEPTED0.00 sdetails
#46ACCEPTED0.00 sdetails
#47ACCEPTED0.00 sdetails
#48ACCEPTED0.00 sdetails
#49ACCEPTED0.00 sdetails
#50ACCEPTED0.00 sdetails
#51ACCEPTED0.00 sdetails
#52ACCEPTED0.00 sdetails
#53ACCEPTED0.00 sdetails
#54ACCEPTED0.00 sdetails
#55ACCEPTED0.00 sdetails
#56ACCEPTED0.00 sdetails
#57ACCEPTED0.00 sdetails
#58ACCEPTED0.00 sdetails
#59ACCEPTED0.00 sdetails
#60ACCEPTED0.00 sdetails
#61ACCEPTED0.00 sdetails
#62ACCEPTED0.17 sdetails
#63ACCEPTED0.20 sdetails
#64ACCEPTED0.17 sdetails
#65ACCEPTED0.17 sdetails
#66ACCEPTED0.20 sdetails
#67ACCEPTED0.17 sdetails
#68ACCEPTED0.17 sdetails
#69ACCEPTED0.17 sdetails
#70ACCEPTED0.17 sdetails
#71ACCEPTED0.17 sdetails
#72ACCEPTED0.10 sdetails
#73ACCEPTED0.10 sdetails

Code

#include <bits/stdc++.h>
using namespace std;

struct Box {
    int x;
    int y;
    long long z;
    int y_rank;
};

struct FenwickTree {
    int size;
    vector<long long> tree;

    FenwickTree(int n) : size(n), tree(n + 1, 0) {}

    void update(int idx, long long val) {
        while (idx <= size) {
            if (val > tree[idx]) {
                tree[idx] = val;
            } else {
                break;
            }
            idx += idx & (-idx);
        }
    }

    long long query(int idx) const {
        long long res = 0;
        int current = idx;
        while (current > 0) {
            res = max(res, tree[current]);
            current -= current & (-current);
        }
        return res;
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin >> n;
    vector<Box> boxes(n);
    vector<int> ys(n);
    for(int i=0; i < n; ++i){
        cin >> boxes[i].x >> boxes[i].y >> boxes[i].z;
        ys[i] = boxes[i].y;
    }

    sort(boxes.begin(), boxes.end(), [&](const Box &a, const Box &b) -> bool {
        if(a.x != b.x) return a.x < b.x;
        return a.y < b.y;
    });

    vector<int> sorted_ys = ys;
    sort(sorted_ys.begin(), sorted_ys.end());
    sorted_ys.erase(unique(sorted_ys.begin(), sorted_ys.end()), sorted_ys.end());

    for(int i=0; i < n; ++i){
        boxes[i].y_rank = lower_bound(sorted_ys.begin(), sorted_ys.end(), boxes[i].y) - sorted_ys.begin() + 1;
    }

    int m = sorted_ys.size();
    FenwickTree BIT(m);

    long long global_max = 0;

    for(int i=0; i < n; ++i){
        int y_r = boxes[i].y_rank;
        long long current_max = BIT.query(y_r);
        long long dp = current_max + boxes[i].z;
        global_max = max(global_max, dp);
        BIT.update(y_r, dp);
    }

    cout << global_max;
}

Test details

Test 1

Verdict: ACCEPTED

input
1
7 8 3

correct output
3

user output
3

Test 2

Verdict: ACCEPTED

input
2
6 5 4
1 1 1

correct output
5

user output
5

Test 3

Verdict: ACCEPTED

input
2
3 6 7
9 7 10

correct output
17

user output
17

Test 4

Verdict: ACCEPTED

input
2
5 1 3
8 5 9

correct output
12

user output
12

Test 5

Verdict: ACCEPTED

input
3
7 7 4
2 6 9
2 5 2

correct output
15

user output
15

Test 6

Verdict: ACCEPTED

input
3
8 2 9
9 6 8
7 7 3

correct output
17

user output
17

Test 7

Verdict: ACCEPTED

input
3
1 6 6
5 1 9
10 6 10

correct output
19

user output
19

Test 8

Verdict: ACCEPTED

input
4
1 2 1
7 8 3
3 4 8
6 6 3

correct output
15

user output
15

Test 9

Verdict: ACCEPTED

input
4
8 2 9
9 6 8
7 7 3
1 6 4

correct output
17

user output
17

Test 10

Verdict: ACCEPTED

input
4
1 2 3
5 2 10
6 2 5
7 3 6

correct output
24

user output
24

Test 11

Verdict: ACCEPTED

input
4
9 7 7
3 8 3
2 2 10
10 7 4

correct output
21

user output
21

Test 12

Verdict: ACCEPTED

input
5
6 6 8
9 7 9
6 9 5
7 7 4
...

correct output
30

user output
30

Test 13

Verdict: ACCEPTED

input
5
5 10 8
10 1 2
4 10 2
3 1 4
...

correct output
14

user output
14

Test 14

Verdict: ACCEPTED

input
5
5 2 1
10 6 10
5 5 5
4 4 2
...

correct output
17

user output
17

Test 15

Verdict: ACCEPTED

input
5
6 1 8
9 3 2
6 6 9
5 9 1
...

correct output
20

user output
20

Test 16

Verdict: ACCEPTED

input
5
10 10 6
2 10 9
8 7 7
6 3 2
...

correct output
15

user output
15

Test 17

Verdict: ACCEPTED

input
5
3 1 9
9 3 4
10 10 5
1 7 4
...

correct output
20

user output
20

Test 18

Verdict: ACCEPTED

input
5
9 10 4
3 9 1
1 4 2
10 6 1
...

correct output
11

user output
11

Test 19

Verdict: ACCEPTED

input
5
1 3 8
4 5 10
8 5 10
4 6 3
...

correct output
28

user output
28

Test 20

Verdict: ACCEPTED

input
5
9 1 10
3 9 4
6 9 3
5 1 7
...

correct output
17

user output
17

Test 21

Verdict: ACCEPTED

input
5
1 4 6
5 5 1
2 4 2
1 3 9
...

correct output
18

user output
18

Test 22

Verdict: ACCEPTED

input
10
6 6 8
9 7 9
6 9 5
7 7 4
...

correct output
41

user output
41

Test 23

Verdict: ACCEPTED

input
10
5 10 8
10 1 2
4 10 2
3 1 4
...

correct output
35

user output
35

Test 24

Verdict: ACCEPTED

input
10
5 2 1
10 6 10
5 5 5
4 4 2
...

correct output
33

user output
33

Test 25

Verdict: ACCEPTED

input
10
6 1 8
9 3 2
6 6 9
5 9 1
...

correct output
21

user output
21

Test 26

Verdict: ACCEPTED

input
10
10 10 6
2 10 9
8 7 7
6 3 2
...

correct output
36

user output
36

Test 27

Verdict: ACCEPTED

input
10
3 1 9
9 3 4
10 10 5
1 7 4
...

correct output
37

user output
37

Test 28

Verdict: ACCEPTED

input
10
9 10 4
3 9 1
1 4 2
10 6 1
...

correct output
25

user output
25

Test 29

Verdict: ACCEPTED

input
10
1 3 8
4 5 10
8 5 10
4 6 3
...

correct output
33

user output
33

Test 30

Verdict: ACCEPTED

input
10
9 1 10
3 9 4
6 9 3
5 1 7
...

correct output
28

user output
28

Test 31

Verdict: ACCEPTED

input
10
1 4 6
5 5 1
2 4 2
1 3 9
...

correct output
29

user output
29

Test 32

Verdict: ACCEPTED

input
100
589284012 636562060 767928734
906523441 647212241 921212095
585063857 909729626 454895875
669546421 693523526 412726717
...

correct output
10998205207

user output
10998205207

Test 33

Verdict: ACCEPTED

input
100
447773962 773442532 122816
137572579 324627123 157577940
253498609 99147813 425825313
199995380 416515986 371043004
...

correct output
8897447989

user output
8897447989

Test 34

Verdict: ACCEPTED

input
100
468145963 198730372 27838076
590195590 467423861 520495379
451366491 344173378 354694313
165814381 219739800 750398099
...

correct output
9141888792

user output
9141888792

Test 35

Verdict: ACCEPTED

input
100
591414747 75940263 760367935
901888417 312356591 130275571
548496961 611293382 958794496
469291685 962387379 20130523
...

correct output
10698400685

user output
10698400685

Test 36

Verdict: ACCEPTED

input
100
967034924 587586158 185430194
918715995 767527830 653946995
749180621 641621091 232024335
151896000 241061404 6689688
...

correct output
12833486742

user output
12833486742

Test 37

Verdict: ACCEPTED

input
100
238363353 59249204 934941692
892631472 221963002 390559518
986350949 524427523 96444602
656854970 425992688 822387303
...

correct output
11001071375

user output
11001071375

Test 38

Verdict: ACCEPTED

input
100
958701283 356460601 224848374
881788059 68992860 44771412
397401947 115595477 638932295
106806913 568887059 653343572
...

correct output
8425454448

user output
8425454448

Test 39

Verdict: ACCEPTED

input
100
81935404 244103474 837431431
342493822 470738321 776814822
489180570 330726191 578205540
283329158 538074003 93140055
...

correct output
10342518662

user output
10342518662

Test 40

Verdict: ACCEPTED

input
100
937837681 11934038 257096283
933290530 405355767 570001955
876668629 249890139 453495728
12239373 657165788 462212374
...

correct output
8144852495

user output
8144852495

Test 41

Verdict: ACCEPTED

input
100
11139168 391337048 538883744
535937150 532332526 8099343
143698367 339543270 152590624
14304401 234675590 941909102
...

correct output
9522402349

user output
9522402349

Test 42

Verdict: ACCEPTED

input
200
589284012 636562060 767928734
906523441 647212241 921212095
585063857 909729626 454895875
669546421 693523526 412726717
...

correct output
15209742885

user output
15209742885

Test 43

Verdict: ACCEPTED

input
200
447773962 773442532 122816
137572579 324627123 157577940
253498609 99147813 425825313
199995380 416515986 371043004
...

correct output
13615506574

user output
13615506574

Test 44

Verdict: ACCEPTED

input
200
468145963 198730372 27838076
590195590 467423861 520495379
451366491 344173378 354694313
165814381 219739800 750398099
...

correct output
12798974974

user output
12798974974

Test 45

Verdict: ACCEPTED

input
200
591414747 75940263 760367935
901888417 312356591 130275571
548496961 611293382 958794496
469291685 962387379 20130523
...

correct output
15137965961

user output
15137965961

Test 46

Verdict: ACCEPTED

input
200
967034924 587586158 185430194
918715995 767527830 653946995
749180621 641621091 232024335
151896000 241061404 6689688
...

correct output
17343709538

user output
17343709538

Test 47

Verdict: ACCEPTED

input
200
238363353 59249204 934941692
892631472 221963002 390559518
986350949 524427523 96444602
656854970 425992688 822387303
...

correct output
16484729704

user output
16484729704

Test 48

Verdict: ACCEPTED

input
200
958701283 356460601 224848374
881788059 68992860 44771412
397401947 115595477 638932295
106806913 568887059 653343572
...

correct output
13668103642

user output
13668103642

Test 49

Verdict: ACCEPTED

input
200
81935404 244103474 837431431
342493822 470738321 776814822
489180570 330726191 578205540
283329158 538074003 93140055
...

correct output
12761842389

user output
12761842389

Test 50

Verdict: ACCEPTED

input
200
937837681 11934038 257096283
933290530 405355767 570001955
876668629 249890139 453495728
12239373 657165788 462212374
...

correct output
13061107314

user output
13061107314

Test 51

Verdict: ACCEPTED

input
200
11139168 391337048 538883744
535937150 532332526 8099343
143698367 339543270 152590624
14304401 234675590 941909102
...

correct output
14114713109

user output
14114713109

Test 52

Verdict: ACCEPTED

input
1000
589284012 636562060 767928734
906523441 647212241 921212095
585063857 909729626 454895875
669546421 693523526 412726717
...

correct output
33891762384

user output
33891762384

Test 53

Verdict: ACCEPTED

input
1000
447773962 773442532 122816
137572579 324627123 157577940
253498609 99147813 425825313
199995380 416515986 371043004
...

correct output
33355847789

user output
33355847789

Test 54

Verdict: ACCEPTED

input
1000
468145963 198730372 27838076
590195590 467423861 520495379
451366491 344173378 354694313
165814381 219739800 750398099
...

correct output
33709682513

user output
33709682513

Test 55

Verdict: ACCEPTED

input
1000
591414747 75940263 760367935
901888417 312356591 130275571
548496961 611293382 958794496
469291685 962387379 20130523
...

correct output
34006550890

user output
34006550890

Test 56

Verdict: ACCEPTED

input
1000
967034924 587586158 185430194
918715995 767527830 653946995
749180621 641621091 232024335
151896000 241061404 6689688
...

correct output
34976444698

user output
34976444698

Test 57

Verdict: ACCEPTED

input
1000
238363353 59249204 934941692
892631472 221963002 390559518
986350949 524427523 96444602
656854970 425992688 822387303
...

correct output
34699884028

user output
34699884028

Test 58

Verdict: ACCEPTED

input
1000
958701283 356460601 224848374
881788059 68992860 44771412
397401947 115595477 638932295
106806913 568887059 653343572
...

correct output
34046313943

user output
34046313943

Test 59

Verdict: ACCEPTED

input
1000
81935404 244103474 837431431
342493822 470738321 776814822
489180570 330726191 578205540
283329158 538074003 93140055
...

correct output
33580356050

user output
33580356050

Test 60

Verdict: ACCEPTED

input
1000
937837681 11934038 257096283
933290530 405355767 570001955
876668629 249890139 453495728
12239373 657165788 462212374
...

correct output
35744107623

user output
35744107623

Test 61

Verdict: ACCEPTED

input
1000
11139168 391337048 538883744
535937150 532332526 8099343
143698367 339543270 152590624
14304401 234675590 941909102
...

correct output
35345712189

user output
35345712189

Test 62

Verdict: ACCEPTED

input
200000
589284012 636562060 767928734
906523441 647212241 921212095
585063857 909729626 454895875
669546421 693523526 412726717
...

correct output
526570534463

user output
526570534463

Test 63

Verdict: ACCEPTED

input
200000
447773962 773442532 122816
137572579 324627123 157577940
253498609 99147813 425825313
199995380 416515986 371043004
...

correct output
527468957174

user output
527468957174

Test 64

Verdict: ACCEPTED

input
200000
468145963 198730372 27838076
590195590 467423861 520495379
451366491 344173378 354694313
165814381 219739800 750398099
...

correct output
530364449483

user output
530364449483

Test 65

Verdict: ACCEPTED

input
200000
591414747 75940263 760367935
901888417 312356591 130275571
548496961 611293382 958794496
469291685 962387379 20130523
...

correct output
531529346089

user output
531529346089

Test 66

Verdict: ACCEPTED

input
200000
967034924 587586158 185430194
918715995 767527830 653946995
749180621 641621091 232024335
151896000 241061404 6689688
...

correct output
532748830637

user output
532748830637

Test 67

Verdict: ACCEPTED

input
200000
238363353 59249204 934941692
892631472 221963002 390559518
986350949 524427523 96444602
656854970 425992688 822387303
...

correct output
525080370284

user output
525080370284

Test 68

Verdict: ACCEPTED

input
200000
958701283 356460601 224848374
881788059 68992860 44771412
397401947 115595477 638932295
106806913 568887059 653343572
...

correct output
529566966249

user output
529566966249

Test 69

Verdict: ACCEPTED

input
200000
81935404 244103474 837431431
342493822 470738321 776814822
489180570 330726191 578205540
283329158 538074003 93140055
...

correct output
521546866708

user output
521546866708

Test 70

Verdict: ACCEPTED

input
200000
937837681 11934038 257096283
933290530 405355767 570001955
876668629 249890139 453495728
12239373 657165788 462212374
...

correct output
531573800077

user output
531573800077

Test 71

Verdict: ACCEPTED

input
200000
11139168 391337048 538883744
535937150 532332526 8099343
143698367 339543270 152590624
14304401 234675590 941909102
...

correct output
529286403784

user output
529286403784

Test 72

Verdict: ACCEPTED

input
200000
1000000000 1000000000 10000000...

correct output
200000000000000

user output
200000000000000

Test 73

Verdict: ACCEPTED

input
200000
1000000000 1 1
1000000000 2 2
1000000000 3 3
1000000000 4 4
...

correct output
20000100000

user output
20000100000