QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#701967 | #9165. Petrol stations | RDFZchenyy | 8 | 349ms | 32632kb | C++14 | 4.1kb | 2024-11-02 15:01:09 | 2024-11-02 15:01:10 |
Judging History
answer
#include<bits/stdc++.h>
using ll=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) now=f[now][i],t-=d[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 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);
if(atp&&q[atp].d==0) q[atp].c++;
else q[++atp].d=0,q[atp].c=1;
for(int i=1;i<=atp;i++){
q[i].s=q[i-1].s+q[i].c;
}
calc(u,0,1,atp,1);
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: 0
Wrong Answer
Test #1:
score: 18
Accepted
time: 3ms
memory: 11932kb
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: 12104kb
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: 0
Wrong Answer
time: 3ms
memory: 12020kb
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 911 0 0 760 0 0 0 24 989 0 493555 0 0 0 0 0 1996 979 890 1996 0 0 0 0 1996 0 0 0 0 0 0 0 254523 0 997 2 450646 0 244035 728 0 2043 0 0 0 0 0 0 0 0 0 0 0 0 10825 1996 0 0 0 0 997 0 0 0 0 1994 0 2 0 0 0 0 0 0 0 0 1968 0 0 0 0 3990 493900 0 0 0 0 0 0 3988 0 938 0 0 0 0 0 1996 0 2365 0 2491...
result:
wrong answer 5th lines differ - expected: '912', found: '911'
Subtask #2:
score: 8
Accepted
Test #13:
score: 8
Accepted
time: 0ms
memory: 9868kb
input:
2 1 0 1 1
output:
0 0
result:
ok 2 lines
Test #14:
score: 8
Accepted
time: 330ms
memory: 32552kb
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: 0
Wrong Answer
time: 349ms
memory: 32632kb
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:
174738 2614745 544919266 267682 175120 60041 35866 830159397 42488 715 193987 681945 96412 0 189369753 88636 54067 646344 116760 125814137 901114019 411310345 12046188 175338 1180870853 178366827 740116397 163349628 186310439 1222665736 1192655005 192217187 508267224 865255693 560408216 865530232 20...
result:
wrong answer 1st lines differ - expected: '450859', found: '174738'
Subtask #4:
score: 0
Wrong Answer
Test #17:
score: 0
Wrong Answer
time: 251ms
memory: 29344kb
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:
1226511 24837 1752428 1169569 4 279840 452432 839438 1368743 23 977228 484219 629604 0 0 1104800 649324 419788 242130 69967 1434810 130884 569296 494825 492309 275283 1311176 379238 69975 1506525 69965 349818 209890 714609 1463744 493520 256622 1 279855 209900 769552 740195 394450 699602 180199 1787...
result:
wrong answer 1st lines differ - expected: '769608', found: '1226511'
Subtask #5:
score: 0
Skipped
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #1:
0%