QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#641556#9165. Petrol stationsnot_amir8 1109ms827404kbC++145.0kb2024-10-14 21:11:162024-10-14 21:11:17

Judging History

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

  • [2024-10-14 21:11:17]
  • 评测
  • 测评结果:8
  • 用时:1109ms
  • 内存:827404kb
  • [2024-10-14 21:11:16]
  • 提交

answer

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

template<typename T>
using V = vector<T>;

typedef pair<int, int> pii;
typedef long long ll;

ll INF;

struct Seg {
    Seg *left = nullptr, *right = nullptr;
    int l, r, mid;
    int val;
    bool sparse = false;


    Seg(int l, int r) : l(l), r(r) ,mid((r + l) / 2) {

    }

    void push() {
        if (!sparse) {
            if (r - l > 1 ) {
                left = new Seg(l,mid);
                right = new Seg(mid,r);
            }
            sparse = true;
        }
    }

    void add(int x, int u) {
        push();
        if (r - l <= 1) {
            val += u;
            return;
        }

        if (x < mid)
            left->add(x,u);
        else
            right->add(x,u);
        val = left->val + right->val;
    }

    int q(int a, int b) {
        push();
        if (b <= l || a >= r) return 0;
        if (a <= l && b >= r) return val;

        return left->q(a,b) + right->q(a,b);
    }
};

vector<int> vals;
map<int, int> to;

struct SegPoint {
    Seg *seg;

    SegPoint() {
        seg = new Seg(0, 1e9);
        if (!to.empty()) return;
        std::sort(vals.begin(), vals.end());

        int id = 0;
        for (auto x : vals) {
            to[x] = id++;
        }
    }

    void add(int x, int u) {
        seg->add(x, u);
    }

    int q(int a, int b) {
        return seg->q(a,b);
    }
};

vector<vector<pii>> G;
vector<bool> is_removed;
vector<int> subsize;

int get_subtree_size(int v, int p = -1) {
    subsize[v] = 1;
    for (auto [u, w] : G[v]) {
        if (u == p || is_removed[u]) { continue; }
        subsize[v] += get_subtree_size(u, v);
    }
    return subsize[v];
}

int get_centroid(int v, int S, int p = -1) {
    for (auto [u, w] : G[v]) {
        if (u == p || is_removed[u]) { continue; }
        if (subsize[u] * 2 > S) {
            return get_centroid(u, S, v);
        }
    }
    return v;
}

int k;

vector<ll> ans, res, jumpw, parw;
vector<int> depth, jump, par;

vector<SegPoint*> st;

void dfs1(SegPoint* stt, int v, int p, int d, int child, int S) {
    depth[v] = d;
    res[v] = 0;
    if(p != -1) {
        if(depth[jump[p]] - depth[p] == depth[jump[jump[p]]] - depth[jump[p]])
            jump[v] = jump[jump[p]], jumpw[v] = jumpw[jump[p]] + jumpw[p] + parw[v];
        else
            jump[v] = p, jumpw[v] = parw[v];
    }
    for(auto [u, w] : G[v]) {
        if(u == p || is_removed[u])
            continue;
        par[u] = v, parw[u] = w;
        if(p == -1)
            st[u] = new SegPoint();
        dfs1(stt, u, v, d + 1, p==-1?u:child, S);
    }

    if (p == -1) return;
    int a = v;
    ll s = 0;
    ans[v] += res[v] * (S - subsize[child]);

    while(jump[a] != a && s + parw[a] <= k) {
        if(jumpw[a] + s <= k)
            s += jumpw[a], a = jump[a];
        else
            s += parw[a], a = par[a];
    }

    if(jump[a] == a) {
        stt->add(k - s, res[v] + 1);
        st[child]->add(k - s, res[v] + 1);
    }
    else
        res[a] += res[v] + 1;
}

void dfs2(SegPoint* stt, int v, int p, ll s, int child) {
    for(auto [u, w] : G[v]) {
        if(u == p || is_removed[u])
            continue;
        if(p == -1) {
            child = u;
        }
        int refuel = stt->q(s, s + w) - st[child]->q(s, s + w);
        ans[v] += refuel * subsize[u];
        stt->add(s + k, refuel);
        dfs2(stt, u, v, s + w, child);
        stt->add(s + k, -refuel);
    }
}

void dfs0(int v, int p, ll s = 0){
    vals.push_back(s);
    for(auto [u, w] : G[v]){
        if(u == p || is_removed[u])
            continue;
        dfs0(u, v, s + w);
    }
}

void centroid_decomp(int v, bool pre) {
    int centroid = get_centroid(v, get_subtree_size(v));

    if(pre) {
        dfs0(centroid, -1);
    }
    else {
        SegPoint *stt = new SegPoint();

        jump[centroid] = centroid;
        jumpw[centroid] = 0;
        dfs1(stt, centroid, -1, 0, 0, get_subtree_size(centroid));
        stt->add( k, 1);
        dfs2(stt, centroid, -1, 0, 0);
    }

    is_removed[centroid] = true;

    for (auto [u, w] : G[centroid]) {
        if (is_removed[u]) { continue; }
        centroid_decomp(u, pre);
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n >> k;
    INF = ll(n) * ll(k);
    G.resize(n);
    is_removed.resize(n);
    subsize.resize(n);
    par.resize(n);
    parw.resize(n);
    jump.resize(n);
    jumpw.resize(n);
    ans.resize(n);
    res.resize(n);
    depth.resize(n);
    st.resize(n);
    for(int i = 0; i < n - 1; i++) {
        int u, v, l;
        cin >> u >> v >> l;
        G[u].push_back({v, l});
        G[v].push_back({u, l});
    }
    centroid_decomp(0, 1);
    is_removed.clear();
    is_removed.resize(n);
    centroid_decomp(0, 0);
    for(int i = 0; i < n; i++)
        cout << ans[i] << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 4ms
memory: 17856kb

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
-13
45
94
0
0
0
0
0
37
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
-190
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
-142
0
43
0
0
...

result:

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

Subtask #2:

score: 8
Accepted

Test #13:

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

input:

2 1
0 1 1

output:

0
0

result:

ok 2 lines

Test #14:

score: 8
Accepted
time: 1109ms
memory: 827404kb

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: 0
Memory Limit Exceeded

Dependency #2:

100%
Accepted

Test #15:

score: 0
Memory Limit Exceeded

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:

31996174
212731760
544342103
78682967
-1766523640
2115191
71104225
14160956
1329189
451908
1347466992
453415857
-184045846
0
1323261539
89295
216212
1173878
-1788799608
1104003345
-1741551876
343298
2328548724
407801469
42487051
440836861
2169602445
162351093
9820
1560884436
-621521334
56401374
8120...

result:


Subtask #4:

score: 0
Wrong Answer

Test #17:

score: 0
Wrong Answer
time: 880ms
memory: 757444kb

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:

770460
34955
1162358
626724
44
420104
492704
419962
1815899
210
836332
515625
769116
-2
0
658992
915900
209930
208960
69912
1250040
277988
1231305
518190
825798
561192
417652
349212
489580
978804
139944
139896
209992
1518135
1146963
287840
403781
-7
279855
209860
699603
733499
723284
769368
140408
1...

result:

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

Subtask #5:

score: 0
Skipped

Dependency #4:

0%

Subtask #6:

score: 0
Skipped

Dependency #1:

0%