QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#72923#5421. Factories Once MorechenshiWA 2ms9740kbC++1.4kb2023-01-20 11:49:592023-01-20 11:50:00

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-20 11:50:00]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:9740kb
  • [2023-01-20 11:49:59]
  • 提交

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;
}

詳細信息

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'