QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#303449#4924. 蜘蛛爬树zyc0704193 9ms42036kbC++147.7kb2024-01-12 15:44:162024-01-12 15:44:16

Judging History

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

  • [2024-01-12 15:44:16]
  • 评测
  • 测评结果:3
  • 用时:9ms
  • 内存:42036kb
  • [2024-01-12 15:44:16]
  • 提交

answer

#include <bits/stdc++.h>
#define INF 9e18
#define inf 1e9
#define ll long long
using namespace std;
const int N = 2e5 + 3;

inline int read() {
    char ch = getchar(); int x = 0;
    while (!isdigit(ch)) {ch = getchar();}
    while (isdigit(ch)) {x = x * 10 + ch - 48; ch = getchar();}
    return x;
}

inline ll Read() {
    char ch = getchar(); ll x = 0;
    while (!isdigit(ch)) {ch = getchar();}
    while (isdigit(ch)) {x = x * 10 + ch - 48; ch = getchar();}
    return x;
}

struct Node {
    int id, y, d;
    Node(int a = 0, int b = 0, int c = 0) {id = a; y = b; d = c;}
};
int n, m, T, a[N];
int depth[N], fa[N], sz[N], son[N], col[N];
int sum, root, mx[N];
ll dis[N], now[N], ans[N];
vector< pair<int, ll> > g[N];
bitset<N> vis;
vector<Node> vec[N], mem[N], nxt;
struct LC_Segment_Tree {
    struct node {
        int ls, rs, k; ll b;
        node() {ls = rs = k = 0; b = INF;}
    }t[N];
    // vector<node> t;
    int tot, fir;

    inline ll f(int x, int k, ll b) {return 1ll * k * x + b;}

    void update(int &rt, int l, int r, int k, ll b) {
        if (!rt) {
            rt = ++tot;
            // t.push_back(node());
            // cerr << rt << ' ' << t.size() << '\n';
            // if (tot >= t.size()) puts("qwq"), exit(0);
            t[tot].k = k; t[tot].b = b;
            return;
        }
        // if (rt >= t.size()) puts("qwq"), exit(0);
        // assert(rt < t.size());
        int mid = (l + r) >> 1;
        if (f(mid, k, b) < f(mid, t[rt].k, t[rt].b)) swap(k, t[rt].k), swap(b, t[rt].b);
        if (l == r) return;
        if (f(l, k, b) < f(l, t[rt].k, t[rt].b)) update(t[rt].ls, l, mid, k, b);
        if (f(r, k, b) < f(r, t[rt].k, t[rt].b)) update(t[rt].rs, mid + 1, r, k, b);
    }

    ll query(int rt, int l, int r, int x) {
        if (!rt) return INF;
        // assert(rt < t.size());
        if (l == r) return f(x, t[rt].k, t[rt].b);
        int mid = (l + r) >> 1; ll res = f(x, t[rt].k, t[rt].b);
        if (x <= mid) res = min(res, query(t[rt].ls, l, mid, x));
        else res = min(res, query(t[rt].rs, mid + 1, r, x));
        return res;
    }

    void init() {
        for (int i = 0; i <= tot; ++i) t[i] = node();
        tot = fir = 0;
    }

    // void init() {t.clear(); t.push_back(node()); tot = fir = 0;}
}t[3];

void dfs1(int x, int pa, int d, int anc) {
    t[0].update(t[0].fir, 0, inf, a[x], dis[x] + dis[x]);
    depth[x] = d; fa[x] = pa; sz[x] = 1; col[x] = anc;
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == pa || vis[y]) continue;
        dis[y] = dis[x] + tmp.second;
        dfs1(y, x, d + 1, anc == -1 ? y : anc);
        sz[x] += sz[y];
        if (sz[y] > sz[son[x]]) son[x] = y;
    }
}

void dfs2(int x) {
    for (auto tmp : vec[x]) {
        int y = tmp.y;
        if (col[x] == col[y]) {
            int id = tmp.id, d = tmp.d;
            ans[id] = min(ans[id], t[0].query(t[0].fir, 0, inf, d) + dis[x] + dis[y]);
            nxt.push_back(tmp);
        }
        else mem[x].push_back(tmp);
    }
    swap(vec[x], nxt);
    nxt.clear();
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || vis[y]) continue;
        dfs2(y);
    }
}

void update(int x, ll val) {
    t[1].update(t[1].fir, 0, inf, a[x], dis[x] + dis[x] - val - val);
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || vis[y]) continue;
        update(y, val);
    }
}

void query(int x) {
    for (auto tmp : mem[x]) {
        int y = tmp.y, id = tmp.id, d = tmp.d;
        ans[id] = min(ans[id], t[1].query(t[1].fir, 0, inf, d) + dis[x] + dis[y]);
    }
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || vis[y]) continue;
        query(y);
    }
}

void Update(int x) {
    t[2].update(t[2].fir, 0, inf, a[x], dis[x] + dis[x]);
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || vis[y]) continue;
        Update(y);
    }
}

void Query(int x, int anc) {
    for (auto tmp : mem[x]) {
        int y = tmp.y, d = tmp.d, id = tmp.id;
        ans[id] = min(ans[id], t[2].query(t[2].fir, 0, inf, d) + dis[x] + dis[y] - dis[anc] - dis[anc]);
        // printf("[%d %d %lld]\n", anc, id, ans[id]);
    }
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || vis[y]) continue;
        Query(y, anc);
    }
}

void dfs3(int x) {
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || y == son[x] || vis[y]) continue;
        dfs3(y); t[2].init();
    }
    if (son[x]) dfs3(son[x]);
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || y == son[x] || vis[y]) continue;
        Update(y);
    }
    t[2].update(t[2].fir, 0, inf, a[x], dis[x] + dis[x]);
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || y == son[x] || vis[y]) continue;
        // printf("[%d %d]\n", y, x);
        Query(y, x);
    }
    for (auto tmp : mem[x]) {
        int y = tmp.y, id = tmp.id, d = tmp.d;
        ans[id] = min(ans[id], t[2].query(t[2].fir, 0, inf, d) + dis[y] - dis[x]);
        // printf("[%d %d %d]\n", x, id, ans[id]);
    }
}

void dfs4(int x) {
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || y == son[x] || vis[y]) continue;
        update(y, dis[x]);
    }
    t[1].update(t[1].fir, 0, inf, a[x], 0);
    for (auto tmp : mem[x]) {
        int y = tmp.y, id = tmp.id, d = tmp.d;
        ans[id] = min(ans[id], t[1].query(t[1].fir, 0, inf, d) + dis[x] + dis[y]);
    }
    mem[x].clear();
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || y == son[x] || vis[y]) continue;
        query(y);
    }
    if (son[x]) dfs4(son[x]);
    else t[1].init();
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == fa[x] || y == son[x] || vis[y]) continue;
        dfs4(y);
    }
}

void get_rt(int x, int pa) {
    sz[x] = 1; mx[x] = 0;
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (y == pa || vis[y]) continue;
        get_rt(y, x);
        sz[x] += sz[y];
        mx[x] = max(mx[x], sz[y]);
    }
    mx[x] = max(mx[x], n - sz[x]);
    if (mx[x] < mx[root]) root = x;
}

void work(int x) {
    t[0].init(); t[1].init();
    dis[x] = 0; vis[x] = 1;
    dfs1(x, x, 1, -1); dfs2(x); dfs3(x);
    for (auto tmp : g[x]) {
        int y = tmp.first;
        if (vis[y]) continue;
        sum = sz[y]; mx[root = 0] = inf; get_rt(y, 0); work(root);
    }
}

void solve(int rt) {
    t[0].init(); t[1].init(); dis[rt] = 0;
    dfs1(rt, rt, 1, -1);
    for (int i = 1; i <= n; ++i)
        for (auto tmp : vec[i])
            if (tmp.y == rt) mem[i].push_back(tmp);
    dfs3(rt); t[2].init(); dfs4(rt);
    // printf("[%d %lld]\n", rt, ans[1]);
    for (int i = 1; i <= n; ++i) son[i] = 0;
}

int main() {
    n = read(); m = read(); T = read();
    for (int i = 1; i <= n; ++i) a[i] = read();
    for (int i = 1; i < n; ++i) {
        int x = read(), y = read(); ll v = Read();
        g[x].push_back(make_pair(y, v));
        g[y].push_back(make_pair(x, v));
    }
    for (int i = 1; i <= T; ++i) {
        ll xx = Read() - 1, yy = Read() - 1;
        int x = xx % n + 1, y = yy % n + 1, d = xx / n - yy / n;
        if (d < 0) d = -d;
        // printf("[%d %d %d]\n", x, y, d);
        vec[x].push_back(Node(i, y, d));
        vec[y].push_back(Node(i, x, d));
        ans[i] = INF;
    }
    // solve(3); return 0;
    for (int i = 1; i <= n; ++i) solve(i);
    // solve(63);
    for (int i = 1; i <= T; ++i) printf("%lld\n", ans[i]);
    return 0;
}

//考虑点分治套点分治,递归变成子任务6

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 3
Accepted

Test #1:

score: 3
Accepted
time: 3ms
memory: 32676kb

input:

97 99 1000
763118300 558295517 80676328 362318619 473105413 468927175 311496507 936935430 176032966 304576040 308583326 681580095 549392233 518152994 829474320 751189715 542810029 587869244 878512027 530678371 832766129 535259635 799122942 596982955 884696876 605325753 495661541 506105495 561218313 ...

output:

6130845802041
10806758605627
3440559556796
5426989115608
4458875959622
1566659300400
7908007295597
1846030561682
5112206409383
6968388472340
4706970599850
5270158948178
4633066810868
3176122148295
2331646415266
961435137842
14353365296423
9675072605938
4256954122467
7333255321628
8376795894537
12319...

result:

ok 1000 lines

Test #2:

score: 0
Accepted
time: 7ms
memory: 32112kb

input:

96 100 1000
31199641 534644486 57980198 794620020 322548308 524438614 467991232 68617179 243820212 229414440 471419229 316085673 271698528 136252783 926625435 615138376 200446739 551914057 483498389 879166147 512229523 45850421 337184110 799426876 46405170 427929494 235848997 861270528 291868400 616...

output:

2436336301732
4467388472930
6498834342013
6450642313333
4049880787954
7509100670176
5831628235154
4150554274586
3112250048344
202594784082
2974050754796
8714737807242
7727115169865
1321297431165
3071603311467
4662413775237
5469193332429
2306749862693
6240860740176
1297819731517
5602374629655
5876108...

result:

ok 1000 lines

Test #3:

score: 0
Accepted
time: 3ms
memory: 38408kb

input:

96 100 1000
557703101 38662752 91559144 406758463 248251717 279124287 667387330 272252891 892434115 281731667 162886140 786660171 350559478 909940602 476034354 78826379 748607300 381191755 708777514 223906483 954905399 405424569 356033791 565979037 5787205 21241249 399771402 280058652 527147793 5875...

output:

80606469890
86777173467
35481695596
11498756054
87983213070
37171191055
33172639202
31451029430
105454750479
31626589074
105218154775
46986908645
14488184465
20368758481
41150521804
57639739744
45269689956
24620398400
51392609182
44732144926
72097558763
13572235163
78364419227
40255815091
1195379951...

result:

ok 1000 lines

Test #4:

score: 0
Accepted
time: 3ms
memory: 41712kb

input:

96 96 1000
651436202 969634688 411838043 313319930 906863003 709869271 467187062 352351954 116039480 172098032 933097773 469945118 162439715 767382758 713254949 387661957 387494696 343642279 730353853 607472395 431993920 231539946 570437226 454446439 941394569 230535263 758579901 173778951 636556431...

output:

81136576805
65057090263
57308140599
70187240654
42682272024
83341639885
53487742661
53219947761
14656518493
18143524741
27930061212
75815621849
67528908552
39936561353
44548681818
122544815339
64143328818
13510734748
16412423500
108236922255
83503599273
53331146110
59331932211
93957710008
3825533077...

result:

ok 1000 lines

Test #5:

score: 0
Accepted
time: 9ms
memory: 42036kb

input:

100 97 1000
9442498 799978686 853182282 938513473 647407813 233204982 473300672 884708491 641608620 453797741 412704210 204494322 711344505 287815571 401113141 110034416 590478367 831110886 255614524 234577289 759353531 774504637 366342991 154214800 692604750 308540773 768713312 121270668 425512518 ...

output:

5006945326
9445360831
13045856109
4494648380
6833826505
3769548416
11380661054
5754815524
8246147623
4801077020
5798520769
1392753490
6948207422
12106173499
6097834765
4210216111
3541517785
5402770609
8790748574
10564152311
2826265999
5688050930
7790838243
2760570076
4835335024
5099967138
3178901350...

result:

ok 1000 lines

Test #6:

score: 0
Accepted
time: 0ms
memory: 39400kb

input:

100 100 1000
72360333 314738290 34206178 541145218 174183915 396182816 34673830 913434757 537587312 731274646 498514633 251108131 102959912 646618466 457048174 475505598 769612876 452196872 718739596 624410437 9031420 286458655 569299852 299007318 306647081 98662275 920437282 210801779 674405507 219...

output:

3655918495
8154487755
14137574021
9267685665
7011313073
12013474026
11488238672
8912382325
14741526855
17840835926
8748441239
7607949288
7949030269
10402650219
7895349896
9942798476
19632481428
6001424701
5150011606
14328944041
7536672479
10941755235
8915887826
11121022173
5570869661
5489621389
7171...

result:

ok 1000 lines

Test #7:

score: 0
Accepted
time: 6ms
memory: 38424kb

input:

100 100 1000
671289501 901524700 187943785 477991411 752983691 647246158 953320691 934684418 412368667 925367409 762075316 358848462 963075530 457549089 783006165 5363230 270806670 315603863 281313188 630063184 613102269 512390085 496057389 735900160 98801654 915737591 295267905 169463128 895274860 ...

output:

109982431024
38431175833
69772178732
48480356522
11451380583
20622728312
34853416584
49223376377
50708590192
23464488129
66396102290
49072787090
63071461232
100259758746
39605337609
43518034110
69171797185
76120124501
31732771736
43824267429
50401683487
18812219611
17592704047
40305088337
8902132735...

result:

ok 1000 lines

Test #8:

score: 0
Accepted
time: 4ms
memory: 38688kb

input:

100 100 1000
97012332 782222818 959933898 427265795 145416034 1879121 649904335 612684991 536487830 163534103 503165874 529288026 317196350 758782134 932469419 45296081 275446181 306725071 736168208 601979715 145679830 269223691 287672389 93736686 102440922 49242307 14300820 7040380 666185124 780231...

output:

43197564487
61281841688
44768770079
49385322040
35679440075
61129947858
71570620926
54095812113
65913522420
61326476305
50331061951
39723327870
52634647963
37572255373
44983995360
20296010788
65756945356
16562779626
52036714858
19671474013
46849723315
81935125579
28823217661
58491548742
63997977890
...

result:

ok 1000 lines

Test #9:

score: 0
Accepted
time: 0ms
memory: 38688kb

input:

1 2 5
1000000000
1 1
1 2
2 1
2 2
1 1

output:

0
1000000000
1000000000
0
0

result:

ok 5 lines

Test #10:

score: 0
Accepted
time: 0ms
memory: 42028kb

input:

2 1 5
2 1
1 2 1000000000000
1 1
1 2
2 2
1 1
2 1

output:

0
1000000000000
0
0
1000000000000

result:

ok 5 lines

Subtask #2:

score: 0
Time Limit Exceeded

Dependency #1:

100%
Accepted

Test #11:

score: 0
Time Limit Exceeded

input:

4968 1000000000 4947
35398 17280 28501 30106 20289 12704 8742 38872 40600 32757 4709 49509 18925 8232 12657 5856 35298 16182 34878 29788 22757 3667 6147 33251 10280 21807 9932 43760 25234 21837 2000 42316 42227 30480 48217 18842 12065 3569 33962 10434 9965 6816 46030 14352 7696 39388 37027 2641 4927...

output:


result:


Subtask #3:

score: 0
Time Limit Exceeded

Test #20:

score: 0
Time Limit Exceeded

input:

200000 20 200000
679416469 548913625 468159997 137709609 883140368 682558021 473174374 400192350 124143873 825920417 372498686 851213321 822264481 78195915 5427143 453304163 233551905 810910186 810046144 52603791 282167184 385032797 81387991 747194790 917579656 585184539 12659388 249218417 158295502...

output:


result:


Subtask #4:

score: 0
Time Limit Exceeded

Test #28:

score: 0
Time Limit Exceeded

input:

200000 1000000000 200000
28270302 472359923 262785485 923929785 393684160 761485431 72038469 116384740 426631758 437934930 610834083 455314140 734276543 903544756 220163018 756113376 732404264 947339315 109784905 625451008 794076307 818852312 758007217 124450858 674924509 311761991 507260538 7032362...

output:


result:


Subtask #5:

score: 0
Time Limit Exceeded

Test #36:

score: 0
Time Limit Exceeded

input:

199918 1000000000 199903
1496 2382 3896 3664 1177 1627 2821 4200 3074 3783 2069 4403 629 2610 4991 4074 3033 2798 4333 3501 3667 3064 663 2821 2818 458 2950 4020 2665 3578 63 4855 4941 3492 2423 4510 1489 1018 4829 1912 3133 3174 309 287 2909 4102 4296 4526 3170 3683 4960 4863 4738 2927 2405 3600 44...

output:


result:


Subtask #6:

score: 0
Time Limit Exceeded

Test #43:

score: 0
Time Limit Exceeded

input:

200000 1000000000 200000
81882094 47220813 43282454 17633207 52769165 4830673 31396360 64793163 9174729 36727563 71268262 24662923 40146030 1430053 62926106 30042905 1330107 81817720 98841078 87766129 51155045 23216268 79896310 66625868 87854925 42976560 86542933 28336449 34932261 19698851 584453 90...

output:


result:


Subtask #7:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%

Subtask #8:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%