QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#643116 | #2429. Conquer The World | HJY2022 | TL | 684ms | 166124kb | C++14 | 1.7kb | 2024-10-15 18:59:25 | 2024-10-15 18:59:25 |
Judging History
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 = 4e6 + 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];
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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 16ms
memory: 112972kb
Test #2:
score: 0
Accepted
time: 12ms
memory: 112960kb
Test #3:
score: 0
Accepted
time: 8ms
memory: 112936kb
Test #4:
score: 0
Accepted
time: 20ms
memory: 112936kb
Test #5:
score: 0
Accepted
time: 16ms
memory: 113004kb
Test #6:
score: 0
Accepted
time: 19ms
memory: 113128kb
Test #7:
score: 0
Accepted
time: 11ms
memory: 113044kb
Test #8:
score: 0
Accepted
time: 25ms
memory: 113248kb
Test #9:
score: 0
Accepted
time: 126ms
memory: 122032kb
Test #10:
score: 0
Accepted
time: 249ms
memory: 124648kb
Test #11:
score: 0
Accepted
time: 325ms
memory: 129712kb
Test #12:
score: 0
Accepted
time: 21ms
memory: 113032kb
Test #13:
score: 0
Accepted
time: 12ms
memory: 113048kb
Test #14:
score: 0
Accepted
time: 26ms
memory: 114948kb
Test #15:
score: 0
Accepted
time: 15ms
memory: 113296kb
Test #16:
score: 0
Accepted
time: 20ms
memory: 113116kb
Test #17:
score: 0
Accepted
time: 12ms
memory: 114368kb
Test #18:
score: 0
Accepted
time: 12ms
memory: 114068kb
Test #19:
score: 0
Accepted
time: 35ms
memory: 117276kb
Test #20:
score: 0
Accepted
time: 331ms
memory: 135508kb
Test #21:
score: 0
Accepted
time: 121ms
memory: 130828kb
Test #22:
score: 0
Accepted
time: 109ms
memory: 130052kb
Test #23:
score: 0
Accepted
time: 103ms
memory: 133164kb
Test #24:
score: 0
Accepted
time: 75ms
memory: 123860kb
Test #25:
score: 0
Accepted
time: 73ms
memory: 127600kb
Test #26:
score: 0
Accepted
time: 127ms
memory: 132012kb
Test #27:
score: 0
Accepted
time: 144ms
memory: 138892kb
Test #28:
score: 0
Accepted
time: 138ms
memory: 135220kb
Test #29:
score: 0
Accepted
time: 194ms
memory: 147916kb
Test #30:
score: 0
Accepted
time: 488ms
memory: 163880kb
Test #31:
score: 0
Accepted
time: 300ms
memory: 153736kb
Test #32:
score: 0
Accepted
time: 246ms
memory: 153400kb
Test #33:
score: 0
Accepted
time: 264ms
memory: 150948kb
Test #34:
score: 0
Accepted
time: 684ms
memory: 158088kb
Test #35:
score: 0
Accepted
time: 512ms
memory: 153304kb
Test #36:
score: 0
Accepted
time: 620ms
memory: 153860kb
Test #37:
score: 0
Accepted
time: 468ms
memory: 166124kb
Test #38:
score: -100
Time Limit Exceeded