QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#749979 | #2429. Conquer The World | LIUIR | TL | 324ms | 22360kb | C++14 | 2.7kb | 2024-11-15 11:49:13 | 2024-11-15 11:49:14 |
Judging History
answer
/*
Author: LIUIR
Created: 2024.11.15 11:32:44
Last Modified: 2024.11.15 11:48:00
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2.5e5 + 5;
const int M = 1e6 + 5;
const ll INF = 1e12;
int n, rtS[N], rtT[N];
ll ans, a[N], dis[N];
vector<int> edge[N];
vector<ll> val[N];
class Heap{
private:
int cur = 0;
struct Node{
int ls, rs, dist;
ll val;
bool operator < (const Node& rhs){return val < rhs.val;}
}heap[M << 2];
public:
Heap(){heap[0].dist = -1;}
int New(ll val)
{
cur++;
heap[cur] = Node{0, 0, 0, val};
return cur;
}
int Merge(int u, int v)
{
if (!u || !v)
return u | v;
if (heap[u].val > heap[v].val)
swap(u, v);
heap[u].rs = Merge(heap[u].rs, v);
if (heap[heap[u].ls].dist < heap[heap[u].rs].dist)
swap(heap[u].ls, heap[u].rs);
return u;
}
void Delete(int& rt){rt = Merge(heap[rt].ls, heap[rt].rs);}
friend void Work(int);
}heap;
void Work(int);
void Dfs(int, int);
signed main()
{
scanf("%d", &n);
for (int i = 1, u, v, w; i < n; i++)
{
scanf("%d%d%d", &u, &v, &w);
edge[u].push_back(v);
edge[v].push_back(u);
val[u].push_back(w);
val[v].push_back(w);
}
for (int i = 1, x, y; i <= n; i++)
scanf("%d%d", &x, &y), a[i] = x - y;
Dfs(1, 0);
for (int i = 1; i <= n; i++)if (a[i] < 0)
ans += -a[i] * INF;
printf("%lld\n", ans);
return 0;
}
void Work(int u)
{
while(rtS[u] && rtT[u] && heap.heap[rtS[u]].val + heap.heap[rtT[u]].val - 2ll * dis[u] < 0)
{
ll s = heap.heap[rtS[u]].val, t = heap.heap[rtT[u]].val;
ans += s + t - 2ll * dis[u];
heap.Delete(rtS[u]), heap.Delete(rtT[u]);
rtS[u] = heap.Merge(rtS[u], heap.New(2ll * dis[u] - t));
rtT[u] = heap.Merge(rtT[u], heap.New(2ll * dis[u] - s));
}
}
void Dfs(int u, int fa)
{
if (a[u] > 0)
{
for (int i = 1; i <= a[u]; i++)
rtS[u] = heap.Merge(rtS[u], heap.New(dis[u]));
}
else
{
for (int i = 1; i <= -a[u]; i++)
rtT[u] = heap.Merge(rtT[u], heap.New(dis[u] - INF));
}
for (int i = 0; i < edge[u].size(); i++)
{
int v = edge[u][i];
if (v == fa)
continue;
dis[v] = dis[u] + val[u][i];
Dfs(v, u);
rtS[u] = heap.Merge(rtS[u], rtS[v]);
rtT[u] = heap.Merge(rtT[u], rtT[v]);
}
Work(u);
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 20268kb
Test #2:
score: 0
Accepted
time: 0ms
memory: 20360kb
Test #3:
score: 0
Accepted
time: 0ms
memory: 18224kb
Test #4:
score: 0
Accepted
time: 3ms
memory: 20208kb
Test #5:
score: 0
Accepted
time: 3ms
memory: 18320kb
Test #6:
score: 0
Accepted
time: 13ms
memory: 18464kb
Test #7:
score: 0
Accepted
time: 9ms
memory: 22360kb
Test #8:
score: 0
Accepted
time: 324ms
memory: 21004kb
Test #9:
score: -100
Time Limit Exceeded