QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#334638#5421. Factories Once MoreSkyjoyWA 54ms13392kbC++142.5kb2024-02-22 10:46:392024-02-22 10:46:40

Judging History

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

  • [2024-02-22 10:46:40]
  • 评测
  • 测评结果:WA
  • 用时:54ms
  • 内存:13392kb
  • [2024-02-22 10:46:39]
  • 提交

answer

#include<bits/stdc++.h>
#define I using
#define love namespace
#define Elaina std
#define ll long long
I love Elaina;
const int N=100010;
ll read(){
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<3)+(x<<1)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
ll n,m,x,y,z,head[N],cnt,ans;
struct edge{
	int to,nxt;
	ll val;
}e[N<<1];
void addedge(int u,int v,ll w){e[++cnt].to=v,e[cnt].nxt=head[u],e[cnt].val=w,head[u]=cnt;}
namespace AyaseEli{
	int root[N],fa[N],ch[N][2];
	ll val[N],siz[N],tag1[N],tag2[N];
	vector<int>stk;
	void pushup(int k){siz[k]=siz[ch[k][0]]+siz[ch[k][1]]+1;}
	void pushtag(int k,int t1,int t2){val[k]+=t1+(siz[ch[k][0]]+1)*t2,tag1[k]+=t1,tag2[k]+=t2;}
	void pushdown(int k){
		if(ch[k][0])pushtag(ch[k][0],tag1[k],tag2[k]);
		if(ch[k][1])pushtag(ch[k][1],tag1[k]+1ll*(siz[ch[k][0]]+1)*tag2[k],tag2[k]);
		tag1[k]=tag2[k]=0;
	}
	void rotate(int x){
		int y=fa[x],z=fa[y];
		int k=x==ch[y][1];
		ch[y][k]=ch[x][k^1];
		if(ch[x][k^1])fa[ch[x][k^1]]=y;
		ch[x][k^1]=y,fa[y]=x,fa[x]=z;
		if(z)ch[z][y==ch[z][1]]=x;
		pushup(y),pushup(x);
	}
	void splay(int &rt,int x,int goal=0){
		for(int y=fa[x];y=fa[x],y!=goal;rotate(x)){
			int z=fa[y];
			if(z!=goal)rotate((x==ch[y][1])^(y==ch[z][1])?x:y);
		}
		if(!goal)rt=x;
	}
	void dfs(int u){
		if(!u)return;
		pushdown(u);
		dfs(ch[u][0]);
		stk.push_back(u);
		dfs(ch[u][1]);
	}
	void insert(int &rt,int k){
		int v=val[k];
		if(!rt){
			rt=k,ch[rt][0]=ch[rt][1]=0,siz[rt]=1;
			pushup(rt);
			return;
		}
		int x=rt,y=0;
		while(1){
			pushdown(x);
			y=x,x=ch[y][v<val[y]];
			if(!x){
				x=k,ch[x][0]=ch[x][1]=0,siz[x]=1,fa[x]=y,ch[y][v<val[y]]=x;
				pushup(x),pushup(y);
				break;
			}
		}
	}
	void merge(int &rt,int x){
		stk.clear();
		dfs(x);
		insert(rt,stk[0]),splay(rt,stk[0]);
		for(int i=1;i<stk.size();i++)insert(stk[i-1],stk[i]),splay(rt,stk[i]);
	}
}
I love AyaseEli;
void dfs1(int u,int fa){
	insert(root[u],u);
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].to;
		if(v==fa)continue;
		dfs1(v,u);
		pushtag(root[v],1ll*(m+1)*e[i].val,-2ll*e[i].val);
		if(siz[root[u]]<siz[root[v]])swap(root[u],root[v]);
		merge(root[u],root[v]);
	}
}
int main(){
	n=read(),m=read();
	for(int i=1;i<n;i++){
		x=read(),y=read(),z=read();
		addedge(x,y,z),addedge(y,x,z);
	}
	dfs1(1,0);
	stk.clear();
	dfs(root[1]);
	for(int i=0;i<m;i++)ans+=val[stk[i]];
	printf("%lld",ans);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5912kb

input:

6 3
1 2 3
2 3 2
2 4 1
1 5 2
5 6 3

output:

22

result:

ok 1 number(s): "22"

Test #2:

score: 0
Accepted
time: 1ms
memory: 5984kb

input:

4 3
1 2 2
1 3 3
1 4 4

output:

18

result:

ok 1 number(s): "18"

Test #3:

score: 0
Accepted
time: 1ms
memory: 5920kb

input:

2 2
1 2 1

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: -100
Wrong Answer
time: 54ms
memory: 13392kb

input:

100000 17
37253 35652 9892
56367 53643 1120
47896 49255 4547
93065 88999 1745
5251 6742 5031
49828 50972 8974
31548 46729 1032
56341 56287 4812
21896 22838 1682
82124 90557 7307
76289 76949 7028
33834 45380 6856
15499 15064 2265
10127 5251 9920
87208 93945 9487
68990 72637 6891
91640 85004 2259
4748...

output:

22309160602

result:

wrong answer 1st numbers differ - expected: '4915539756', found: '22309160602'