QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#706786 | #9165. Petrol stations | RDFZchenyy | 26 | 464ms | 39112kb | C++14 | 4.3kb | 2024-11-03 13:28:27 | 2024-11-03 13:28:27 |
Judging History
answer
#include<bits/stdc++.h>
using ll=long long;
#define int long long
#define MAXN 70005
struct Edge{
int v; ll w;
int nxt;
};
struct Node{
ll d,c,s;
friend bool operator<(Node a,Node b){
return a.d>b.d;
}
};
int n,x,y; ll w,k;
Edge e[MAXN*2]; int h[MAXN],ec;
int del[MAXN]; ll sz[MAXN];
ll dep[MAXN];
ll ans[MAXN];
Node q[MAXN],all[MAXN]; int atp,qtp;
int f[MAXN][20]; ll d[MAXN][20];
ll cnt[MAXN];
void add(int u,int v,ll w){
e[ec].v=v,e[ec].w=w; e[ec].nxt=h[u];
h[u]=ec; ec++;
return;
}
int getsz(int u,int fa){
sz[u]=1;
for(int i=h[u];i+1;i=e[i].nxt){
int v=e[i].v; if(v==fa||del[v]) continue;
sz[u]+=getsz(v,u);
}
return sz[u];
}
int getrt(int u,int fa,int asz){
for(int i=h[u];i+1;i=e[i].nxt){
int v=e[i].v; if(v==fa||del[v]) continue;
if(sz[v]>asz/2) return getrt(v,u,asz);
}
return u;
}
void getdep(int u,int fa){
for(int i=h[u];i+1;i=e[i].nxt){
int v=e[i].v; if(v==fa||del[v]) continue;
dep[v]=dep[u]+e[i].w; getdep(v,u);
}
return;
}
void clear(int u,int fa){
for(int i=0;i<=17;i++) f[u][i]=d[u][i]=0;
cnt[u]=0;
for(int i=h[u];i+1;i=e[i].nxt){
int v=e[i].v; if(v==fa||del[v]) continue;
clear(v,u);
}
return;
}
void dfs(int u,int fa,int rt,int osz){
for(int i=1;i<=17;i++){
f[u][i]=f[f[u][i-1]][i-1];
d[u][i]=d[f[u][i-1]][i-1]+d[u][i-1];
}
for(int i=h[u];i+1;i=e[i].nxt){
int v=e[i].v; if(v==fa||del[v]) continue;
f[v][0]=u; d[v][0]=e[i].w;
dfs(v,u,rt,osz);
}
ll t=k; int now=u;
cnt[u]++;
for(int i=17;i>=0;i--){
if(f[now][i]&&d[now][i]<=t) t-=d[now][i],now=f[now][i];
}
if(now==rt){
q[++qtp].d=dep[u];
q[qtp].c=cnt[u];
}else{
ans[now]+=cnt[u]*osz;
cnt[now]+=cnt[u];
}
return;
}
void calc(int u,int fa,int l,int r,ll val){
for(int i=h[u];i+1;i=e[i].nxt){
int v=e[i].v; if(v==fa||del[v]) continue;
int nl=l-1,nr=r;
while(nl<nr){
int mid=(nl+nr+1)>>1;
if(q[mid].d+dep[v]>k){
nl=mid;
}else{
nr=mid-1;
}
}
q[r+1].c=q[nl].s-q[l-1].s;
q[r+1].d=-dep[u];
q[r+1].s=q[r].s+q[r+1].c;
ans[u]+=(long long)q[r+1].c*sz[v]*val;
calc(v,u,nl+1,r+1,val);
}
return;
}
void meta(int u,int fa,ll dc){
for(int i=h[u];i+1;i=e[i].nxt){
int v=e[i].v; if(del[v]||v==fa) continue;
if(dc+e[i].w>k) ans[u]+=sz[v],meta(v,u,e[i].w);
else meta(v,u,dc+e[i].w);
}
return;
}
void solve(int u){
del[u]=1; dep[u]=0;
f[u][0]=0; d[u][0]=0;
getsz(u,0),getdep(u,0);
for(int i=h[u];i+1;i=e[i].nxt){
int v=e[i].v; if(del[v]) continue;
f[v][0]=u; d[v][0]=e[i].w;
dfs(v,u,u,sz[u]-sz[v]);
std::sort(q+1,q+qtp+1);
for(int i=1;i<=qtp;i++){
q[i].s=q[i-1].s+q[i].c;
}
ll w=e[i].w;
int l=1,r=qtp;
int nl=l-1,nr=r;
while(nl<nr){
int mid=(nl+nr+1)>>1;
if(q[mid].d+dep[v]>k){
nl=mid;
}else{
nr=mid-1;
}
}
q[r+1].c=q[nl].s-q[l-1].s;
q[r+1].d=-dep[u];
q[r+1].s=q[r].s+q[r+1].c;
ans[u]-=(long long)q[r+1].c*sz[v];
calc(v,u,nl+1,r+1,-1);
for(int i=1;i<=qtp;i++){
all[++atp]=q[i];
}
qtp=0;
}
for(int i=1;i<=atp;i++){
q[i]=all[i];
}
std::sort(q+1,q+atp+1);
for(int i=1;i<=atp;i++){
q[i].s=q[i-1].s+q[i].c;
}
calc(u,0,1,atp,1);
meta(u,0,0);
clear(u,0); atp=0;
for(int i=h[u];i+1;i=e[i].nxt){
int v=e[i].v; if(del[v]) continue;
solve(getrt(v,u,getsz(v,u)));
}
return;
}
signed main(){
std::ios::sync_with_stdio(false);
std::cin.tie(0),std::cout.tie(0);
memset(h,-1,sizeof(h));
std::cin>>n>>k;
for(int i=1;i<=n-1;i++)
std::cin>>x>>y>>w,x++,y++,add(x,y,w),add(y,x,w);
solve(getrt(1,0,getsz(1,0)));
for(int i=1;i<=n;i++) std::cout<<ans[i]<<'\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 18
Accepted
Test #1:
score: 18
Accepted
time: 0ms
memory: 12068kb
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 96 45 94 0 0 0 0 0 146 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 28 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 76 0 43 0 0 75 2...
result:
ok 750 lines
Test #2:
score: 18
Accepted
time: 0ms
memory: 13904kb
input:
967 334 285 176 1 648 431 1 493 893 2 261 165 1 158 512 2 675 881 1 232 200 2 452 380 2 961 35 1 831 131 1 424 458 1 807 835 2 154 855 1 524 513 2 448 27 1 82 331 2 454 190 2 469 210 2 15 445 2 860 1 2 901 47 0 496 572 2 227 122 1 141 223 2 214 924 1 4 381 2 394 703 2 859 611 2 63 688 1 197 674 1 59...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 967 lines
Test #3:
score: 18
Accepted
time: 0ms
memory: 13904kb
input:
1000 963 408 385 876 23 519 951 649 232 605 821 385 792 194 221 882 18 984 910 115 40 155 558 973 126 876 633 625 795 781 727 923 248 397 120 632 320 392 531 185 527 973 508 986 457 918 74 339 432 259 385 243 93 973 961 640 385 531 614 325 839 823 936 691 159 240 184 40 625 891 692 385 331 196 140 1...
output:
0 0 482612 0 912 0 0 766 0 0 0 24 989 0 493138 0 0 0 0 0 1996 979 890 1996 0 0 0 0 1996 0 0 0 0 0 0 0 254507 0 997 2 450646 0 244035 727 0 2043 0 0 0 0 0 0 0 0 0 0 0 0 19929 1996 0 0 0 0 997 0 0 0 0 1994 0 2 0 0 0 0 0 0 0 0 1962 0 0 0 0 3990 493900 0 0 0 0 0 0 3988 0 940 0 0 0 0 0 1996 0 2366 0 1989...
result:
ok 1000 lines
Test #4:
score: 18
Accepted
time: 3ms
memory: 11976kb
input:
1000 396 412 257 190 290 965 25 399 938 174 980 459 117 874 575 124 386 912 40 666 164 124 97 680 49 589 442 197 451 649 109 893 648 134 975 733 198 121 966 63 346 775 94 381 246 169 507 319 159 333 287 101 127 682 118 48 784 34 708 170 0 142 723 148 836 766 185 277 711 188 512 974 68 658 404 136 95...
output:
215160 138947 196491 47632 103775 128657 26093 195362 2694 0 234548 3952 120001 175260 489004 1998 9576 0 97602 2334 86553 18629 5792 57931 251700 122745 0 33115 227964 190106 203868 102592 1290 440 0 9054 79 1620 2166 496039 28992 169701 102833 9701 218081 1463 999 433548 7728 4935 882 817 87200 36...
result:
ok 1000 lines
Test #5:
score: 18
Accepted
time: 0ms
memory: 12084kb
input:
1000 333 896 853 0 28 756 0 658 183 0 488 17 0 475 283 0 587 6 0 979 607 0 918 95 0 247 848 0 799 23 0 815 685 0 580 58 0 724 192 0 398 209 0 140 526 0 966 397 0 595 468 0 827 19 0 866 666 0 62 329 0 599 799 0 13 972 0 526 481 0 593 170 0 312 809 0 106 584 0 670 945 0 602 62 0 765 731 0 756 935 0 33...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 1000 lines
Test #6:
score: 18
Accepted
time: 0ms
memory: 11836kb
input:
2 31 0 1 31
output:
0 0
result:
ok 2 lines
Test #7:
score: 18
Accepted
time: 2ms
memory: 11988kb
input:
1000 847 24 298 474 208 141 485 789 89 60 1 809 437 936 670 449 254 938 364 796 766 340 114 453 609 656 474 374 522 795 378 80 518 279 220 648 619 643 924 191 267 499 288 483 382 643 270 949 816 284 146 481 861 706 686 103 642 631 626 136 294 642 687 407 30 253 603 149 923 502 396 637 762 0 715 67 5...
output:
16 1080 1966 5980 1856 0 5980 0 0 0 0 0 0 0 0 1992 4702 0 0 968 1996 0 0 0 0 1996 0 0 58 0 61453 0 9874 64 3990 41198 11458 13818 1952 59099 17820 1 0 0 0 0 50596 0 0 0 0 0 0 37219 1215 0 7960 1996 175610 3990 0 31526 21 3988 1996 0 0 8953 11926 0 6980 1302 0 23237 0 0 0 0 0 0 0 1996 0 0 0 0 1996 19...
result:
ok 1000 lines
Test #8:
score: 18
Accepted
time: 2ms
memory: 11984kb
input:
1000 767 476 799 361 271 239 215 447 941 269 219 664 600 904 6 264 154 82 98 711 836 437 7 75 381 922 557 741 61 413 505 87 839 548 389 87 504 634 81 393 428 214 8 812 243 511 492 17 761 765 29 129 617 879 355 251 267 554 8 21 380 505 443 235 427 97 416 678 666 313 342 93 397 145 614 179 991 113 322...
output:
1017 997 3988 1996 0 0 2 0 0 971 1996 1996 0 2983 0 1996 19530 13742 0 0 13888 5015 0 997 0 0 0 0 0 996 0 2 0 78 1996 0 0 0 7948 1 6 0 0 1996 3 0 0 0 0 997 1996 0 0 75588 51097 1996 3988 0 0 0 40284 91174 1996 0 0 0 0 26 11 5976 112650 1996 1996 0 0 12837 0 31468 0 0 0 2967 159 0 9940 0 0 40883 1000...
result:
ok 1000 lines
Test #9:
score: 18
Accepted
time: 2ms
memory: 11980kb
input:
1000 1000 574 351 479 444 634 559 531 70 113 180 828 194 123 521 790 295 646 79 539 462 598 362 736 541 673 23 185 363 906 34 859 258 777 921 726 216 504 760 725 787 64 629 314 245 98 412 361 306 636 841 287 40 818 247 674 267 783 838 903 50 175 509 177 96 46 8 56 775 113 117 762 45 511 718 69 527 4...
output:
17839 4922 3988 0 0 0 0 0 0 0 1996 1996 0 0 0 0 0 2938 958 0 0 0 0 0 0 33 0 0 1097 50763 11918 0 0 4931 4942 0 0 0 1996 0 0 3988 1996 26430 0 3 997 15 0 0 0 61525 11 2971 1974 7960 1996 7783 0 0 0 11 17 4927 3994 0 0 3957 2968 8947 986 0 0 0 0 0 0 0 0 0 5976 0 2985 0 0 0 5974 6879 40013 45110 0 1872...
result:
ok 1000 lines
Test #10:
score: 18
Accepted
time: 3ms
memory: 11948kb
input:
1000 1000 680 881 340 73 368 929 303 239 219 861 605 561 243 667 531 357 96 797 561 264 569 267 59 169 146 639 660 161 931 423 209 525 524 406 689 275 544 504 153 296 406 735 245 144 706 806 165 557 444 234 477 614 437 682 503 628 205 539 636 135 219 202 863 938 665 376 983 452 512 841 241 924 664 9...
output:
88 3990 9954 0 0 1996 8458 0 0 5974 0 0 0 0 9952 2 9954 0 1996 6915 978 1996 989 0 31481 1996 157 1996 0 0 988 1996 0 17140 7960 0 1919 928 268 0 0 0 0 0 0 0 0 14876 0 1996 43 59135 983 0 0 0 0 0 1996 76302 0 0 4592 5976 2 0 0 0 0 43971 990 0 0 0 0 0 3972 1996 0 0 1915 0 1994 1996 997 0 0 0 138 0 0 ...
result:
ok 1000 lines
Test #11:
score: 18
Accepted
time: 3ms
memory: 12000kb
input:
1000 1000 164 378 963 934 141 358 866 915 598 341 220 55 598 757 654 149 668 202 215 502 818 439 618 693 55 873 554 927 190 748 976 487 779 402 411 89 732 990 641 557 432 734 515 574 638 491 907 217 217 556 332 483 614 81 25 106 975 245 635 724 902 208 219 211 722 209 390 738 587 748 602 338 216 14 ...
output:
1996 0 1147 1996 18739 2976 0 0 0 0 0 1995 3990 0 13587 0 0 5982 1 0 997 0 1996 1996 0 0 0 0 2953 1994 1996 16 664 0 0 0 0 1996 30 3986 0 6932 3988 173897 1358 0 8907 0 0 0 1996 0 0 0 1996 1996 0 0 11938 1163 136386 1996 0 411 0 0 0 0 7903 29433 9084 3988 0 331 9940 23743 1996 0 0 3957 0 0 0 991 0 0...
result:
ok 1000 lines
Test #12:
score: 18
Accepted
time: 2ms
memory: 12028kb
input:
1000 537 276 155 470 533 155 391 175 155 343 553 155 270 24 155 447 614 155 239 927 155 314 899 155 188 398 155 344 18 155 501 937 155 413 671 155 184 808 155 418 200 155 501 491 155 374 717 155 245 692 155 476 313 155 455 0 155 232 702 155 275 544 155 498 66 155 237 245 155 233 865 155 188 574 155 ...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 1000 lines
Subtask #2:
score: 8
Accepted
Test #13:
score: 8
Accepted
time: 0ms
memory: 13800kb
input:
2 1 0 1 1
output:
0 0
result:
ok 2 lines
Test #14:
score: 8
Accepted
time: 396ms
memory: 38636kb
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
Wrong Answer
Dependency #2:
100%
Accepted
Test #15:
score: 10
Accepted
time: 428ms
memory: 38604kb
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:
450859 215263283 544492560 222149 1276050 1993840 179130 3145479 1061520 186918 436257 128366615 192824 0 190234048 88612 216212 1174022 0 1203363128 455706327 343283 436439022 417240 1223229612 182003220 739660714 325508658 0 1222504809 1192350690 0 507280212 444391111 0 866072172 564047 0 90962902...
result:
ok 70000 lines
Test #16:
score: 0
Wrong Answer
time: 464ms
memory: 38708kb
input:
70000 611016790 21272 16063 50360 30758 33429 30642 23317 5625 9045 66335 5731 24130 36096 15843 18089 60665 41152 19640 6148 39003 53047 42150 9859 46839 5191 59681 3924 53733 53709 36514 50103 15977 57464 40309 10907 5473 38449 24104 14928 69191 4445 31512 57129 33963 47456 14993 17284 40793 34588...
output:
182384 -917499725 11801 177672 -917553802 6 170123 -917590440 0 185021 -917604518 17280 205589 -917617860 1134 176451 -917655510 7 187502 -917684521 0 192677 -917693440 8344 177117 -917700645 7 170139 -917715790 8221 170144 -917761528 34825 170144 -917743080 2 180735 -917800019 7 170153 -917830513 1...
result:
wrong answer 1st lines differ - expected: '69999', found: '182384'
Subtask #4:
score: 0
Wrong Answer
Test #17:
score: 12
Accepted
time: 303ms
memory: 35428kb
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:
769608 34960 1162375 626228 2 419792 493364 419754 1817301 15 838740 515085 769584 0 0 659707 915948 209895 208656 69972 1251384 277968 1231290 517455 825285 560784 417892 349256 489692 979020 139930 139938 209902 1518040 1146693 287344 403088 1 279861 209896 699603 732904 723193 769575 140252 10391...
result:
ok 69973 lines
Test #18:
score: 12
Accepted
time: 417ms
memory: 38556kb
input:
70000 6 12580 20937 2 31244 33335 1 62095 66946 0 2558 64306 2 38762 111 2 64974 13699 1 43807 54819 1 20265 66474 3 56426 21940 0 17487 26140 2 11417 36996 2 640 46428 0 45138 54845 1 50554 13418 3 66554 43463 0 54345 51628 2 50263 55697 1 11292 25053 1 29900 10115 2 54500 17279 1 54173 57033 3 608...
output:
95678 275287413 81937227 47960445 176569 64768 441678666 980093268 490395803 549945 2442380991 572294079 0 1210579335 0 23682745 0 901519414 203750140 923453439 104544 293252454 0 514830 915152602 1023643716 710424 0 1138664466 696145940 2080089300 0 0 589624 1220178831 51949 2340422751 193419 90424...
result:
ok 70000 lines
Test #19:
score: 0
Wrong Answer
time: 441ms
memory: 39112kb
input:
70000 10 45546 20793 0 44801 49720 0 54732 9736 0 64375 18647 0 5128 61973 0 43544 39499 0 7553 64059 0 52671 60698 0 3856 66222 0 22937 53710 0 37212 17625 0 4088 60067 0 4594 67707 0 43329 36060 0 16713 10870 0 26623 56853 0 52301 6112 0 4772 42600 0 30359 51840 0 15979 7826 0 18168 47506 0 25681 ...
output:
139998 1302541392 0 139998 0 0 139998 0 0 139998 0 0 139998 0 0 206217054 0 69999 209997 -27579606 0 209997 0 69999 190467279 -34229511 448203597 279996 0 0 279996 0 0 279996 0 0 279996 0 0 279996 0 0 279996 0 891647262 279996 18689733 0 279996 0 0 279996 0 0 279996 0 0 279996 565591920 0 279996 0 6...
result:
wrong answer 1st lines differ - expected: '0', found: '139998'
Subtask #5:
score: 0
Skipped
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
0%