QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#531338#9165. Petrol stationsmakrav#0 186ms35928kbC++207.4kb2024-08-24 20:00:252024-08-24 20:00:26

Judging History

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

  • [2024-08-24 20:00:26]
  • 评测
  • 测评结果:0
  • 用时:186ms
  • 内存:35928kb
  • [2024-08-24 20:00:25]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define pb push_back

mt19937 rnd(time(NULL));

struct node {
    int val, val2, v, prior, sum;
    node* l = nullptr, *r = nullptr;
    node() = default;
    node(int vl_, int vl2, int v_) {
        val = vl_;
        val2 = vl2;
        v = v_;
        prior = rnd();
        sum = val2;
        l = nullptr;
        r = nullptr;
    }
};  

struct cartesiantree {
    node* root = nullptr;
    cartesiantree() = default;

    int sum(node* root) {
        return (root == nullptr ? 0 : root->sum);
    }

    void upd(node* root) {
        if (root == nullptr) return;
        root->sum = root->val2 + sum(root->l) + sum(root->r);
    }

    pair<node*, node*> split(node* v, int x, int y, int V) {
        if (v == nullptr) return {nullptr, nullptr};
        if (v->val < x || (v->val == x && v->val2 < y) || (v->val == x && v->val2 == y && v->v <= V)) {
            auto rs = split(v->r, x, y, V);
            v->r = rs.first;
            upd(v);
            return {v, rs.second};
        } else {
            auto rs = split(v->l, x, y, V);
            v->l = rs.second;
            upd(v);
            return {rs.first, v};
        }
    }

    node* merge(node* a, node* b) {
        if (a == nullptr) return b;
        if (b == nullptr) return a;
        if (a->prior < b->prior) {
            node* x = merge(a->r, b);
            a->r = x;
            upd(a);
            return a;
        } else {
            node*x = merge(a, b->l);
            b->l = x;
            upd(b);
            return b;
        }
    }

    node* insert(node* v, node* nw) {
        auto [r1, r2] = split(v, nw->val, nw->val2, nw->v);
        return merge(merge(r1, nw), r2);
    }

    node* erase(node* v, int val, int val2, int vt) {
        auto [r1, r2] = split(v, val, val2, vt - 1);
        auto [r3, r4] = split(r2, val, val2, vt);
        return merge(r1, r4);
    }

    int getcnt(int l, int r) {
        auto [r1, r2] = split(root, l - 1, 1e9, 1e9);
        auto [r3, r4] = split(r2, r, 1e9, 1e9);
        int ans = sum(r3);
        root = merge(r1, merge(r3, r4));
        return ans;
    }
};

void solve() {
    int n, k; cin >> n >> k;
    vector<vector<pair<int, int>>> g(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v, w; cin >> u >> v >> w;
        g[u].pb({v, w});
        g[v].pb({u, w});
    }

    vector<int> h(n), siz(n), dp(n), tin(n), tout(n);
    vector<ll> ans(n);
    vector<vector<int>> sub(n);
    vector<cartesiantree> ct(n);

    vector<vector<int>> up(18, vector<int>(n));
    int tim = 0;
    auto dfs1 = [&](int v, int p, int inw, auto&&dfs1) -> void {
        tin[v] = tim++;
        up[0][v] = p;
        for (int i = 1; i < 18; i++) {
            up[i][v] = up[i - 1][up[i - 1][v]];
        }
        siz[v] = 1;
        int sons = 0;
        for (auto &[u, w] : g[v]) {
            if (u != p) {
                h[u] = h[v] + w;
                sons++;
                dfs1(u, v, w, dfs1);
                siz[v] += siz[u];
            }
        }
        if (!sons) {
            sub[v] = {h[v]};
            dp[v] = 0;
            node* nw = new node(h[v], 1, v);
            ct[v].root = ct[v].insert(ct[v].root, nw);
            return;
        }
        int hs = -1;
        for (auto &[u, w] : g[v]) {
            if (u != p && (hs == -1 || siz[hs] < siz[u])) {
                hs = u;
            }
        }
        swap(sub[hs], sub[v]);
        swap(ct[hs], ct[v]);
        for (auto &[u, w] : g[v]) {
            if (u != p && u != hs) {
                for (auto &vt : sub[u]) {
                    sub[v].pb(vt);
                    node* nw = new node(h[vt], dp[vt] + 1, vt);
                    ct[v].root = ct[v].insert(ct[v].root, nw);
                }
            }
        }
        dp[v] = ct[v].getcnt(k - inw + h[v] + 1, k + h[v]);
        ans[v] += dp[v] * 1ll * (n - siz[v]);
        node* cur = new node(h[v], dp[v] + 1, v);
        ct[v].root = ct[v].insert(ct[v].root, cur);
        tout[v] = tim++;
    };  

    auto is_par = [&](int u, int v) {
        return tin[u] <= tin[v] && tout[v] <= tout[u];
    };

    auto lca = [&](int u, int v) {
        if (is_par(u, v)) return u;
        if (is_par(v, u)) return v;
        for (int i = 17; i >= 0; i--) {
            if (!is_par(up[i][v], u)) v = up[i][v];
        }
        return up[0][v];
    };

    auto dist = [&](int u, int v) {
        return h[u] + h[v] - 2 * h[lca(u, v)];
    };

    auto dfs2 = [&](int v, int p, vector<int>&hp, cartesiantree &ctup, int addit_up, int inw, auto&&dfs2) -> void {
        int cc = -1;
        if (v != 0) {
            int curcnt = ctup.getcnt(k + 1 - addit_up, k + inw - addit_up);
            for (auto &u : hp) {
                int D = dist(p, u);
                curcnt += ct[u].getcnt(k - inw + 1 - D + h[u], k - D + h[u]);
            }
            ans[p] += curcnt * 1ll * siz[v];
            cc = curcnt;
            node* nw = new node(-addit_up + inw, curcnt +  1, p);
            ctup.root = ctup.insert(ctup.root, nw);
            //cout << "add " << -addit_up + inw << ' ' << curcnt + 1 << '\n';
        }
        int bs = -1, sons = 0, bsw = -1;
        for (auto &[u, w] : g[v]) {
            if (u != p) {
                sons++;
                if (bs == -1 || siz[bs] < siz[u]) {bs = u; bsw = w; }
            }
        }  
        if (!sons) {
            return;
        }
        node* nw = new node(-addit_up, 1, v);
        swap(ct[v], ct[bs]);
        for (auto &[u, w] : g[v]) {
            if (u != p && u != bs) {
                for (auto &vt : sub[u]) {
                    ct[bs].root = ct[bs].erase(ct[bs].root, h[vt], dp[vt] + 1, vt);
                    node* nw = new node(h[vt] - addit_up, dp[vt] + 1, vt);
                    ctup.root = ctup.insert(ctup.root, nw);
                }
            }
        }
        hp.pb(bs);
        for (auto &[u, w] : g[v]) {
            if (u != p && u != bs) {
                for (auto &vt : sub[u]) {
                    ctup.root = ctup.erase(ctup.root, h[vt] - addit_up, dp[vt] + 1, vt);
                }
                dfs2(u, v, hp, ctup, addit_up + w, w, dfs2);
                for (auto &vt : sub[u]) {
                    node* nw = new node(h[vt] - addit_up, dp[vt] + 1, vt);
                    ctup.root = ctup.insert(ctup.root, nw);
                }
            }
        }
        hp.pop_back();
        dfs2(bs, v, hp, ctup, addit_up + bsw, bsw, dfs2);
        for (auto &[u, w] : g[v]) {
            if (u != bs && u != p) {
                for (auto &vt : sub[u]) {
                    ctup.root = ctup.erase(ctup.root, h[vt] - addit_up, dp[vt] + 1, vt);
                }
            }
        }
        if (v != 0) {
            ctup.root = ctup.erase(ctup.root, -addit_up + inw, cc + 1, p);
        }
    };
    dfs1(0, 0, 0, dfs1);
    // for (auto &u : dp) cout << u << ' ';
    // cout<<'\n';
    vector<int> hp;
    cartesiantree ctup;
    dfs2(0, 0, hp, ctup, 0, 0, dfs2);

    for (auto &u : ans) cout << u << '\n';
}

signed main() {
    int tt = 1;
    #ifdef LOCAL 
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
        cin >> tt;
    #else 
        ios::sync_with_stdio(false); cin.tie(0); 
    #endif

    while (tt--) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 3960kb

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
16
5
14
0
0
0
0
0
23
0
0
0
2
4
0
0
11
2
0
0
0
0
0
0
0
4
6
8
0
0
3
0
0
21
0
8
12
0
0
0
0
0
6
0
3
0
0
0
0
2
2
0
0
0
0
0
10
0
0
0
0
2
0
0
0
36
0
6
2
2
6
0
6
0
0
3
4
0
0
0
16
0
0
0
2
10
0
0
0
4
0
0
7
0
0
0
10
0
0
9
3
5
0
0
24
6
18
0
0
3
6
0
6
0
9
0
0
0
4
1
0
0
0
0
10
6
24
8
11
14
11
2
0
0
0
2
0
0
0
3
...

result:

wrong answer 2nd lines differ - expected: '96', found: '16'

Subtask #2:

score: 0
Wrong Answer

Test #13:

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

input:

2 1
0 1 1

output:

0
0

result:

ok 2 lines

Test #14:

score: 0
Wrong Answer
time: 75ms
memory: 35928kb

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:

1064093828
2810386280
1539707222
1198089297
3393848976
732107028
1566809622
1713625500
462345275
403916535
1934200460
1656528528
1685387028
304954596
1309093236
1730503320
2071509456
1638393060
1306707528
788232575
2613312020
3375888828
1442627420
175021716
2825476020
1720306436
860688020
2112843120...

result:

wrong answer 1st lines differ - expected: '2128187656', found: '1064093828'

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Wrong Answer

Test #17:

score: 0
Wrong Answer
time: 186ms
memory: 28492kb

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:

1296
13294370
564791
353268
18928
1088716
140724
420105
5805
5905
354760
4920
146388
142742656
0
283694
495432
209895
16500544
152264
701880
171732
702750
4185
355017
353952
70972
176152
491244
356592
140336
139938
2654687390
842020
635058
1088
290339
12113
98538
85342
699954
142851
493717
146766
12...

result:

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

Subtask #5:

score: 0
Skipped

Dependency #4:

0%

Subtask #6:

score: 0
Skipped

Dependency #1:

0%