QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#334616#5421. Factories Once MoreSkyjoyWA 90ms13408kbC++142.3kb2024-02-22 10:05:222024-02-22 10:05:23

Judging History

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

  • [2024-02-22 10:05:23]
  • 评测
  • 测评结果:WA
  • 用时:90ms
  • 内存:13408kb
  • [2024-02-22 10:05:22]
  • 提交

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 id,int &x,int k){
		if(!x){
			x=k,ch[x][0]=ch[x][1]=0,siz[x]=1;
			return;
		}
		pushdown(x);
		insert(id,ch[x][val[k]<val[x]],k);
		fa[ch[x][val[k]<val[x]]]=x;
		pushup(x);
	}
	void merge(int u,int v){
		if(siz[root[u]]<siz[root[v]])swap(root[u],root[v]);
		stk.clear();
		dfs(root[v]);
		int qwq;
		for(int tmp:stk){
			qwq=root[u];
			insert(u,qwq,tmp);
			splay(root[u],tmp);
		}
	}
}
I love AyaseEli;
void dfs1(int u,int fa){
	insert(u,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);
		merge(u,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;
}

详细

Test #1:

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

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: 0ms
memory: 3764kb

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: 0ms
memory: 3912kb

input:

2 2
1 2 1

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: -100
Wrong Answer
time: 90ms
memory: 13408kb

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:

62539681702

result:

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