QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#100261#4924. 蜘蛛爬树jp01HUANGZI9 3382ms327964kbC++145.4kb2023-04-25 11:28:232023-04-25 11:28:27

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-25 11:28:27]
  • 评测
  • 测评结果:9
  • 用时:3382ms
  • 内存:327964kb
  • [2023-04-25 11:28:23]
  • 提交

answer

// my name is huangzirui, i (jp01) will ak ioi 2024

//
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128 i128;
#define debug(...) fprintf (stderr, __VA_ARGS__)

#define fi first
#define se second
#define lep(i, l, r) for (int i = (l), i##_ext = (r); i <= i##_ext; ++ i)
#define rep(i, r, l) for (int i = (r), i##_ext = (l); i >= i##_ext; -- i)

char _c; bool _f; template <class T> inline void IN (T & x) {
	_f = 0, x = 0; while (_c = getchar (), ! isdigit (_c)) if (_c == '-') _f = 1;
	while (isdigit (_c)) x = x * 10 + _c - '0', _c = getchar (); if (_f) x = -x;
}

template <class T> inline void chkmin (T & x, T y) { if (x > y) x = y; }
template <class T> inline void chkmax (T & x, T y) { if (x < y) x = y; }

const int N = 2e5 + 5;
const int LogN = 20;

const ll INF = 4e18;

int n, m, q;

ll w[N];
vector <pair <int, ll> > to[N];

// {{{ tree

int st[LogN][N], Log2[N];

int tim, par[N], dfn[N], lay[N];
ll len[N];

inline int chktop (int a, int b) {
    return lay[a] < lay[b] ? a : b;
}

void dfs (int u, int pre) {
    par[u] = pre, st[0][dfn[u] = ++ tim] = pre, lay[u] = lay[pre] + 1;
    for (auto & v : to[u]) if (v.fi != pre) {
        len[v.fi] = len[u] + v.se, dfs (v.fi, u);
    }
}
inline void build_st () {
    Log2[0] = -1; lep (i, 1, n) Log2[i] = Log2[i >> 1] + 1;

    dfs (1, 0);
    lep (j, 1, 19) lep (i, 1, n - (1 << j) + 1) {
        st[j][i] = chktop (st[j - 1][i], st[j - 1][i + (1 << j - 1)]);
    }
}

inline int lca (int x, int y) {
    if (x == y) return x;
    if (x = dfn[x], y = dfn[y], x > y) swap (x, y); ++ x;
    int ret = Log2[y - x + 1];
    return chktop (st[ret][x], st[ret][y - (1 << ret) + 1]);
}
inline ll dist (int x, int y) {
    return len[x] + len[y] - (len[lca (x, y)] << 1ll);
}

// }}}

// {{{ convex

struct convex {
    int top;
    vector <pair <ll, ll> > stk;
    inline void init () { top = -1; }

    inline void push_back (pair <ll, ll> & e) {
        while (top > 0 && (i128) (stk[top - 1].se - e.se) * (stk[top].fi - stk[top - 1].fi) >=
                (i128) (stk[top - 1].se - stk[top].se) * (e.fi - stk[top - 1].fi)) -- top, stk.pop_back ();
        stk.emplace_back ( e ), ++ top;
    }
    inline ll query (ll d) {
        if (! ~ top) return INF;

        int l = 0, r = top - 1, mid, p = 0;
        while (l <= r) {
            mid = l + r >> 1;
            if ((i128) d * (stk[mid + 1].fi - stk[mid].fi) >= (stk[mid].se - stk[mid + 1].se)) p = mid, r = mid - 1;
            else l = mid + 1;
        }
        return stk[p].fi * d + stk[p].se;
    }
};

// }}}

// {{{ divide tree

int cen[N][LogN], adj[N][LogN], lst_rt[N], dep[N], siz[N];
bool vis[N];

convex con[N][LogN];

int rt, mx;
vector <pair <ll, ll> > pt;

void gsiz (int u, int pre) {
    siz[u] = 1;
    for (auto & v : to[u]) if (v.fi != pre && ! vis[v.fi]) {
        gsiz (v.fi, u), siz[u] += siz[v.fi];
    }
}
void find_rt (int u, int pre, int & all) {
    int tp = all - siz[u];
    for (auto & v : to[u]) if (v.fi != pre && ! vis[v.fi]) {
        find_rt (v.fi, u, all), chkmax (tp, siz[v.fi]);
    }
    if (tp < mx) mx = tp, rt = u;
}

void color (int u, int pre, int & _cen, int & _adj) {
    cen[u][dep[_cen]] = _cen;
    adj[u][dep[_cen]] = _adj;
    for (auto & v : to[u]) if (v.fi != pre && ! vis[v.fi]) color (v.fi, u, _cen, _adj);
}
void collect (int u, int pre, int & _cen, int & _aim) {
    pt.emplace_back ( w[u], dist (u, _cen) + dist (u, _aim));
    for (auto & v : to[u]) if (v.fi != pre && ! vis[v.fi]) collect (v.fi, u, _cen, _aim);
}
int build_tr (int u, int all, int _dep = 0) {
    rt = 0, mx = all + 1, gsiz (u, 0), find_rt (u, 0, all); // find rt
    dep[rt] = _dep, vis[rt] = true, color (rt, 0, rt, u); // cen & adj

    rep (i, _dep, 0) {
        pt.clear (), collect (rt, 0, rt, cen[rt][i]);
        sort (pt.begin (), pt.end ()), con[rt][i].init ();
        for (auto & e : pt) con[rt][i].push_back ( e );
    }

    int _rt = rt; gsiz (_rt, 0);
    for (auto & v : to[_rt]) if (! vis[v.fi]) build_tr (v.fi, siz[v.fi], _dep + 1);
    return _rt;
}

ll query_1p (int c, int a, int & d, int _dep, int _aim) {
    ll ret = dist (c, a) + con[c][_aim].query (d);
    if (a != c) chkmin (ret, query_1p (cen[a][_dep + 1], a, d, _dep + 1, _aim));
    return ret;
}
ll query_2p (int c, int a, int b, int & d, int _dep) {
    ll ret = dist (a, c) + dist (b, c) + con[c][_dep].query (d);

    if (adj[a][_dep + 1] == adj[b][_dep + 1]) {
        if (a == c && b == c) return ret;
        return min (ret, query_2p (cen[a][_dep + 1], a, b, d, _dep + 1));
    }

    if (a != c) chkmin (ret, dist (b, c) + query_1p (cen[a][_dep + 1], a, d, _dep + 1, _dep));
    if (b != c) chkmin (ret, dist (a, c) + query_1p (cen[b][_dep + 1], b, d, _dep + 1, _dep));
    return ret;
}

// }}}

inline void input () {
    IN (n), IN (m), IN (q);
    lep (i, 1, n) IN (w[i]);

    int a, b; ll v;
    lep (i, 1, n - 1) {
        IN (a), IN (b),IN (v);
        to[a].emplace_back ( b, v );
        to[b].emplace_back ( a, v );
    }
}
signed main () {
    input (), build_st ();
    rt = build_tr (1, n);

    ll t1, t2; int u, v, d;
    while (q -- ) {
        IN (t1), IN (t2), u = (t1 - 1) % n + 1, v = (t2 - 1) % n + 1, d = abs ((t1 - 1) / n - (t2 - 1) / n);
        printf ("%lld\n", query_2p (rt, u, v, d, 0));
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 3
Accepted
time: 31ms
memory: 133476kb

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

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:

wrong answer 951st lines differ - expected: '52444921320', found: '1093193589050'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Time Limit Exceeded

Test #20:

score: 11
Accepted
time: 3382ms
memory: 327964kb

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:

920563165
270738856
355012553
363898450
515535908
734168762
81197110
448355845
204186827
966151314
377621564
856252543
311456222
368700872
197258906
567302636
172379629
579171621
1043838058
244996663
621435809
278057792
727463012
573783312
395879848
500677226
891900111
1031612062
771021332
691010101...

result:

ok 200000 lines

Test #21:

score: -11
Time Limit Exceeded

input:

199998 20 199928
841581743 193826897 19260647 316900759 938030012 734551083 200340391 232139411 654311599 1143318 596086442 603556286 904977745 575551276 670573487 214312499 155571640 318139630 664877075 921888211 314261245 840096855 656620366 784431866 158438090 761901044 794827280 603867695 489777...

output:

623283525
8593864781
7874704109
155914357
3646556950
3740880356
3717912008
12901066587
1524759519
4985719750
2248493864
5114948482
3925676469
579421045
1507306567
2095047126
606785057
146334438
4045519468
8910005611
4581660381
4073333567
3935919804
676004871
2344714675
5021627074
5038533943
15002805...

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:

29294995135992468
9003943574137677
39324997066279292
37544709020512848
57388992119827952
54425124319330092
19450449300737912
25838911017710871
2608104102967357
32395369352281774
5765752637876701
65609495812941401
57820177390587134
1971831067795873
19213682025389514
30244870693646792
3672338761985429...

result:


Subtask #5:

score: 9
Accepted

Test #36:

score: 9
Accepted
time: 431ms
memory: 210008kb

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:

1352416884531
1380463318391
923920163167
1525224977139
1405019709299
869269749781
715671043328
876194052054
1358007874327
127994985855
1230162209719
1532026808855
611656467332
1023855959729
414792924571
1316679734677
827308370883
1265411315424
821484360433
1051517948640
837509712760
582943943131
457...

result:

ok 199903 lines

Test #37:

score: 0
Accepted
time: 429ms
memory: 210108kb

input:

200000 1000 200000
809770918 700177243 142627650 840666719 799717263 288840787 130614153 965150450 584417569 833256629 453961603 553430999 842122932 156970995 233405993 462368588 449589390 97217337 576814616 526506175 16887352 919946415 588340411 47310125 508028561 746882745 289969878 38349443 85588...

output:

1585694392495
706038536805
586801212025
729763504879
1121912701709
602929530934
1384874490966
932809860298
1786651350814
1173133997984
642188971333
1847564817170
874110129257
1634207197990
1165001912684
860420326934
364758620851
736767366986
901294347345
1499330839732
451636930949
1002710230684
1556...

result:

ok 200000 lines

Test #38:

score: 0
Accepted
time: 535ms
memory: 209984kb

input:

200000 1000000000 200000
399998482 399998882 643012112 481981456 399998451 475990021 399997292 399997409 399996369 399998092 399998185 399998416 399998701 399997027 399996347 1000000000 411997672 399996237 399997188 402404134 399996973 399998072 459327897 399997196 399997360 606704265 399997369 3999...

output:

56425935917250335
348929904541748910
43150321666095229
218746357373815571
108846332361563136
211578526026780722
142755080047590213
244555928973138123
59355666550218703
274305014995294225
171177308635990844
94566903236734112
84270300399685207
317423517245573254
902979060499211
14514565807335715
18696...

result:

ok 200000 lines

Test #39:

score: 0
Accepted
time: 433ms
memory: 213144kb

input:

199989 1000000000 199949
5101893 2534711 252776 33497 4575476 620658 35790 1061631 1362697 834917 2062598 2789238 2540552 2557066 725856 2407848 4266144 1731334 653868 4676650 235573 2010805 1576557 922173 617762 1140093 387911 618359 2084018 2717580 9938 4014950 411349 3801906 341206 665844 2556003...

output:

376419619600
353028349944
783455928283
427146243318
508001272847
231025894377
614377184831
496116219491
384142701402
337878147372
528478063399
414595122323
604998898988
244135680083
319848781263
358386785447
481117281935
464006706964
356458898506
260105342030
610113746365
259007651455
414991108424
2...

result:

ok 199949 lines

Test #40:

score: 0
Accepted
time: 380ms
memory: 213148kb

input:

200000 1000000000 200000
1101540 573488 61066 1014872 39283 626062 84341 591377 109026 505272 1339 74452 729192 49315 521939 959958 249731 940337 56264 1071790 609623 239862 57448 809987 464526 111430 226312 124386 673550 421690 211347 45875 138962 705453 739456 464892 44238 52980 905593 205558 5198...

output:

655436303263
616441802310
638361564730
586321577191
519122088245
660130086237
389806954608
241891011597
423594953230
510963332372
630353140994
627262451077
339051346548
308888235187
550167732447
354951509166
308776095000
597351022439
625625736560
772346222022
549689478477
667370706484
319926160326
2...

result:

ok 200000 lines

Test #41:

score: 0
Accepted
time: 21ms
memory: 133284kb

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 #42:

score: 0
Accepted
time: 24ms
memory: 133320kb

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 #6:

score: 0
Time Limit Exceeded

Test #43:

score: 22
Accepted
time: 2934ms
memory: 326948kb

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:

516260625003899
380880451347644
183401242058615
56975236749493
349851829300288
188845759476214
188011317678919
414887287533565
111834744858133
305218494040213
227244584301956
365579485207024
201761449059479
246263150359463
468212144364502
389353276591541
207814284476264
341801277159919
4270404442188...

result:

ok 200000 lines

Test #44:

score: -22
Time Limit Exceeded

input:

200000 1000000000 200000
885272642 374759028 888606054 718260881 712640799 453067010 847699265 597983546 473777736 340935923 415594372 874762802 957196626 674414761 601425225 628341608 249369250 380959879 619963443 106167226 73865409 826858024 56062512 437693354 340445108 604619683 791991483 7300264...

output:

7769103153522109
21209520101497866
12453875916706553
21861512525349055
21994844775114551
22606385523748384
4962030058940312
20614777846726216
4514335046749431
20346398113954539
16479975916989640
2806007917483201
19622386582746420
22955087055842971
21041026349491377
24046136537255789
2041470515268697...

result:


Subtask #7:

score: 0
Skipped

Dependency #1:

0%

Subtask #8:

score: 0
Skipped

Dependency #1:

0%