QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#506817 | #4924. 蜘蛛爬树 | 5toubun_no_hanayome | 22 | 1123ms | 1032132kb | C++14 | 5.9kb | 2024-08-05 22:41:30 | 2024-08-05 22:41:33 |
Judging History
answer
#include<bits/stdc++.h>
#define rep(i, a, b) for(int i = (a);i <= (b);++i)
#define per(i, a, b) for(int i = (a);i >= (b);--i)
#define lc (k << 1)
#define rc (k << 1 | 1)
#define lowbit(x) ((x) & -(x))
#define odd(x) ((x) & 1)
#define even(x) (!odd(x))
#define fir first
#define sec second
#define pb push_back
#define il inline
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define i128 __int128
#define f128 __float128
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define SZ(x) ((int)(x).size())
#define debug(x) cerr << "In Line " << __LINE__ << " the " << #x << " = " << (x) << "\n"
using namespace std;
using ll = long long;
using ull = unsigned long long;
template<class T> using vc = vector<T>;
template<class Tx, class Ty>
il void chkmx(Tx& x, const Ty y) {x = max<common_type_t<Tx, Ty>>(x, y);}
template<class Tx, class Ty>
il void chkmn(Tx& x, const Ty y) {x = min<common_type_t<Tx, Ty>>(x, y);}
const int inf = 0x3f3f3f3f;
const ll llinf = 0x3f3f3f3f3f3f3f3fll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
bool ms;
const int N = 2e5 + 5, LOG = 32 * 5;
int head[N], cnt;
int fa[N], dep[N], sz[N], son[N];
int top[N], id[N], rev[N], tot;
int pos1[N], pos2[N], ct;
int a[N], m;
ll d[N];
struct node {
int to;
ll w;
int nxt;
} e[N << 1];
void init() {
cnt = 0;
memset(head, -1, sizeof(head));
}
void add(int u, int v, ll w) {
e[cnt] = {v, w, head[u]};
head[u] = cnt++;
}
void adde(int u, int v, ll w) {
add(u, v, w);
add(v, u, w);
}
void dfs1(int u, int f) {
sz[u] = 1;
for(int i = head[u];~i;i = e[i].nxt) {
int v = e[i].to;
ll w = e[i].w;
if(v == f)
continue;
fa[v] = u;
dep[v] = dep[u] + 1;
d[v] = d[u] + w;
dfs1(v, u);
sz[u] += sz[v];
if(sz[v] > sz[son[u]])
son[u] = v;
}
}
void dfs2(int u, int t) {
top[u] = t;
id[u] = ++tot;
rev[tot] = u;
if(!son[u])
return;
dfs2(son[u], t);
for(int i = head[u];~i;i = e[i].nxt) {
int v = e[i].to;
if(v != fa[u] && v != son[u])
dfs2(v, v);
}
}
il int LCA(int u, int v) {
while(top[u] != top[v]) {
if(dep[top[u]] < dep[top[v]])
swap(u, v);
u = fa[top[u]];
}
return dep[u] < dep[v] ? u : v;
}
struct LiChaoTree {
int ls, rs;
ll lz;
pair<int, ll> f{inf, llinf};
} tr[N * LOG];
il ll calc(pair<int, ll>& f, ll x) {
return f.fir * x + f.sec;
}
il void lazy(int k, ll v) {
tr[k].lz += v;
tr[k].f.sec += v;
}
il void push_down(int k) {
if(tr[k].lz) {
if(tr[k].ls) {
tr[++ct] = tr[tr[k].ls];
lazy(tr[k].ls = ct, tr[k].lz);
}
if(tr[k].rs) {
tr[++ct] = tr[tr[k].rs];
lazy(tr[k].rs = ct, tr[k].lz);
}
tr[k].lz = 0;
}
}
il void update(int& k, pair<int, ll> x, int L = 1, int R = m) {
tr[++ct] = tr[k];
push_down(k = ct);
int mid = L + R >> 1;
if(calc(x, mid) < calc(tr[k].f, mid))
swap(x, tr[k].f);
if(calc(x, L) < calc(tr[k].f, L))
update(tr[k].ls, x, L, mid);
else if(calc(x, R) < calc(tr[k].f, R))
update(tr[k].rs, x, mid + 1, R);
}
il int merge(int x, int y, int L = 1, int R = m) {
if(!x || !y)
return x + y;
int mid = L + R >> 1;
if(calc(tr[x].f, mid) > calc(tr[y].f, mid))
swap(x, y);
if(L == R)
return x;
push_down(x);
push_down(y);
int k = ++ct;
tr[k].f = tr[x].f;
tr[k].ls = merge(tr[x].ls, tr[y].ls, L, mid);
tr[k].rs = merge(tr[x].rs, tr[y].rs, mid + 1, R);
update(k, tr[y].f, L, R);
return k;
}
il ll query(int k, int p, int L = 1, int R = m) {
if(!k || L == R)
return calc(tr[k].f, p);
push_down(k);
int mid = L + R >> 1;
ll ans = p <= mid ? query(tr[k].ls, p, L, mid) : query(tr[k].rs, p, mid + 1, R);
return min(ans, calc(tr[k].f, p));
}
void dfs3(int u) {
update(pos1[u], {a[u], 0});
if(!son[u]) {
pos2[u] = pos1[u];
return;
}
dfs3(son[u]);
ll W = 0;
for(int i = head[u];~i;i = e[i].nxt) {
int v = e[i].to;
ll w = e[i].w;
if(v != fa[u] && v != son[u]) {
dfs3(v);
tr[++ct] = tr[pos2[v]];
lazy(ct, w << 1);
pos1[u] = merge(pos1[u], ct);
} else if(v == son[u])
W = w;
}
tr[++ct] = tr[pos2[son[u]]];
lazy(ct, W << 1);
pos2[u] = merge(pos1[u], ct);
}
void dfs4(int u) {
if(son[u])
pos1[son[u]] = merge(pos1[u], pos1[son[u]]);
for(int i = head[u];~i;i = e[i].nxt) {
int v = e[i].to;
if(v != fa[u])
dfs4(v);
}
}
bool mt;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cerr << fabs(&ms - &mt) / 1024 / 1024 << "\n";
init();
int n, q;
cin >> n >> m >> q;
rep(i, 1, n)
cin >> a[i];
rep(i, 2, n) {
int u, v;
ll w;
cin >> u >> v >> w;
adde(u, v, w);
}
dep[1] = 1;
dfs1(1, 1);
dfs2(1, 1);
dfs3(1);
dfs4(1);
while(q--) {
ll s, t;
cin >> s >> t;
assert(s == 1);
--s, --t;
int i = s / n, j = t / n, k = abs(i - j);
int u = s % n + 1, v = t % n + 1;
int l = LCA(u, v);
ll dis = d[u] + d[v] - (d[l] << 1);
if(!k) {
cout << dis << "\n";
continue;
}
ll ans = llinf;
while(v) {
chkmn(ans, query(pos2[v], k));
chkmn(ans, query(pos1[v], k));
v = fa[top[v]];
}
ans += dis;
cout << ans << "\n";
}
cerr << 1e3 * clock() / CLOCKS_PER_SEC << " ms\n";
return 0;
}
詳細信息
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 0
Runtime Error
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:
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Runtime Error
Test #20:
score: 0
Runtime Error
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
Runtime Error
Test #28:
score: 0
Runtime Error
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
Runtime Error
Test #36:
score: 0
Runtime Error
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: 22
Accepted
Test #43:
score: 22
Accepted
time: 301ms
memory: 1023712kb
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
Accepted
time: 338ms
memory: 1031984kb
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:
ok 200000 lines
Test #45:
score: 22
Accepted
time: 318ms
memory: 1026948kb
input:
200000 1000000000 200000 221911326 672598376 586864284 652343839 645072453 477182516 470253244 169559270 913860372 153896545 452982258 356042452 131678734 285479642 780356439 919192461 374130593 89810026 390686444 990599905 300023735 916489417 40995583 613948336 361863393 17299001 909709725 80365886...
output:
3850358894573407 4727815116715378 6494041025937004 4824435980551637 3378697610475978 5909426402365293 5518872599382274 4158124880334301 3937773073272371 9687643834151992 2427228601277127 2243767802218885 2464182590746753 8320457785791601 2372831686710907 7624139791764704 2729257787239080 90363701429...
result:
ok 200000 lines
Test #46:
score: 22
Accepted
time: 390ms
memory: 1032132kb
input:
199997 1000000000 200000 418668520 349968181 349972501 413151936 413168716 358529603 413712865 419740996 413260207 413754768 413754409 413644778 352475862 391987463 349972716 1000000000 349968635 376567534 413307389 413513241 392814142 413753730 349967166 349969051 365151841 349973369 413757992 3499...
output:
246430378081337415 289991643186437654 296315729206931331 364169975100364021 192595239991666627 57389957577630585 322772936556813385 154410265195118126 15121202928554667 95942665106074526 29110913867320341 247647043585119923 331278885019774528 399239706543450142 259799041077439947 308782410202237399 ...
result:
ok 200000 lines
Test #47:
score: 22
Accepted
time: 692ms
memory: 1029584kb
input:
199997 1000000000 200000 5099350 2290489 288943 3023893 498119 1980893 61654 4972753 2125310 135980 1090538 2954994 4015300 812572 4645903 3233143 1379788 818444 1534283 4069851 19634 2435868 4145501 16053 5943 3925849 3172608 871962 1005093 1205114 8917 758532 3035834 2817889 545073 3670544 5057079...
output:
234196984676 486350257291 322294896890 216546281167 494216113461 572299792560 489662712294 475722942319 243629568040 429703691047 203546469394 283970977506 247616916213 535967681169 243027652067 472054561070 338167336711 249147033135 288935860327 290760911811 309565528803 513985763532 431591688646 4...
result:
ok 200000 lines
Test #48:
score: 22
Accepted
time: 816ms
memory: 1023172kb
input:
200000 1000000000 200000 400973183 400377730 400709896 400180375 400313803 400511962 400371890 400134460 400148639 400269163 400170270 1000000000 400217075 400325397 400194331 1000000000 400130150 1000000000 400102020 400139679 400123734 1000000000 400126747 400222387 1000000000 400122213 400115355 ...
output:
330898574562445672 112499815410130451 292078105105911864 226269792209495968 86667035410318720 173003927946045263 96345981791642245 386925883084229768 201270426486087515 390559064393977652 363775401603363524 316465987026547677 189681741290208846 179522924025315419 231976763836669836 17536211389322141...
result:
ok 200000 lines
Test #49:
score: 22
Accepted
time: 1123ms
memory: 1023128kb
input:
200000 1000000000 200000 3104552 3104468 3104522 3104441 3104006 3104418 3104492 3104257 3104338 3103037 3103790 3104124 3104403 3104409 3104324 3104186 3103634 3100765 3102923 3098416 3101925 3103680 3102937 3103140 3103205 3104375 3104227 3097705 3104353 3099751 3104174 3104077 3098358 3103547 310...
output:
299170245832 492089907878 234626220221 479271609338 502550631422 276242675712 568363575475 499894660691 637989402461 528007462738 472851936868 609342882560 603201080952 470943763001 332242018172 266091057439 451790590271 501620901482 570283671029 619943731238 492033377164 607909626590 555558037644 4...
result:
ok 200000 lines
Test #50:
score: 22
Accepted
time: 460ms
memory: 1023800kb
input:
199924 1000000000 199984 399869085 398376861 439948274 399733681 398624841 398713237 479735387 399767025 400080369 649078920 399661262 400009677 1000000000 399981396 399790787 398757873 400028368 410098923 400282166 399725262 399857368 398570981 398465638 399994302 400011046 398750814 701394147 4264...
output:
274712263738077387 361957805519428564 312885220894571314 91126316434021637 14632663337398615 288041199082475383 285788012165091014 239444158149519924 228152425395680281 386763034110222318 335523640469294150 231950045342323397 11909732801116553 327243695168307095 227866104753359078 293094204545827505...
result:
ok 199984 lines
Test #51:
score: 22
Accepted
time: 475ms
memory: 1027884kb
input:
200000 1000000000 200000 391175458 386460764 381754652 782149047 413042241 391189070 386250325 408504234 450354921 389257952 400953425 400000679 383617987 870067693 407269224 383581360 413227573 382122391 385646739 405208885 412435077 549469669 392037145 387667032 387500501 395760590 408533213 40678...
output:
359574023501711749 189386606118067911 340221522042157239 94695135310902641 194853259519380481 204036382398674152 220150543637394295 247692420482925693 170505194682743836 128701965779923218 221082537626355128 152363458918389004 229636099144301752 212471069198315247 290534107817356008 3837587588639638...
result:
ok 200000 lines
Test #52:
score: 22
Accepted
time: 224ms
memory: 1022736kb
input:
200000 1000000000 200000 474717573 399999395 399998209 399998002 400000391 400000384 400000598 400001243 399997532 399998369 400000255 399998545 400000326 522897504 399998475 400000410 400000375 399998364 399998641 399998124 399997405 399997992 399998572 399999371 400000289 400001268 399998034 39999...
output:
173736955036522085 33650951440191764 143730970143276166 99418193510942190 342634198332954072 162502488670705477 375965255476953997 258543590358454738 232061399883523900 261060000771343170 357529029841999653 240058654959790996 204637421229932299 343226944296757095 151951735010708339 23769275557349395...
result:
ok 200000 lines
Test #53:
score: 22
Accepted
time: 653ms
memory: 1023736kb
input:
199982 1000000000 199933 13905 69644 11795 17651 18054 465661 20911 738216 1097153 735181 986430 65838 1015433 420487 80345 2483 157723 6170 3872 169523 310529 772003 317617 957530 10689 50872 725052 351260 406429 905643 29455 787932 727733 156301 827039 801839 271562 84335 74338 96058 461503 110830...
output:
315889687715 274454363440 294262916565 87340375425 267007540685 359869041992 302546759206 324552934313 226536315022 297144155332 494360796272 355456788344 53123205018 270937855399 250406745471 259508383005 461215278318 300738324383 326197147912 306529365019 267905971033 268651306040 272619646060 235...
result:
ok 199933 lines
Test #54:
score: 22
Accepted
time: 571ms
memory: 1028220kb
input:
200000 1000000000 200000 3001595 14097 180920 1421096 1780325 345585 6321 536970 108359 1580188 188319 2209073 93991 62691 826278 1406374 2372360 1935896 2941114 1198315 1818930 159407 182164 1372932 1257296 2766581 1294716 1197130 1954695 2410246 229834 2266485 1553788 1983380 994284 250717 583339 ...
output:
464814966079 550459273553 392015576571 330654126238 295524188309 557539614122 499310432985 466739001540 206521426474 486492095002 347320572639 480630888276 303763127639 515800154811 277829449015 491439750254 514105778996 262998042597 477953258405 346983235644 377144239806 469569787980 320557580253 2...
result:
ok 200000 lines
Test #55:
score: 22
Accepted
time: 314ms
memory: 1022740kb
input:
200000 1000000000 200000 180082 114229 64689 3671213 168564 3304613 22338 188029 59435 1582099 3442061 4783670 1380330 837820 4808200 64087 125886 480033 18218 3362487 4919646 2317099 146322 34703 161950 1209001 1370897 3727366 291020 4251841 3045276 830451 309 3555827 288899 4096315 1145583 3630161...
output:
470985272633 330577001589 439814644058 523567895118 501048366869 462793156984 291316709009 404699099167 290256852644 444960749007 473443352397 538540471582 643148473320 273919681872 601471298829 498808821230 571596269149 377997366417 384048397221 473618982159 527591322785 251088873050 454153652833 4...
result:
ok 200000 lines
Test #56:
score: 22
Accepted
time: 52ms
memory: 1004796kb
input:
1 2 5 1000000000 1 1 1 2 1 2 1 1 1 1
output:
0 1000000000 1000000000 0 0
result:
ok 5 lines
Test #57:
score: 22
Accepted
time: 51ms
memory: 1004808kb
input:
2 1 5 2 1 1 2 1000000000000 1 1 1 2 1 1 1 1 1 2
output:
0 1000000000000 0 0 1000000000000
result:
ok 5 lines
Test #58:
score: 22
Accepted
time: 333ms
memory: 1023920kb
input:
200000 4000000 200000 469534105 869657262 201026406 706247703 720363334 591393274 140527583 403309685 669391268 962180948 533915565 370916928 278461532 572910588 36045110 106609732 314193846 638246398 749887226 95860789 609990162 263858083 933984169 92866433 774165117 800604654 471471901 384849200 9...
output:
1423533148858611 1203199106273073 65596428936959 1469120764498552 1338293611070646 1031513116329584 1293493771712425 1449296607273261 1448987449112301 1448657128680465 1448482174566858 1279397039368897 1449767211059881 857407952795972 1138358455373900 1449579740661381 1360981514512919 96162444990027...
result:
ok 200000 lines
Test #59:
score: 22
Accepted
time: 327ms
memory: 1023980kb
input:
200000 1000000000 200000 3695933 725812 3938862 3188795 74871 179361 892126 899932 2952206 1461698 674172 2140411 3124478 253567 1930733 1468200 844171 3820729 3146995 2143636 3012500 3860711 1978473 458453 1011065 3915797 2489264 581391 3663430 200944 463051 2789062 329754 3391992 1666664 1278446 3...
output:
1313893005299891 1241413179669075 1013214888095897 1283956382437075 832938541171640 1357016581395659 1358632237670655 1358575371105735 1359617242208535 1358550490017855 1262000024359038 1090246621403288 1365726941260351 1070531999758396 1247397876139791 1294791538934294 931852209639129 1358682897483...
result:
ok 200000 lines
Test #60:
score: 22
Accepted
time: 588ms
memory: 1024032kb
input:
200000 1000000000 200000 1000000000 395785681 450569981 397435194 399243043 815631194 396368986 396009218 395184318 399082057 396553759 395139833 405819304 397314248 404595065 396486584 396139088 398990743 397510485 561967645 408268414 396500176 395757782 456603560 396336452 399624664 396449945 3959...
output:
109920129587885069 378225816640797819 309690844924907845 267339736788532370 372175904770256958 321135232766545507 232563027043320134 124882843097505896 115857700588107460 202847102004389723 389602100695363841 235317792421863006 253135733788837109 220301266029980272 169917572575901836 214668686787005...
result:
ok 200000 lines
Test #61:
score: 22
Accepted
time: 910ms
memory: 1024040kb
input:
200000 1000000000 200000 1867594 524322 3615 25303 1538215 1040257 1623469 84359 1503219 419070 1873718 873101 203084 1842101 1034729 598281 1086715 1851042 1126586 240305 111153 1411689 755343 23669 1982825 1608454 2068771 2085317 1086469 19627 30122 1623595 138403 33439 1979692 1136830 173176 1415...
output:
306225753801 342005544451 436459567316 236781181877 341005923055 438745980019 330732109809 239627724290 562373111121 467513530464 239225732828 326333872737 326471417616 429675583322 552828882509 239263092986 288718695356 245227283983 471568113901 241072755476 241360488328 309136881480 242266843083 2...
result:
ok 200000 lines
Test #62:
score: 22
Accepted
time: 854ms
memory: 1024400kb
input:
200000 1000000000 200000 941812 82062 38722 678737 68338 1072195 141897 141638 1002771 780921 137477 673465 918996 649333 406005 726096 496011 796418 346152 733114 890963 1057424 367441 343348 18182 83852 84060 786683 504660 44953 769252 734380 460070 272021 934209 71402 748582 323154 298122 7832 15...
output:
126436853796 465180591754 369107820231 281078670236 345259813302 494359911875 244336591685 484282259162 243752267165 460503945863 473839717345 439525600179 243099218281 505218592257 363750198100 244895817789 374199391493 465134123100 207985506641 513109340343 498612704121 213872806786 460528982772 2...
result:
ok 200000 lines
Subtask #7:
score: 0
Skipped
Dependency #1:
0%
Subtask #8:
score: 0
Skipped
Dependency #1:
0%