QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#498204#9165. Petrol stationsQwerty123218 645ms24348kbC++238.9kb2024-07-30 04:32:492024-07-30 04:32:49

Judging History

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

  • [2024-07-30 04:32:49]
  • 评测
  • 测评结果:18
  • 用时:645ms
  • 内存:24348kb
  • [2024-07-30 04:32:49]
  • 提交

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}) - 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, 0}}});
            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;
        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(0, 0, {});
            for (auto& c2 : f) {
                c = cum_merge(c, c2);
            }
            heap.push({{-sz[t], {ti}}, std::move(c)});
        }

        while (heap.size() > 1) {
            auto [p1, c1] = heap.top();
            heap.pop();
            auto [p2, c2] = heap.top();
            heap.pop();
            auto [sz1, ti1] = p1;
            auto [sz2, ti2] = 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);
            }

            Cum c = cum_merge(c1, c2);
            ti1.insert(ti1.end(), ti2.begin(), ti2.end());
            heap.push({{sz1 + sz2, ti1}, std::move(c)});
        }

        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;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

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

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: 3716kb

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: 0
Wrong Answer
time: 3ms
memory: 3892kb

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
468924
0
880
0
0
743
0
0
0
24
954
0
479386
0
0
0
0
0
1961
944
866
1969
0
0
0
0
1961
0
0
0
0
0
0
0
246915
0
997
2
439082
0
237303
705
0
1018
0
0
0
0
0
0
0
0
0
0
0
0
18831
1967
0
0
0
0
997
0
0
0
0
967
0
2
0
0
0
0
0
0
0
0
1904
0
0
0
0
3932
478668
0
0
0
0
0
0
2923
0
917
0
0
0
0
0
1961
0
1335
0
1958
...

result:

wrong answer 3rd lines differ - expected: '482612', found: '468924'

Subtask #2:

score: 8
Accepted

Test #13:

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

input:

2 1
0 1 1

output:

0
0

result:

ok 2 lines

Test #14:

score: 8
Accepted
time: 621ms
memory: 22896kb

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: 645ms
memory: 21932kb

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: 630ms
memory: 24348kb

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: 0
Wrong Answer

Test #17:

score: 0
Wrong Answer
time: 409ms
memory: 13068kb

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:

769548
34957
1162273
626148
2
419744
493284
419754
1817075
15
838614
514980
769476
0
0
659674
915888
209895
208640
69972
1251336
277952
1231215
517395
825231
560736
417872
349216
489692
978912
139930
139938
209878
1517964
1146588
287280
403060
1
279825
209874
699603
732870
723141
769476
140244
10388...

result:

wrong answer 1st lines differ - expected: '769608', found: '769548'

Subtask #5:

score: 0
Skipped

Dependency #4:

0%

Subtask #6:

score: 0
Skipped

Dependency #1:

0%