QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#292672#2429. Conquer The WorldKevin5307RE 929ms84292kbC++203.6kb2023-12-28 10:55:512023-12-28 10:55:52

Judging History

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

  • [2023-12-28 10:55:52]
  • 评测
  • 测评结果:RE
  • 用时:929ms
  • 内存:84292kb
  • [2023-12-28 10:55:51]
  • 提交

answer

//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define int ll
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
vector<pii> G[300300];
int n;
int x[300300],y[300300];
int c[300300],f[300300];
int l[300300],r[300300];
int fval[300300];
namespace treap
{
	#define ls(x) nd[x].ls
	#define rs(x) nd[x].rs
	#define siz(x) nd[x].siz
	#define pri(x) nd[x].pri
	#define tag(x) nd[x].tag
	#define val(x) nd[x].val
	mt19937_64 rnd(32183332);
	struct node
	{
		int siz,val,tag;
		ull pri;
		int ls,rs;
		node(){}
	}nd[3003000];
	int tot;
	void pushdown(int x)
	{
		tag(ls(x))+=tag(x);
		tag(rs(x))+=tag(x);
		val(x)+=tag(x);
		tag(x)=0;
	}
	void pushup(int x)
	{
		pushdown(ls(x));
		pushdown(rs(x));
		siz(x)=siz(ls(x))+siz(rs(x))+1;
	}
	int merge(int x,int y)
	{
		if(!x||!y) return x+y;
		pushdown(x);
		pushdown(y);
		if(pri(x)>pri(y))
		{
			rs(x)=merge(rs(x),y);
			pushup(x);
			return x;
		}
		else
		{
			ls(y)=merge(x,ls(y));
			pushup(y);
			return y;
		}
	}
	void valSplit(int x,int v,int &a,int &b)
	{
		if(!x)
		{
			a=b=0;
			return ;
		}
		pushdown(x);
		if(val(x)<=v)
		{
			a=x;
			valSplit(rs(x),v,rs(a),b);
			pushup(a);
		}
		else
		{
			b=x;
			valSplit(ls(x),v,a,ls(b));
			pushup(b);
		}
	}
	void sizSplit(int x,int s,int &a,int &b)
	{
		if(!x)
		{
			a=b=0;
			return ;
		}
		pushdown(x);
		if(siz(ls(x))>=s)
		{
			b=x;
			sizSplit(ls(x),s,a,ls(b));
			pushup(b);
		}
		else
		{
			a=x;
			sizSplit(rs(x),s-siz(ls(x))-1,rs(a),b);
			pushup(a);
		}
	}
	int insert(int x,int val)
	{
		int a,b;
		valSplit(x,val,a,b);
		tot++;
		val(tot)=val;
		siz(tot)=1;
		pri(tot)=rnd();
		return merge(a,merge(tot,b));
	}
	void unfold(int x,vector<int> &vec)
	{
		if(!x) return ;
		pushdown(x);
		vec.pb(val(x));
		unfold(ls(x),vec);
		unfold(rs(x),vec);
	}
	int unordered_merge(int x,int y)
	{
		if(siz(x)<siz(y))
			swap(x,y);
		vector<int> vec;
		unfold(y,vec);
		for(auto val:vec)
			x=insert(x,val);
		return x;
	}
	int change(int x,int len,int d)
	{
		int a,b;
		sizSplit(x,len,a,b);
		tag(a)-=d;
		tag(b)+=d;
		return merge(a,b);
	}
	pii front(int x)
	{
		int a,b;
		sizSplit(x,1,a,b);
		return mp(val(a),b);
	}
}
int dfs(int u,int fa)
{
	int ret=0;
	for(auto pr:G[u])
		if(pr.first!=fa)
		{
			c[pr.first]=pr.second;
			ret=treap::unordered_merge(ret,dfs(pr.first,u));
			l[u]+=l[pr.first];
			r[u]+=r[pr.first];
			fval[u]+=fval[pr.first];
		}
	for(int i=0;i<x[u];i++)
		ret=treap::insert(ret,0);
	l[u]+=y[u]-x[u];
	r[u]+=y[u];
	fval[u]+=abs(l[u])*c[u];
	ret=treap::change(ret,max(0ll,-l[u]),c[u]);
	return ret;
}
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=1;i<n;i++)
	{
		int u,v,c;
		cin>>u>>v>>c;
		G[u].pb(v,c);
		G[v].pb(u,c);
	}
	for(int i=1;i<=n;i++)
		cin>>x[i]>>y[i];
	int rt=dfs(1,0);
	assert(l[1]<=0&&r[1]>=0);
	ll ans=fval[1];
	for(int i=0;i<-l[1];i++)
	{
		pii pr=treap::front(rt);
		ans+=pr.first;
		rt=pr.second;
	}
	cout<<ans<<endl;
	return 0;
}

詳細信息

Test #1:

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

Test #2:

score: 0
Accepted
time: 4ms
memory: 22192kb

Test #3:

score: 0
Accepted
time: 0ms
memory: 23016kb

Test #4:

score: 0
Accepted
time: 0ms
memory: 22616kb

Test #5:

score: 0
Accepted
time: 0ms
memory: 22220kb

Test #6:

score: 0
Accepted
time: 5ms
memory: 24484kb

Test #7:

score: 0
Accepted
time: 3ms
memory: 21584kb

Test #8:

score: 0
Accepted
time: 6ms
memory: 21572kb

Test #9:

score: 0
Accepted
time: 691ms
memory: 70624kb

Test #10:

score: 0
Accepted
time: 929ms
memory: 84292kb

Test #11:

score: 0
Accepted
time: 728ms
memory: 75752kb

Test #12:

score: 0
Accepted
time: 6ms
memory: 21756kb

Test #13:

score: 0
Accepted
time: 0ms
memory: 23008kb

Test #14:

score: 0
Accepted
time: 3ms
memory: 21504kb

Test #15:

score: 0
Accepted
time: 14ms
memory: 24644kb

Test #16:

score: 0
Accepted
time: 9ms
memory: 24684kb

Test #17:

score: 0
Accepted
time: 12ms
memory: 24584kb

Test #18:

score: 0
Accepted
time: 117ms
memory: 36992kb

Test #19:

score: 0
Accepted
time: 531ms
memory: 65648kb

Test #20:

score: -100
Runtime Error