QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#749993 | #2429. Conquer The World | LIUIR | AC ✓ | 570ms | 107324kb | C++14 | 2.9kb | 2024-11-15 11:53:08 | 2024-11-15 11:53:08 |
Judging History
answer
/*
Author: LIUIR
Created: 2024.11.15 11:32:44
Last Modified: 2024.11.15 11:52:37
*/
#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];
queue<int> q;
public:
Heap(){heap[0].dist = -1;}
int New(ll val)
{
int now;
if (q.empty())
now = ++cur;
else
now = q.front(), q.pop();
heap[now] = Node{0, 0, 0, val};
return now;
}
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);
heap[u].dist = heap[heap[u].rs].dist + 1;
return u;
}
void Delete(int& rt)
{
q.push(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);
}
Details
Test #1:
score: 100
Accepted
time: 3ms
memory: 18132kb
Test #2:
score: 0
Accepted
time: 0ms
memory: 20428kb
Test #3:
score: 0
Accepted
time: 3ms
memory: 20248kb
Test #4:
score: 0
Accepted
time: 0ms
memory: 20196kb
Test #5:
score: 0
Accepted
time: 3ms
memory: 20196kb
Test #6:
score: 0
Accepted
time: 0ms
memory: 22432kb
Test #7:
score: 0
Accepted
time: 0ms
memory: 20312kb
Test #8:
score: 0
Accepted
time: 0ms
memory: 20544kb
Test #9:
score: 0
Accepted
time: 24ms
memory: 31816kb
Test #10:
score: 0
Accepted
time: 46ms
memory: 36280kb
Test #11:
score: 0
Accepted
time: 65ms
memory: 43876kb
Test #12:
score: 0
Accepted
time: 0ms
memory: 20264kb
Test #13:
score: 0
Accepted
time: 0ms
memory: 18200kb
Test #14:
score: 0
Accepted
time: 5ms
memory: 20352kb
Test #15:
score: 0
Accepted
time: 0ms
memory: 20780kb
Test #16:
score: 0
Accepted
time: 1ms
memory: 20660kb
Test #17:
score: 0
Accepted
time: 0ms
memory: 18628kb
Test #18:
score: 0
Accepted
time: 0ms
memory: 21632kb
Test #19:
score: 0
Accepted
time: 6ms
memory: 28116kb
Test #20:
score: 0
Accepted
time: 85ms
memory: 53192kb
Test #21:
score: 0
Accepted
time: 99ms
memory: 37488kb
Test #22:
score: 0
Accepted
time: 102ms
memory: 37888kb
Test #23:
score: 0
Accepted
time: 104ms
memory: 39676kb
Test #24:
score: 0
Accepted
time: 67ms
memory: 32404kb
Test #25:
score: 0
Accepted
time: 72ms
memory: 34136kb
Test #26:
score: 0
Accepted
time: 115ms
memory: 39900kb
Test #27:
score: 0
Accepted
time: 97ms
memory: 53244kb
Test #28:
score: 0
Accepted
time: 104ms
memory: 47684kb
Test #29:
score: 0
Accepted
time: 151ms
memory: 64992kb
Test #30:
score: 0
Accepted
time: 231ms
memory: 81804kb
Test #31:
score: 0
Accepted
time: 194ms
memory: 71508kb
Test #32:
score: 0
Accepted
time: 156ms
memory: 67464kb
Test #33:
score: 0
Accepted
time: 177ms
memory: 65392kb
Test #34:
score: 0
Accepted
time: 380ms
memory: 73624kb
Test #35:
score: 0
Accepted
time: 201ms
memory: 65696kb
Test #36:
score: 0
Accepted
time: 246ms
memory: 69492kb
Test #37:
score: 0
Accepted
time: 259ms
memory: 84120kb
Test #38:
score: 0
Accepted
time: 388ms
memory: 105172kb
Test #39:
score: 0
Accepted
time: 330ms
memory: 98388kb
Test #40:
score: 0
Accepted
time: 266ms
memory: 73700kb
Test #41:
score: 0
Accepted
time: 271ms
memory: 79648kb
Test #42:
score: 0
Accepted
time: 230ms
memory: 73796kb
Test #43:
score: 0
Accepted
time: 222ms
memory: 72552kb
Test #44:
score: 0
Accepted
time: 217ms
memory: 79040kb
Test #45:
score: 0
Accepted
time: 249ms
memory: 83904kb
Test #46:
score: 0
Accepted
time: 0ms
memory: 20164kb
Test #47:
score: 0
Accepted
time: 59ms
memory: 44644kb
Test #48:
score: 0
Accepted
time: 4ms
memory: 20444kb
Test #49:
score: 0
Accepted
time: 142ms
memory: 62924kb
Test #50:
score: 0
Accepted
time: 115ms
memory: 39900kb
Test #51:
score: 0
Accepted
time: 3ms
memory: 18424kb
Test #52:
score: 0
Accepted
time: 0ms
memory: 20196kb
Test #53:
score: 0
Accepted
time: 0ms
memory: 20168kb
Test #54:
score: 0
Accepted
time: 0ms
memory: 18412kb
Test #55:
score: 0
Accepted
time: 0ms
memory: 20196kb
Test #56:
score: 0
Accepted
time: 0ms
memory: 20200kb
Test #57:
score: 0
Accepted
time: 3ms
memory: 20248kb
Test #58:
score: 0
Accepted
time: 8ms
memory: 19144kb
Test #59:
score: 0
Accepted
time: 70ms
memory: 34748kb
Test #60:
score: 0
Accepted
time: 112ms
memory: 48684kb
Test #61:
score: 0
Accepted
time: 281ms
memory: 79984kb
Test #62:
score: 0
Accepted
time: 387ms
memory: 85716kb
Test #63:
score: 0
Accepted
time: 264ms
memory: 87396kb
Test #64:
score: 0
Accepted
time: 266ms
memory: 86524kb
Test #65:
score: 0
Accepted
time: 559ms
memory: 87208kb
Test #66:
score: 0
Accepted
time: 570ms
memory: 90008kb
Test #67:
score: 0
Accepted
time: 244ms
memory: 92340kb
Test #68:
score: 0
Accepted
time: 266ms
memory: 104684kb
Test #69:
score: 0
Accepted
time: 328ms
memory: 105972kb
Test #70:
score: 0
Accepted
time: 326ms
memory: 107324kb