QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#286363#5421. Factories Once Moreship2077WA 67ms15464kbC++142.2kb2023-12-17 19:07:232023-12-17 19:07:23

Judging History

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

  • [2023-12-17 19:07:23]
  • 评测
  • 测评结果:WA
  • 用时:67ms
  • 内存:15464kb
  • [2023-12-17 19:07:23]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
constexpr int M=1e5+5;vector<int>vec;
int n,m,x,y,z,rt[M],siz[M];LL ans;
vector<pair<int,int>>g[M];
namespace Treap{
	mt19937 mt(time(NULL));
	struct Treap{
		int ls,rs,rnd,siz;
		LL val,lazy1,lazy2;
	}tr[M]; int num;
	int new_node(int x){
		tr[++num].val=x;tr[num].siz=1;
		tr[num].rnd=mt();return num;
	}
	void pushup(int x){
		tr[x].siz=tr[tr[x].ls].siz+tr[tr[x].rs].siz+1;
	}
	void addtag(int x,LL k,LL b){
		tr[x].val+=(tr[tr[x].ls].siz+1)*k+b;
		tr[x].lazy1+=k;tr[x].lazy2+=b;
	}
	void pushdown(int x){
		if (tr[x].lazy1||tr[x].lazy2)
			addtag(tr[x].ls,tr[x].lazy1,tr[x].lazy2),
			addtag(tr[x].rs,tr[x].lazy1,tr[x].lazy1*(tr[tr[x].ls].siz+1)+tr[x].lazy2),
			tr[x].lazy1=tr[x].lazy2=0;
	}
	void split(int now,LL k,int &x,int &y){
		if (!now) return x=y=0,void(); pushdown(now);
		if (tr[now].val>k||tr[now].val==k&&mt()&1)
			x=now,split(tr[x].rs,k,tr[x].rs,y);
		else y=now,split(tr[y].ls,k,x,tr[y].ls);
		pushup(now);
	}
	int merge(int x,int y){
		if (!x||!y) return x|y;
		if (tr[x].rnd<tr[y].rnd){
			pushdown(x);
			tr[x].rs=merge(tr[x].rs,y);
			pushup(x); return x;
		}
		pushdown(y);
		tr[y].ls=merge(x,tr[y].ls);
		pushup(y); return y;
	}
	int Merge(int x,int y){
		if (!x||!y) return x|y;
		if (tr[x].rnd>tr[y].rnd) swap(x,y);
		pushdown(x);int a,b;
		split(y,tr[x].val,a,b);
		tr[x].ls=Merge(tr[x].ls,a);
		tr[x].rs=Merge(tr[x].rs,b);
		return pushup(x),x;
	}
	void solve(int x){
		if (!x) return ;
		pushdown(x);
		solve(tr[x].ls);
		vec.push_back(tr[x].val);
		solve(tr[x].rs);
	}
}
int read(){
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)) {if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)) x=x*10+ch-48,ch=getchar();
	return x*f;
}
void dfs(int x,int f){
	siz[x]=1;
	rt[x]=Treap::new_node(0);
	for (auto [u,v]:g[x]){
		if (u==f) continue;
		dfs(u,x);siz[x]+=siz[u];
		Treap::addtag(rt[u],-2ll*v,1ll*(m+1)*v);
		rt[x]=Treap::Merge(rt[x],rt[u]);
	}
}
int main(){
	n=read();m=read();
	for (int i=1;i<n;i++){
		x=read();y=read();z=read();
		g[x].push_back({y,z});
		g[y].push_back({x,z});
	}
	dfs(1,0);Treap::solve(rt[1]);
	for (int i=0;i<m;i++)
		ans+=vec[i];
	printf("%lld\n",ans);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 6800kb

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

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: 2ms
memory: 7008kb

input:

2 2
1 2 1

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 67ms
memory: 15408kb

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:

4915539756

result:

ok 1 number(s): "4915539756"

Test #5:

score: -100
Wrong Answer
time: 58ms
memory: 15464kb

input:

100000 54
52645 54121 2692
53845 52739 658
88841 87795 9298
79147 80362 6720
80683 80909 7138
30882 28439 3197
85375 85227 6903
80229 77345 445
79601 78148 7956
15262 16666 8402
87894 95824 844
17024 12005 5687
63972 65707 8592
40510 45074 9135
50774 49596 7692
37825 38581 9735
18425 8926 1747
84473...

output:

25051325903

result:

wrong answer 1st numbers differ - expected: '42231195087', found: '25051325903'