QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#72923 | #5421. Factories Once More | chenshi | WA | 2ms | 9740kb | C++ | 1.4kb | 2023-01-20 11:49:59 | 2023-01-20 11:50:00 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int o=2e5+10;
int n,K,h[o],cnt,rt[o],w[o],s[o],ls[o],rs[o];long long ans,v[o],flgk[o],flgb[o];mt19937 rnd(time(0));
struct Edge{int v,p,w;}e[o];
inline void add(int U,int V,int W){e[++cnt].v=V;e[cnt].p=h[U];e[h[U]=cnt].w=W;}
inline void ad(int x,long long k,long long b){if(x) v[x]+=b+k*s[ls[x]],flgk[x]+=k,flgb[x]+=b;}
inline void pushdown(int id){ad(ls[id],flgk[id],flgb[id]);ad(rs[id],flgk[id],flgb[id]);flgk[id]=flgb[id]=0;}
inline void update(int id){s[id]=s[ls[id]]+s[rs[id]]+1;}
void split(int id,long long val,int&x,int&y){
if(!id){x=y=0;return;}
pushdown(id);
if(v[id]>=val) x=id,split(rs[id],val,rs[x],y);
else y=id,split(ls[id],val,x,ls[y]);
update(id);
}
void merge(int&id,int x,int y){
if(!x||!y){id=x|y;return;}
if(w[x]<w[y]) swap(x,y);
int t1=0,t2=0;
pushdown(id=x);split(y,v[x],t1,t2);
merge(ls[id],ls[x],t1);merge(rs[id],rs[x],t2);
update(id);
}
void dfs(int nw,int fa){
w[rt[nw]=nw]=rnd();s[nw]=1;
for(int i=h[nw];i;i=e[i].p) if(e[i].v^fa)
dfs(e[i].v,nw),ad(rt[e[i].v],-2*e[i].w,e[i].w*(K-1ll)),merge(rt[nw],rt[nw],rt[e[i].v]);
}
void Dfs(int nw,int val){
pushdown(nw);
if(ls[nw]) Dfs(ls[nw],val);
if(rs[nw]) Dfs(rs[nw],val-s[ls[nw]]-1);
if(val>s[ls[nw]]) ans+=v[nw];
}
int main(){
scanf("%d%d",&n,&K);
for(int i=1,u,v,w;i<n;++i) scanf("%d%d%d",&u,&v,&w),add(u,v,w),add(v,u,w);
dfs(1,0);
Dfs(rt[1],K);
printf("%lld",ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 9740kb
input:
6 3 1 2 3 2 3 2 2 4 1 1 5 2 5 6 3
output:
28
result:
wrong answer 1st numbers differ - expected: '22', found: '28'