QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#687341#9165. Petrol stationsQwerty1232100 ✓507ms41120kbC++239.1kb2024-10-29 18:28:342024-10-29 18:28:34

Judging History

你现在查看的是最新测评结果

  • [2024-10-29 18:28:34]
  • 评测
  • 测评结果:100
  • 用时:507ms
  • 内存:41120kb
  • [2024-10-29 18:28:34]
  • 提交

answer

#include <bits/stdc++.h>

// #define int int64_t
int K;

struct Shit {
    int64_t tm;
    int cnt, prf;

    bool operator<(const Shit& other) const {
        return tm < other.tm;
    }
};
struct Cum {
    int64_t dlt;
    int sh;
    std::vector<Shit> vec;

    bool operator<(const Cum& other) const {
        return false;
    }
};
using Fuck = std::vector<Cum>;
// using Fuck2 = std::pair<int64_t, Fuck>;

int cum_size(const Cum& c) {
    return c.vec.size() - c.sh;
}

void cum_push(Cum& a) {
    for (int i = a.sh; i < a.vec.size(); i++) {
        a.vec[i].tm += a.dlt;
    }
}

Cum cum_merge(Cum& a, Cum& b) {
    Cum res(0, 0, {});
    cum_push(a);
    cum_push(b);
    res.vec.resize(cum_size(a) + cum_size(b));
    std::merge(a.vec.begin() + a.sh, a.vec.end(),
               b.vec.begin() + b.sh, b.vec.end(),
               res.vec.begin());
    for (int i = 0, s = 0; i < res.vec.size(); i++) {
        res.vec[i].prf = (s += res.vec[i].cnt);
    }

    a.dlt = 0, a.sh = 0, a.vec.clear();
    b.dlt = 0, b.sh = 0, b.vec.clear();
    return res;
}

// merges to a
void fuck_merge(Fuck& a, Fuck& b) {
    a.resize(std::max(a.size(), b.size()) + 1);
    Cum carry{0, 0, {}};
    for (int i = 0; i < a.size(); i++) {
        if (cum_size(carry)) {
            if (cum_size(a[i])) {
                carry = cum_merge(carry, a[i]);
            } else {
                a[i] = std::move(carry);
            }
        }
        if (i < b.size() && cum_size(b[i])) {
            if (cum_size(a[i])) {
                carry = cum_merge(a[i], b[i]);
            } else {
                a[i] = std::move(b[i]);
            }
        }
    }
    assert(cum_size(carry) == 0);
    while (a.size() && cum_size(a.back()) == 0) {
        a.pop_back();
    }
    b.clear();
}

__attribute__((optimize("O3"), target("avx2"))) std::pair<int, int> cum_fisting(Cum& c, int w) {
    c.dlt -= w;
    int dlt = 0;
    int sum = 0;
    int it = std::lower_bound(c.vec.begin() + c.sh, c.vec.end(), Shit{-c.dlt, 0, 0}) - c.vec.begin();
    // while (cum_size(c) && c.vec[c.sh].tm + c.dlt < 0) {

    sum = (it == 0 ? 0 : c.vec[it - 1].prf) - (c.sh == 0 ? 0 : c.vec[c.sh - 1].prf);
    // for (int i = c.sh; i < it; i++) {
    //     sum += c.vec[i].cnt;
    // }
    dlt = it - c.sh;
    c.sh = it;
    if (dlt) {
        c.vec.push_back({(K - w) - c.dlt, sum});
        c.vec.back().prf = c.vec.back().cnt + (c.vec.size() == 1 ? 0 : c.vec.rbegin()[1].prf);
    }
    return {dlt, sum};
}

int fuck_fisting(Fuck& f, int w) {
    int sum = 0;
    for (auto& cum : f) {
        auto [dt, sm] = cum_fisting(cum, w);
        sum += sm;
    }
    return sum;
}

void cum_reverse_fisting(Cum& c, int w, int ftg) {
    c.dlt += w;
    if (ftg) {
        c.sh -= ftg;
        c.vec.pop_back();
    }
}

int32_t main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n;
    std::cin >> n >> K;
    std::vector<std::vector<std::pair<int, int>>> gr(n);
    std::vector<std::vector<int64_t>> dp(n);

    for (int i = 0; i < n - 1; i++) {
        int u, v, w;
        std::cin >> u >> v >> w;
        gr[u].push_back({v, w}), gr[v].push_back({u, w});
    }
    for (int i = 0; i < n; i++) {
        dp[i].resize(gr[i].size());
    }
    std::vector<int> fising_sz(n), fisting_depth(n);
    {
        auto dfs = [&](auto dfs, int v, int f) -> void {
            fising_sz[v] = 1;
            for (auto [t, w] : gr[v]) {
                if (t != f) {
                    fisting_depth[t] = fisting_depth[v] + 1;
                    dfs(dfs, t, v);
                    fising_sz[v] += fising_sz[t];
                }
            }
        };
        dfs(dfs, 0, -1);
    }

    auto get_sb_sz = [&](int u, int v) {
        if (fisting_depth[u] < fisting_depth[v]) {
            return fising_sz[v];
        }
        return n - fising_sz[u];
    };

    // for (int i = 0; i < n; i++) {
    //     auto dfs = [&](auto dfs, int v, int fr) -> Fuck {
    //         Fuck f;
    //         f.push_back(Cum{0, 0, {Shit{K, 1}}});
    //         for (int ti = 0; ti < gr[v].size(); ti++) {
    //             auto [t, w] = gr[v][ti];
    //             if (t == fr) {
    //                 continue;
    //             }
    //             auto f2 = dfs(dfs, t, v);
    //             int sum = fuck_fisting(f2, w);
    //             dp[v][ti] = sum;
    //             fuck_merge(f, f2);
    //         }
    //         return f;
    //     };
    //     dfs(dfs, i, -1);
    // }

    std::vector<int> sz(n), used(n, 0);

    auto find = [&](int v) {
        auto dfs = [&](auto dfs, int v, int f) -> void {
            sz[v] = 1;
            for (auto [t, w] : gr[v]) {
                if (t != f && !used[t]) {
                    dfs(dfs, t, v);
                    sz[v] += sz[t];
                }
            }
        };
        dfs(dfs, v, -1);
        int f = -1;
        int s = sz[v];
        while (true) {
            auto it = std::find_if(gr[v].begin(), gr[v].end(), [&](auto p) { return p.first != f && !used[p.first] && sz[p.first] > s / 2; });
            if (it == gr[v].end()) {
                break;
            } else {
                f = v;
                v = it->first;
            }
        }
        return v;
    };

    std::vector<int64_t> ans(n, 0);
    auto build = [&](auto build, int v) -> void {
        v = find(v);

        auto dfs = [&](auto dfs, int v, int fr, int cum_left, int sm = 0) -> Fuck {
            sz[v] = 1;
            Fuck f;
            f.push_back(Cum{0, 0, {Shit{K, 1, 1}}});
            for (int ti = 0; ti < gr[v].size(); ti++) {
                auto [t, w] = gr[v][ti];
                if (t == fr) {
                    dp[v][ti] += sm;
                    continue;
                }
                if (used[t]) {
                    continue;
                }

                int sum = int(cum_left - w < 0);
                // dp[v][ti] += sum;
                // ans[v] += sum;

                auto f2 = dfs(dfs, t, v, cum_left - w < 0 ? K - w : cum_left - w, sum);
                sz[v] += sz[t];
                fuck_fisting(f2, w);
                fuck_merge(f, f2);
            }
            return f;
        };

        // struct Cmp {
        //     template <typename T>
        //     bool operator()(const T& a, const T& b) const {
        //         return a.first < b.first;
        //     }
        // };

        // std::priority_queue<std::pair<std::pair<int, std::vector<int>>, Cum>> heap;
        std::priority_queue<std::pair<int, std::pair<std::vector<int>*, Cum*>>> heap;

        for (int ti = 0, xx = 0; ti < gr[v].size(); ti++) {
            auto [t, w] = gr[v][ti];
            if (used[t]) {
                continue;
            }

            Fuck f;
            f.push_back(Cum{0, 0, {}});
            xx = 1;
            auto f2 = dfs(dfs, t, v, K - w);
            int sum = fuck_fisting(f2, w);
            dp[v][ti] += sum;
            fuck_merge(f, f2);

            Cum* c = new Cum(0, 0, {});
            for (auto& c2 : f) {
                *c = cum_merge(*c, c2);
            }
            std::pair<std::vector<int>*, Cum*> ptr(new std::vector<int>{ti}, c);
            heap.push({-sz[t], ptr});
        }

        while (heap.size() > 1) {
            auto [sz1, p1] = heap.top();
            heap.pop();
            auto [sz2, p2] = heap.top();
            heap.pop();

            auto [ti1, c1] = p1;
            auto [ti2, c2] = p2;

            auto dfs = [&](auto dfs, int v, int f, int fw, int f_id, Cum& c) -> void {
                auto [fisting, sum] = cum_fisting(c, fw);
                // dp[f][f_id] += sum;
                for (int ti = 0; ti < gr[v].size(); ti++) {
                    auto [t, w] = gr[v][ti];
                    if (t == f) {
                        dp[v][ti] += sum;
                        continue;
                    }
                    if (used[t]) {
                        continue;
                    }
                    dfs(dfs, t, v, w, ti, c);
                }
                cum_reverse_fisting(c, fw, fisting);
            };

            for (auto ti : *ti1) {
                dfs(dfs, gr[v][ti].first, v, gr[v][ti].second, ti, *c2);
            }
            for (auto ti : *ti2) {
                dfs(dfs, gr[v][ti].first, v, gr[v][ti].second, ti, *c1);
            }

            *c1 = cum_merge(*c1, *c2);
            ti1->insert(ti1->end(), ti2->begin(), ti2->end());
            delete c2, ti2;
            heap.push({sz1 + sz2, {ti1, c1}});
        }

        used[v] = true;
        for (auto [t, w] : gr[v]) {
            if (!used[t]) {
                build(build, t);
            }
        }
    };

    build(build, 0);

    for (int v = 0; v < n; v++) {
        for (int ti = 0; ti < gr[v].size(); ti++) {
            auto [t, w] = gr[v][ti];
            ans[t] += int64_t(dp[v][ti]) * get_sb_sz(t, v);
        }
    }

    for (int i = 0; i < n; i++) {
        std::cout << ans[i] << "\n\n"[i == n - 1];
    }

    return 0;
}

详细

Subtask #1:

score: 18
Accepted

Test #1:

score: 18
Accepted
time: 2ms
memory: 3916kb

input:

750 918
159 63 18
573 310 105
135 400 57
618 27 113
41 265 24
99 576 61
242 85 109
490 88 0
626 721 0
407 446 0
78 644 124
346 198 17
541 504 147
543 423 24
302 450 25
397 344 80
129 607 76
474 59 140
30 10 29
375 260 1
404 81 0
658 695 153
450 100 92
532 249 10
597 151 133
739 714 0
212 345 85
558 ...

output:

0
96
45
94
0
0
0
0
0
146
0
0
0
20
32
0
0
88
18
0
2
0
0
0
0
0
43
48
36
10
13
18
0
42
158
0
35
79
17
0
0
0
0
36
0
84
0
8
0
0
20
38
6
0
0
0
0
52
12
4
0
0
12
0
0
0
198
0
30
16
13
45
0
46
0
0
18
44
0
12
0
105
0
0
0
28
75
0
0
12
48
0
0
66
0
0
0
35
0
18
65
42
52
0
0
140
81
114
0
0
27
60
30
76
0
43
0
0
75
2...

result:

ok 750 lines

Test #2:

score: 18
Accepted
time: 2ms
memory: 4020kb

input:

967 334
285 176 1
648 431 1
493 893 2
261 165 1
158 512 2
675 881 1
232 200 2
452 380 2
961 35 1
831 131 1
424 458 1
807 835 2
154 855 1
524 513 2
448 27 1
82 331 2
454 190 2
469 210 2
15 445 2
860 1 2
901 47 0
496 572 2
227 122 1
141 223 2
214 924 1
4 381 2
394 703 2
859 611 2
63 688 1
197 674 1
59...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 967 lines

Test #3:

score: 18
Accepted
time: 2ms
memory: 3960kb

input:

1000 963
408 385 876
23 519 951
649 232 605
821 385 792
194 221 882
18 984 910
115 40 155
558 973 126
876 633 625
795 781 727
923 248 397
120 632 320
392 531 185
527 973 508
986 457 918
74 339 432
259 385 243
93 973 961
640 385 531
614 325 839
823 936 691
159 240 184
40 625 891
692 385 331
196 140 1...

output:

0
0
482612
0
912
0
0
766
0
0
0
24
989
0
493138
0
0
0
0
0
1996
979
890
1996
0
0
0
0
1996
0
0
0
0
0
0
0
254507
0
997
2
450646
0
244035
727
0
2043
0
0
0
0
0
0
0
0
0
0
0
0
19929
1996
0
0
0
0
997
0
0
0
0
1994
0
2
0
0
0
0
0
0
0
0
1962
0
0
0
0
3990
493900
0
0
0
0
0
0
3988
0
940
0
0
0
0
0
1996
0
2366
0
1989...

result:

ok 1000 lines

Test #4:

score: 18
Accepted
time: 3ms
memory: 4256kb

input:

1000 396
412 257 190
290 965 25
399 938 174
980 459 117
874 575 124
386 912 40
666 164 124
97 680 49
589 442 197
451 649 109
893 648 134
975 733 198
121 966 63
346 775 94
381 246 169
507 319 159
333 287 101
127 682 118
48 784 34
708 170 0
142 723 148
836 766 185
277 711 188
512 974 68
658 404 136
95...

output:

215160
138947
196491
47632
103775
128657
26093
195362
2694
0
234548
3952
120001
175260
489004
1998
9576
0
97602
2334
86553
18629
5792
57931
251700
122745
0
33115
227964
190106
203868
102592
1290
440
0
9054
79
1620
2166
496039
28992
169701
102833
9701
218081
1463
999
433548
7728
4935
882
817
87200
36...

result:

ok 1000 lines

Test #5:

score: 18
Accepted
time: 3ms
memory: 3924kb

input:

1000 333
896 853 0
28 756 0
658 183 0
488 17 0
475 283 0
587 6 0
979 607 0
918 95 0
247 848 0
799 23 0
815 685 0
580 58 0
724 192 0
398 209 0
140 526 0
966 397 0
595 468 0
827 19 0
866 666 0
62 329 0
599 799 0
13 972 0
526 481 0
593 170 0
312 809 0
106 584 0
670 945 0
602 62 0
765 731 0
756 935 0
33...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 1000 lines

Test #6:

score: 18
Accepted
time: 0ms
memory: 3644kb

input:

2 31
0 1 31

output:

0
0

result:

ok 2 lines

Test #7:

score: 18
Accepted
time: 2ms
memory: 3936kb

input:

1000 847
24 298 474
208 141 485
789 89 60
1 809 437
936 670 449
254 938 364
796 766 340
114 453 609
656 474 374
522 795 378
80 518 279
220 648 619
643 924 191
267 499 288
483 382 643
270 949 816
284 146 481
861 706 686
103 642 631
626 136 294
642 687 407
30 253 603
149 923 502
396 637 762
0 715 67
5...

output:

16
1080
1966
5980
1856
0
5980
0
0
0
0
0
0
0
0
1992
4702
0
0
968
1996
0
0
0
0
1996
0
0
58
0
61453
0
9874
64
3990
41198
11458
13818
1952
59099
17820
1
0
0
0
0
50596
0
0
0
0
0
0
37219
1215
0
7960
1996
175610
3990
0
31526
21
3988
1996
0
0
8953
11926
0
6980
1302
0
23237
0
0
0
0
0
0
0
1996
0
0
0
0
1996
19...

result:

ok 1000 lines

Test #8:

score: 18
Accepted
time: 2ms
memory: 3872kb

input:

1000 767
476 799 361
271 239 215
447 941 269
219 664 600
904 6 264
154 82 98
711 836 437
7 75 381
922 557 741
61 413 505
87 839 548
389 87 504
634 81 393
428 214 8
812 243 511
492 17 761
765 29 129
617 879 355
251 267 554
8 21 380
505 443 235
427 97 416
678 666 313
342 93 397
145 614 179
991 113 322...

output:

1017
997
3988
1996
0
0
2
0
0
971
1996
1996
0
2983
0
1996
19530
13742
0
0
13888
5015
0
997
0
0
0
0
0
996
0
2
0
78
1996
0
0
0
7948
1
6
0
0
1996
3
0
0
0
0
997
1996
0
0
75588
51097
1996
3988
0
0
0
40284
91174
1996
0
0
0
0
26
11
5976
112650
1996
1996
0
0
12837
0
31468
0
0
0
2967
159
0
9940
0
0
40883
1000...

result:

ok 1000 lines

Test #9:

score: 18
Accepted
time: 2ms
memory: 4124kb

input:

1000 1000
574 351 479
444 634 559
531 70 113
180 828 194
123 521 790
295 646 79
539 462 598
362 736 541
673 23 185
363 906 34
859 258 777
921 726 216
504 760 725
787 64 629
314 245 98
412 361 306
636 841 287
40 818 247
674 267 783
838 903 50
175 509 177
96 46 8
56 775 113
117 762 45
511 718 69
527 4...

output:

17839
4922
3988
0
0
0
0
0
0
0
1996
1996
0
0
0
0
0
2938
958
0
0
0
0
0
0
33
0
0
1097
50763
11918
0
0
4931
4942
0
0
0
1996
0
0
3988
1996
26430
0
3
997
15
0
0
0
61525
11
2971
1974
7960
1996
7783
0
0
0
11
17
4927
3994
0
0
3957
2968
8947
986
0
0
0
0
0
0
0
0
0
5976
0
2985
0
0
0
5974
6879
40013
45110
0
1872...

result:

ok 1000 lines

Test #10:

score: 18
Accepted
time: 2ms
memory: 3812kb

input:

1000 1000
680 881 340
73 368 929
303 239 219
861 605 561
243 667 531
357 96 797
561 264 569
267 59 169
146 639 660
161 931 423
209 525 524
406 689 275
544 504 153
296 406 735
245 144 706
806 165 557
444 234 477
614 437 682
503 628 205
539 636 135
219 202 863
938 665 376
983 452 512
841 241 924
664 9...

output:

88
3990
9954
0
0
1996
8458
0
0
5974
0
0
0
0
9952
2
9954
0
1996
6915
978
1996
989
0
31481
1996
157
1996
0
0
988
1996
0
17140
7960
0
1919
928
268
0
0
0
0
0
0
0
0
14876
0
1996
43
59135
983
0
0
0
0
0
1996
76302
0
0
4592
5976
2
0
0
0
0
43971
990
0
0
0
0
0
3972
1996
0
0
1915
0
1994
1996
997
0
0
0
138
0
0
...

result:

ok 1000 lines

Test #11:

score: 18
Accepted
time: 2ms
memory: 4176kb

input:

1000 1000
164 378 963
934 141 358
866 915 598
341 220 55
598 757 654
149 668 202
215 502 818
439 618 693
55 873 554
927 190 748
976 487 779
402 411 89
732 990 641
557 432 734
515 574 638
491 907 217
217 556 332
483 614 81
25 106 975
245 635 724
902 208 219
211 722 209
390 738 587
748 602 338
216 14 ...

output:

1996
0
1147
1996
18739
2976
0
0
0
0
0
1995
3990
0
13587
0
0
5982
1
0
997
0
1996
1996
0
0
0
0
2953
1994
1996
16
664
0
0
0
0
1996
30
3986
0
6932
3988
173897
1358
0
8907
0
0
0
1996
0
0
0
1996
1996
0
0
11938
1163
136386
1996
0
411
0
0
0
0
7903
29433
9084
3988
0
331
9940
23743
1996
0
0
3957
0
0
0
991
0
0...

result:

ok 1000 lines

Test #12:

score: 18
Accepted
time: 1ms
memory: 4036kb

input:

1000 537
276 155 470
533 155 391
175 155 343
553 155 270
24 155 447
614 155 239
927 155 314
899 155 188
398 155 344
18 155 501
937 155 413
671 155 184
808 155 418
200 155 501
491 155 374
717 155 245
692 155 476
313 155 455
0 155 232
702 155 275
544 155 498
66 155 237
245 155 233
865 155 188
574 155 ...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 1000 lines

Subtask #2:

score: 8
Accepted

Test #13:

score: 8
Accepted
time: 0ms
memory: 3648kb

input:

2 1
0 1 1

output:

0
0

result:

ok 2 lines

Test #14:

score: 8
Accepted
time: 468ms
memory: 27500kb

input:

70000 1
50913 18377 1
33894 11911 1
61940 7863 1
61602 33470 1
26341 35575 1
30813 50301 1
2994 67214 1
38527 61714 1
37381 3396 1
43274 14867 1
59352 12066 1
68877 40750 1
44756 43671 1
57921 17977 1
10879 47719 1
26878 13556 1
27329 23697 1
45109 11513 1
21307 12312 1
27202 27807 1
7250 14810 1
27...

output:

2128187656
1918647796
1539693556
1198079316
2227641388
537651676
1566795636
1713609688
462341800
403913520
1372196836
2449371376
2448661176
226085260
2289814488
2374543080
1462250988
2446901740
2288343736
788226400
1802397916
2219170356
2367201616
130103388
1927347880
2324936140
630135880
1489088716...

result:

ok 70000 lines

Subtask #3:

score: 10
Accepted

Dependency #2:

100%
Accepted

Test #15:

score: 10
Accepted
time: 507ms
memory: 28952kb

input:

70000 517272873
57335 18148 62837135
28239 56484 253183094
23004 59130 129215861
558 17489 52424960
55884 27928 150784869
35390 18538 216587635
31618 4121 168195328
49409 13677 142007449
61001 39616 40892641
67336 21612 185484704
34486 9556 246576628
4933 23924 201603991
57035 5529 29829254
38668 21...

output:

450859
215263283
544492560
222149
1276050
1993840
179130
3145479
1061520
186918
436257
128366615
192824
0
190234048
88612
216212
1174022
0
1203363128
455706327
343283
436439022
417240
1223229612
182003220
739660714
325508658
0
1222504809
1192350690
0
507280212
444391111
0
866072172
564047
0
90962902...

result:

ok 70000 lines

Test #16:

score: 10
Accepted
time: 496ms
memory: 40992kb

input:

70000 611016790
21272 16063 50360
30758 33429 30642
23317 5625 9045
66335 5731 24130
36096 15843 18089
60665 41152 19640
6148 39003 53047
42150 9859 46839
5191 59681 3924
53733 53709 36514
50103 15977 57464
40309 10907 5473
38449 24104 14928
69191 4445 31512
57129 33963 47456
14993 17284 40793
34588...

output:

69999
102210
23584
30220
0
0
111532
0
0
90100
0
69120
140466
49221
1132
98672
0
16653
60795
0
33401
15030
0
139998
6985
0
80680
0
0
96782
0
3500
19900
0
80444
52863
31767
84426
0
34538
11825
25587
187296
0
78744
36540
0
0
102247
0
71640
100900
58416
0
10056
165995
44813
21184
154679
0
52136
20348
0
...

result:

ok 70000 lines

Subtask #4:

score: 12
Accepted

Test #17:

score: 12
Accepted
time: 312ms
memory: 24004kb

input:

69973 4
44281 27162 1
15299 61302 1
19250 66379 1
45970 65938 1
23683 4445 2
62232 40589 1
37028 58991 2
58769 32024 0
41860 69672 2
14687 10058 2
7874 6780 2
60579 37047 2
739 4096 2
53137 22114 2
35060 21464 0
42597 11591 2
68051 23473 2
61458 35690 2
38719 6601 2
57406 26025 1
38192 41521 0
47941...

output:

769608
34960
1162375
626228
2
419792
493364
419754
1817301
15
838740
515085
769584
0
0
659707
915948
209895
208656
69972
1251384
277968
1231290
517455
825285
560784
417892
349256
489692
979020
139930
139938
209902
1518040
1146693
287344
403088
1
279861
209896
699603
732904
723193
769575
140252
10391...

result:

ok 69973 lines

Test #18:

score: 12
Accepted
time: 502ms
memory: 28940kb

input:

70000 6
12580 20937 2
31244 33335 1
62095 66946 0
2558 64306 2
38762 111 2
64974 13699 1
43807 54819 1
20265 66474 3
56426 21940 0
17487 26140 2
11417 36996 2
640 46428 0
45138 54845 1
50554 13418 3
66554 43463 0
54345 51628 2
50263 55697 1
11292 25053 1
29900 10115 2
54500 17279 1
54173 57033 3
608...

output:

95678
275287413
81937227
47960445
176569
64768
441678666
980093268
490395803
549945
2442380991
572294079
0
1210579335
0
23682745
0
901519414
203750140
923453439
104544
293252454
0
514830
915152602
1023643716
710424
0
1138664466
696145940
2080089300
0
0
589624
1220178831
51949
2340422751
193419
90424...

result:

ok 70000 lines

Test #19:

score: 12
Accepted
time: 490ms
memory: 41120kb

input:

70000 10
45546 20793 0
44801 49720 0
54732 9736 0
64375 18647 0
5128 61973 0
43544 39499 0
7553 64059 0
52671 60698 0
3856 66222 0
22937 53710 0
37212 17625 0
4088 60067 0
4594 67707 0
43329 36060 0
16713 10870 0
26623 56853 0
52301 6112 0
4772 42600 0
30359 51840 0
15979 7826 0
18168 47506 0
25681 ...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 70000 lines

Test #20:

score: 12
Accepted
time: 0ms
memory: 3628kb

input:

2 6
0 1 6

output:

0
0

result:

ok 2 lines

Test #21:

score: 12
Accepted
time: 307ms
memory: 21764kb

input:

70000 10
36906 67900 2
51465 24882 0
65531 32406 0
49018 50640 10
42030 45698 6
69965 51752 0
25375 7403 6
45919 32863 10
10559 50362 9
61538 32462 10
4894 61305 8
12033 10357 2
13580 31068 4
36014 69454 9
58156 22466 10
64024 60212 2
9152 1600 3
16154 3971 7
37873 60499 4
27813 67775 4
34467 20550 ...

output:

0
0
1141061037
69997
0
0
419980
17435495
279988
0
0
0
7973500
0
70179
0
7694762
70276
0
0
0
0
0
559960
0
2659276
0
69925
0
217273336
0
0
0
0
839916
69996
699940
140005
839839
2868360
0
1679688
139996
139996
139992
0
0
70000
1671612030
0
0
1049703
2869077
0
0
419980
2659240
1119845
0
1539768
0
279990...

result:

ok 70000 lines

Test #22:

score: 12
Accepted
time: 323ms
memory: 21892kb

input:

70000 10
40966 26929 6
15381 7596 3
53090 61576 3
6976 65087 2
33827 1931 10
24176 10778 3
43080 33467 2
48511 56217 5
59872 11525 0
18555 36157 1
16496 55674 5
28719 22937 6
52693 5381 8
36164 21449 3
37134 19737 7
27759 33516 7
30601 40021 1
62084 45831 10
42930 25137 2
26293 21275 9
48210 36338 6...

output:

279988
0
0
56174160
0
0
23323888
0
2099071
0
2659455
419980
139994
559960
139990
0
69993
139992
84379456
0
0
70128
0
1819660
7756014
38765430
0
209987
3358890
4617812
419976
139996
419970
1399520
6715296
0
2
139764
0
979888
0
139996
3638596
244188220
0
0
279990
0
279988
0
209964
209964
0
0
0
139992
...

result:

ok 70000 lines

Test #23:

score: 12
Accepted
time: 323ms
memory: 21888kb

input:

70000 10
47111 32841 10
510 5994 10
1362 44478 8
61688 30984 5
30611 58931 7
23371 48692 10
53702 58334 5
45242 64087 9
62520 46584 7
18984 64865 8
19484 69056 6
67745 9899 8
64611 28448 7
28716 36915 4
28330 65755 5
23587 61603 3
2434 30883 6
22277 19653 8
68776 65367 2
6483 42009 10
21878 32298 5
...

output:

419980
0
979765
0
1679710
139996
419872
139996
699954
0
3569329
0
11462313
3778488
0
559934
1889317
140023
5592560
0
0
0
489930
0
2308713
139996
0
1889620
2659240
1399812
0
139996
0
0
0
1539193
3
0
139996
0
3707927
2659240
0
559929
4056462
0
1049593
0
0
893806
139996
1
139996
279988
349971
0
0
16493...

result:

ok 70000 lines

Subtask #5:

score: 17
Accepted

Dependency #4:

100%
Accepted

Test #24:

score: 17
Accepted
time: 224ms
memory: 22976kb

input:

70000 7
12257 45873 7
53771 24407 7
67182 59338 2
68981 59097 6
57729 48126 5
1721 59097 2
3199 59097 3
60691 12829 0
58194 66702 5
33633 67496 7
58727 15878 1
17536 52190 3
50970 48111 4
29987 50799 4
13575 50151 3
60967 6663 2
47883 59097 3
6045 45021 4
30432 47041 3
42998 48111 7
30822 48111 3
11...

output:

0
0
0
0
56749
139996
0
0
2273252787
0
0
0
0
0
0
0
0
0
58877
0
139996
0
0
419976
0
0
2225944862
0
2420326158
0
0
0
559960
0
0
0
643497
0
139996
0
0
139996
0
0
0
0
0
0
0
279988
0
17043
0
0
60317
32887
0
0
2264012956
0
0
0
0
0
0
0
0
139996
349939
0
0
0
0
0
0
0
0
0
0
0
0
0
0
3
144979
0
0
0
0
0
0
2441897...

result:

ok 70000 lines

Test #25:

score: 17
Accepted
time: 225ms
memory: 22020kb

input:

70000 7
60987 29710 2
40235 14667 3
49803 36218 0
1256 23603 0
9202 12636 0
67422 31567 1
54648 38680 2
62637 37872 2
10743 6822 6
53418 48397 1
51738 49375 3
19681 19949 3
36445 50369 3
19712 38063 3
26633 52347 0
23152 59256 2
10977 15333 5
65517 1515 5
25199 50076 6
3371 68446 2
10983 59644 2
684...

output:

559942
0
69996
0
0
139996
0
0
0
139996
1119890
0
0
559934
0
0
139996
0
0
0
0
0
69999
279988
419976
0
0
279988
559966
0
1399780
2099472
0
0
0
0
0
0
0
0
0
139983
0
0
0
0
0
0
0
0
0
0
209973
0
559960
0
0
699940
0
0
0
69996
69996
0
279990
21314538
279988
0
0
419956
0
0
0
0
0
0
69997
559960
0
2
1418
13999...

result:

ok 70000 lines

Test #26:

score: 17
Accepted
time: 231ms
memory: 22276kb

input:

70000 4
29827 12474 3
52717 68608 1
26411 5022 3
66140 68360 2
27054 15938 3
383 51864 4
52370 63324 3
49124 25159 4
42556 47177 1
59874 25843 0
18502 25342 2
7574 23971 4
35758 28101 2
14031 55753 2
50487 50114 4
33386 45120 1
33450 31191 4
695 3466 0
3269 56502 1
67089 56967 0
56892 30196 1
5116 5...

output:

419970
0
0
202
0
0
279984
0
0
280018
139996
349980
139996
0
23
139996
279988
139996
139996
0
559947
209963
0
0
0
0
0
0
0
0
1539806
0
0
0
139996
0
2440548
279988
699954
279988
0
0
2379703
0
0
2309506
0
209979
3987926
0
419976
979648
0
0
0
0
0
0
279988
0
13
0
0
139996
0
0
279988
0
0
139996
139996
0
36...

result:

ok 70000 lines

Test #27:

score: 17
Accepted
time: 220ms
memory: 21840kb

input:

70000 10
41642 65096 7
14235 28073 6
48132 705 7
23384 33897 0
13953 22709 0
27316 52036 3
26123 8861 10
3391 7044 10
1747 10038 10
8948 54554 8
6958 62827 0
58607 65096 1
28063 67423 3
31692 57358 5
62966 15760 8
66029 36119 0
19762 15328 5
65551 25282 10
66353 19368 5
58541 32650 10
53625 52235 1
...

output:

139996
279988
0
0
0
1399787
0
0
0
0
0
0
0
0
69996
0
0
0
0
139996
769901
0
0
0
141
2659434
139996
0
0
0
232
0
0
0
32
0
0
0
0
0
0
69997
0
70008
5527464
209942
699954
279986
1189644
839914
0
139974
0
28903966
0
0
0
0
4548535
0
699942
0
101
0
0
0
0
0
0
0
2519482
0
0
0
0
0
0
69992
0
0
139996
9089021
0
0
...

result:

ok 70000 lines

Test #28:

score: 17
Accepted
time: 222ms
memory: 21984kb

input:

70000 10
44002 46661 10
16351 40057 10
62342 69569 2
277 38372 1
40658 58988 7
15832 23305 8
39381 63632 8
55316 55953 2
4871 40228 6
57312 52025 0
24200 29040 9
32283 24421 9
65203 10297 10
28846 27662 6
46750 32115 8
55937 947 4
32984 11601 10
57523 29674 5
35949 53510 9
56657 8539 8
43913 29333 4...

output:

0
0
559966
0
0
0
0
1259820
769916
0
279988
0
0
69954
0
0
279978
839920
139996
0
139982
139972
0
0
0
139916
139992
769903
139996
193
70134
1049708
0
0
0
839934
0
0
0
0
0
0
139996
839926
699956
0
0
0
419980
840675
139996
0
69995
0
0
0
0
11812190
0
279980
8
0
0
0
139996
0
0
0
0
0
0
0
945265
0
0
0
13999...

result:

ok 70000 lines

Test #29:

score: 17
Accepted
time: 221ms
memory: 21872kb

input:

70000 10
11173 32861 3
13926 50906 10
18305 60850 3
13279 836 9
3466 26666 4
67810 66996 8
67739 2252 2
14556 38643 7
68973 45279 0
32154 29076 10
46391 19237 3
10836 54751 1
23291 45139 6
45366 68190 0
41164 61188 3
7519 11337 6
30900 58457 7
10220 41616 2
37816 59990 7
42226 8216 4
45323 64040 4
2...

output:

0
139996
2
0
0
0
139994
0
15575
69993
0
0
0
0
0
0
0
0
0
2325854
0
0
139994
0
0
17191
279988
0
0
0
1539632
141441
0
0
0
2659336
0
0
1119870
139996
489943
279988
0
0
0
139996
559968
0
139974
0
50059
0
0
209737
279988
419978
0
15101910
1259876
559960
0
909802
1679722
0
0
0
0
0
0
0
3
28485088
0
3918
489...

result:

ok 70000 lines

Test #30:

score: 17
Accepted
time: 113ms
memory: 31548kb

input:

70000 3
18748 12670 2
56209 12670 2
4918 12670 1
26713 12670 2
66342 12670 1
35963 12670 3
41257 12670 3
32775 12670 2
623 12670 3
32333 12670 1
15905 12670 1
63177 12670 3
25408 12670 3
44826 12670 2
28656 12670 2
14547 12670 2
68707 12670 1
12229 12670 1
41434 12670 2
58731 12670 3
1018 12670 1
63...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 70000 lines

Subtask #6:

score: 35
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

100%
Accepted

Dependency #5:

100%
Accepted

Test #31:

score: 35
Accepted
time: 316ms
memory: 30496kb

input:

67845 886519666
14071 38244 390226
23927 10508 4649507
24776 60617 7933069
44979 29276 6727041
33086 57439 9224422
60471 59570 10200895
64064 45838 1032638
50024 498 1872835
35801 18255 52202
18028 63586 3630156
39462 7379 4744904
28195 576 1919586
48018 39366 2477126
11216 62638 8490883
50132 37368...

output:

43
0
395
0
81
0
195
39
57
49
28
0
0
1383
10
8
0
320
24
17
98
40
1224
80
192
546
0
96
0
54
0
32
16
0
44
440
75
0
22
0
0
112
4104
66
0
0
0
0
0
2065
0
0
238
837
0
0
37
34
0
0
0
0
42
0
0
221
0
36
36
0
31
456
49
348
0
120
1902
21
100
0
70
536
80
0
60
36
48
0
26
0
45
0
154
5990
24
0
2786
51
160
536
84
30
...

result:

ok 67845 lines

Test #32:

score: 35
Accepted
time: 242ms
memory: 22948kb

input:

70000 598182175
35387 18369 177725639
34272 27820 390474503
14996 60451 566274308
31897 35730 516503530
43234 29256 108892895
55368 29256 4205483
64642 60451 13033917
37509 14330 363633896
66482 29256 478291597
59155 29256 244567086
52534 60985 99539570
5615 14339 372929621
50593 60451 64994714
6672...

output:

0
0
0
2373343036
2449703732
9462
0
0
0
0
0
0
0
0
0
2293251192
2427411240
2219708062
0
0
0
5390
139996
0
0
0
0
1169621032
0
0
0
626357
0
172747
173640
0
50118
142495
0
8326
99783
0
0
279990
2442230316
139996
0
0
0
64005
963
139996
0
141887
279990
699940
2391577596
279988
0
0
0
0
0
205013
0
0
4
0
0
0
...

result:

ok 70000 lines

Test #33:

score: 35
Accepted
time: 229ms
memory: 21892kb

input:

70000 307114024
18 68382 208881990
51105 51287 133487858
6935 48425 203987749
30063 35675 238707761
28765 22485 49927814
57136 47500 222282370
25828 39678 182524508
30496 59138 173337525
37509 2682 83786472
9206 5603 3337969
38207 4975 245900852
25506 59625 144882927
33324 45776 115053268
21145 1854...

output:

0
5457705
0
420055
0
0
139996
0
76517
0
559960
3828943
0
0
279988
0
0
0
349979
0
0
279990
0
0
0
5596822
0
279984
0
839926
420550
139996
0
0
419980
419974
0
280206
0
0
70010
0
0
0
0
0
0
0
0
139996
0
0
0
279988
0
0
0
0
699928
139970
0
699870
651915
0
0
0
0
1
0
0
0
0
0
0
0
0
139996
0
139996
279988
2799...

result:

ok 70000 lines

Test #34:

score: 35
Accepted
time: 234ms
memory: 21884kb

input:

70000 260495634
10465 32381 229572819
59714 8684 92922283
44269 63680 232833764
47600 15978 126514599
56076 11569 220987907
1736 42372 92835861
46416 16634 39101797
1967 47552 42632625
28258 45972 192563970
993 57564 180063629
13335 45950 247895986
66609 3434 198979633
33507 63227 72307825
27481 787...

output:

350035
839810
1
0
477281
0
0
5116592
2867966
70127
2519460
419954
419972
139996
139996
0
0
0
0
0
69999
139996
35
139996
0
0
279984
699917
629967
69997
0
69995
0
979889
277388
0
559962
0
0
0
0
0
0
0
0
0
0
0
0
419976
0
139996
0
0
0
0
0
279988
559960
0
0
139990
559962
26197202
0
0
69997
1119861
699956
...

result:

ok 70000 lines

Test #35:

score: 35
Accepted
time: 222ms
memory: 27968kb

input:

70000 1000000000
40606 60962 166792
66689 16700 510579
39392 42623 271902
12827 150 783709
44103 27953 824954
8448 53818 505627
20836 62192 635162
62760 4963 417791
55360 16542 503766
28548 1019 22455
47848 12546 99692
6766 56528 128351
29490 37993 612611
18543 37342 545846
68824 23738 382529
16876 ...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 70000 lines

Test #36:

score: 35
Accepted
time: 229ms
memory: 28360kb

input:

70000 1000000000
18028 63559 764765
43037 44898 469661
22315 22254 359926
32987 69384 314936
64614 7836 286346
56865 18837 769512
47779 45708 844130
19917 69179 872775
30813 55358 439985
63706 27808 677953
65342 11878 780726
13796 46033 639535
55991 57078 269892
50311 39721 483616
38327 40537 762381...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 70000 lines

Test #37:

score: 35
Accepted
time: 243ms
memory: 28348kb

input:

70000 1000000000
48195 15915 457746
7436 2784 442186
9122 3611 587852
31354 12943 476363
27748 46799 959092
28199 27753 940620
8197 25757 995144
18019 39964 421557
67894 68441 866331
31451 11008 215283
16791 62360 400435
57053 23688 911977
19869 58988 452272
59307 11630 755710
45424 2463 811083
3448...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 70000 lines

Test #38:

score: 35
Accepted
time: 132ms
memory: 31268kb

input:

70000 852351984
54341 29527 743304598
55902 29527 379127182
8584 29527 326761145
21272 29527 850588761
23040 29527 358744261
38268 29527 574089553
20315 29527 586740647
62544 29527 748613800
68188 29527 416939120
69114 29527 648304193
44074 29527 529602293
67234 29527 577216443
69703 29527 449985306...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 70000 lines

Extra Test:

score: 0
Extra Test Passed