QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#643111#2429. Conquer The WorldHJY2022TL 740ms112468kbC++141.7kb2024-10-15 18:58:062024-10-15 18:58:07

Judging History

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

  • [2024-10-15 18:58:07]
  • 评测
  • 测评结果:TL
  • 用时:740ms
  • 内存:112468kb
  • [2024-10-15 18:58:06]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mk make_pair
#define fir first
#define sec second
const int MX = 2e6 + 7,inf = 1e9;
ll val[MX],dep[MX];ll ans = 0;
vector<pair<int,ll > > g[MX];
int pool[MX],top = 0;
struct node{int l,r;ll x;}s[MX << 1];
int rA[MX],rB[MX];
int newnode(int x){int cur = pool[top];top--;s[cur].x = x;s[cur].l = s[cur].r = 0;return cur;}
int merge(int x,int y){
	if(!x || !y)return x | y;
	if(s[x].x > s[y].x)swap(x,y);
	if(rand() & 1)swap(s[x].l,s[x].r);
	s[x].r = merge(s[x].r,y);
	return x;
}

void solve(int x,int f){
	if(val[x] > 0)for(int i = 1;i <= val[x];i++)rA[x] = merge(rA[x],newnode(dep[x]));
	else for(int i = 1;i <= -val[x];i++)rB[x] = merge(rB[x],newnode(dep[x] - inf));
	for(auto it : g[x]){
		int to = it.fir;ll w = it.sec;
		if(f == to)continue;
		dep[to] = dep[x] + w;solve(to,x);rA[x] = merge(rA[x],rA[to]);rB[x] = merge(rB[x],rB[to]);
	}
	while(rA[x] && rB[x] && s[rA[x]].x + s[rB[x]].x - 2 * dep[x] < 0){
		ll u = s[rA[x]].x,v = s[rB[x]].x;ans += s[rA[x]].x + s[rB[x]].x - 2 * dep[x];
		pool[++top] = rA[x];pool[++top] = rB[x];
		rA[x] = merge(s[rA[x]].l,s[rA[x]].r);rB[x] = merge(s[rB[x]].l,s[rB[x]].r);
		rA[x] = merge(rA[x],newnode(2 * dep[x] - v));rB[x] = merge(rB[x],newnode(2 * dep[x] - u));
	}
}
int main(){
	ios :: sync_with_stdio(false);
	for(int i = 1;i < MX;i++){pool[i] = i;top = i;}
	int n;cin >> n;
	for(int i = 1;i < n;i++){int u,v;ll w;cin >> u >> v >> w;g[u].push_back(mk(v,w));g[v].push_back(mk(u,w));}
	for(int i = 1;i <= n;i++){int x,y;cin >> x >> y;val[i] = x - y;}
	for(int i = 1;i <= n;i++)if(val[i] < 0)ans += inf * -val[i];
	solve(1,1);
	cout << ans << '\n';
	return 0;
}

Details

Test #1:

score: 100
Accepted
time: 8ms
memory: 62948kb

Test #2:

score: 0
Accepted
time: 8ms
memory: 64948kb

Test #3:

score: 0
Accepted
time: 8ms
memory: 64932kb

Test #4:

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

Test #5:

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

Test #6:

score: 0
Accepted
time: 10ms
memory: 64992kb

Test #7:

score: 0
Accepted
time: 19ms
memory: 62908kb

Test #8:

score: 0
Accepted
time: 8ms
memory: 67032kb

Test #9:

score: 0
Accepted
time: 116ms
memory: 73336kb

Test #10:

score: 0
Accepted
time: 225ms
memory: 75308kb

Test #11:

score: 0
Accepted
time: 343ms
memory: 81528kb

Test #12:

score: 0
Accepted
time: 7ms
memory: 65152kb

Test #13:

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

Test #14:

score: 0
Accepted
time: 16ms
memory: 65288kb

Test #15:

score: 0
Accepted
time: 21ms
memory: 65188kb

Test #16:

score: 0
Accepted
time: 17ms
memory: 65080kb

Test #17:

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

Test #18:

score: 0
Accepted
time: 20ms
memory: 63128kb

Test #19:

score: 0
Accepted
time: 47ms
memory: 69112kb

Test #20:

score: 0
Accepted
time: 325ms
memory: 87748kb

Test #21:

score: 0
Accepted
time: 133ms
memory: 80292kb

Test #22:

score: 0
Accepted
time: 135ms
memory: 82068kb

Test #23:

score: 0
Accepted
time: 157ms
memory: 83944kb

Test #24:

score: 0
Accepted
time: 80ms
memory: 74992kb

Test #25:

score: 0
Accepted
time: 91ms
memory: 76288kb

Test #26:

score: 0
Accepted
time: 164ms
memory: 83868kb

Test #27:

score: 0
Accepted
time: 171ms
memory: 91684kb

Test #28:

score: 0
Accepted
time: 175ms
memory: 86404kb

Test #29:

score: 0
Accepted
time: 239ms
memory: 100324kb

Test #30:

score: 0
Accepted
time: 516ms
memory: 110492kb

Test #31:

score: 0
Accepted
time: 327ms
memory: 106360kb

Test #32:

score: 0
Accepted
time: 253ms
memory: 102280kb

Test #33:

score: 0
Accepted
time: 296ms
memory: 102296kb

Test #34:

score: 0
Accepted
time: 740ms
memory: 108336kb

Test #35:

score: 0
Accepted
time: 514ms
memory: 100280kb

Test #36:

score: 0
Accepted
time: 648ms
memory: 104240kb

Test #37:

score: 0
Accepted
time: 492ms
memory: 112468kb

Test #38:

score: -100
Time Limit Exceeded