QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#334634#5421. Factories Once MoreSkyjoyWA 719ms13624kbC++142.5kb2024-02-22 10:44:242024-04-09 18:29:36

Judging History

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

  • [2024-04-09 18:29:36]
  • 管理员手动重测该提交记录
  • 测评结果:WA
  • 用时:719ms
  • 内存:13624kb
  • [2024-02-22 10:44:24]
  • 提交

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

详细

Test #1:

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

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: 5796kb

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: 6028kb

input:

2 2
1 2 1

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: -100
Wrong Answer
time: 719ms
memory: 13624kb

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:

36168083486

result:

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