QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#460577 | #2429. Conquer The World | suomynonA | AC ✓ | 521ms | 194016kb | C++20 | 2.2kb | 2024-07-01 21:08:32 | 2024-07-01 21:08:32 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
const int N = 2.5e5, M = 8e6;
const i64 Inf = 1e12;
int n, a[N + 5];
std::vector<std::pair<int, int>> v[N + 5];
i64 ans;
struct Leftist_Tree {
struct Node { int ls, rs, siz, d; i64 val; }ar[M + 5];
int tot;
int merge(int x, int y) {
if (!x or !y) return x + y;
if (ar[x].val > ar[y].val) std::swap(x, y);
ar[x].rs = merge(ar[x].rs, y);
if (ar[ar[x].rs].d > ar[ar[x].ls].d) std::swap(ar[x].ls, ar[x].rs);
ar[x].d = ar[ar[x].rs].d + 1;
ar[x].siz = ar[ar[x].ls].siz + ar[ar[x].rs].siz + 1;
return x;
}
void insert(int &x, i64 v) {
ar[++tot].val = v, ar[tot].siz = 1, ar[tot].d = 1, x = merge(x, tot);
}
void erase(int &x) { x = merge(ar[x].ls, ar[x].rs); }
int size(int x) { return ar[x].siz; }
i64 top(int x) { return ar[x].val; }
}tr;
int rta[N + 5], rtb[N + 5];
std::vector<i64> ar, br;
i64 dep[N + 5];
void dfs(int x, int fa) {
for (auto [i, c] : v[x]) if (i != fa) {
dep[i] = dep[x] + c, dfs(i, x);
rta[x] = tr.merge(rta[x], rta[i]), rtb[x] = tr.merge(rtb[x], rtb[i]);
}
for (int i = 1; i <= std::abs(a[x]); ++i) {
if (a[x] > 0) tr.insert(rta[x], dep[x]);
else tr.insert(rtb[x], dep[x] - Inf);
}
while (tr.size(rta[x]) and tr.size(rtb[x])) {
i64 p = tr.top(rta[x]), q = tr.top(rtb[x]);
if (p + q - 2 * dep[x] < 0) {
ans += p + q - 2 * dep[x];
ar.push_back(2 * dep[x] - q), br.push_back(2 * dep[x] - p);
tr.erase(rta[x]), tr.erase(rtb[x]);
}else break;
}
for (auto i : ar) tr.insert(rta[x], i);
for (auto i : br) tr.insert(rtb[x], i);
ar.clear(), br.clear();
}
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
std::cin >> n;
for (int i = 1, x, y, z; i < n; ++i) {
std::cin >> x >> y >> z;
v[x].push_back({y, z}), v[y].push_back({x, z});
}
for (int i = 1, x, y; i <= n; ++i) {
std::cin >> x >> y, a[i] = x - y;
if (a[i] < 0) ans -= a[i] * Inf;
}
dfs(1, 0), std::cout << ans << "\n";
return 0;
}
Details
Test #1:
score: 100
Accepted
time: 0ms
memory: 5672kb
Test #2:
score: 0
Accepted
time: 0ms
memory: 5728kb
Test #3:
score: 0
Accepted
time: 1ms
memory: 7784kb
Test #4:
score: 0
Accepted
time: 1ms
memory: 5716kb
Test #5:
score: 0
Accepted
time: 1ms
memory: 5668kb
Test #6:
score: 0
Accepted
time: 1ms
memory: 5680kb
Test #7:
score: 0
Accepted
time: 1ms
memory: 5724kb
Test #8:
score: 0
Accepted
time: 2ms
memory: 7864kb
Test #9:
score: 0
Accepted
time: 40ms
memory: 20020kb
Test #10:
score: 0
Accepted
time: 72ms
memory: 34352kb
Test #11:
score: 0
Accepted
time: 108ms
memory: 37376kb
Test #12:
score: 0
Accepted
time: 1ms
memory: 3816kb
Test #13:
score: 0
Accepted
time: 1ms
memory: 3700kb
Test #14:
score: 0
Accepted
time: 2ms
memory: 3964kb
Test #15:
score: 0
Accepted
time: 2ms
memory: 4248kb
Test #16:
score: 0
Accepted
time: 1ms
memory: 4024kb
Test #17:
score: 0
Accepted
time: 2ms
memory: 4272kb
Test #18:
score: 0
Accepted
time: 4ms
memory: 6056kb
Test #19:
score: 0
Accepted
time: 11ms
memory: 10584kb
Test #20:
score: 0
Accepted
time: 94ms
memory: 57580kb
Test #21:
score: 0
Accepted
time: 80ms
memory: 21676kb
Test #22:
score: 0
Accepted
time: 78ms
memory: 21620kb
Test #23:
score: 0
Accepted
time: 84ms
memory: 23688kb
Test #24:
score: 0
Accepted
time: 48ms
memory: 16708kb
Test #25:
score: 0
Accepted
time: 57ms
memory: 16664kb
Test #26:
score: 0
Accepted
time: 96ms
memory: 24420kb
Test #27:
score: 0
Accepted
time: 82ms
memory: 38812kb
Test #28:
score: 0
Accepted
time: 94ms
memory: 40568kb
Test #29:
score: 0
Accepted
time: 117ms
memory: 50540kb
Test #30:
score: 0
Accepted
time: 184ms
memory: 105260kb
Test #31:
score: 0
Accepted
time: 148ms
memory: 71936kb
Test #32:
score: 0
Accepted
time: 143ms
memory: 57556kb
Test #33:
score: 0
Accepted
time: 131ms
memory: 54392kb
Test #34:
score: 0
Accepted
time: 256ms
memory: 89196kb
Test #35:
score: 0
Accepted
time: 186ms
memory: 51256kb
Test #36:
score: 0
Accepted
time: 213ms
memory: 59720kb
Test #37:
score: 0
Accepted
time: 230ms
memory: 114116kb
Test #38:
score: 0
Accepted
time: 235ms
memory: 194016kb
Test #39:
score: 0
Accepted
time: 487ms
memory: 154632kb
Test #40:
score: 0
Accepted
time: 213ms
memory: 79280kb
Test #41:
score: 0
Accepted
time: 274ms
memory: 91752kb
Test #42:
score: 0
Accepted
time: 229ms
memory: 71264kb
Test #43:
score: 0
Accepted
time: 224ms
memory: 73664kb
Test #44:
score: 0
Accepted
time: 217ms
memory: 81024kb
Test #45:
score: 0
Accepted
time: 257ms
memory: 98380kb
Test #46:
score: 0
Accepted
time: 1ms
memory: 5788kb
Test #47:
score: 0
Accepted
time: 90ms
memory: 30248kb
Test #48:
score: 0
Accepted
time: 1ms
memory: 5644kb
Test #49:
score: 0
Accepted
time: 107ms
memory: 47392kb
Test #50:
score: 0
Accepted
time: 86ms
memory: 23700kb
Test #51:
score: 0
Accepted
time: 1ms
memory: 7724kb
Test #52:
score: 0
Accepted
time: 1ms
memory: 7684kb
Test #53:
score: 0
Accepted
time: 1ms
memory: 5656kb
Test #54:
score: 0
Accepted
time: 1ms
memory: 5680kb
Test #55:
score: 0
Accepted
time: 1ms
memory: 5716kb
Test #56:
score: 0
Accepted
time: 1ms
memory: 7796kb
Test #57:
score: 0
Accepted
time: 1ms
memory: 5788kb
Test #58:
score: 0
Accepted
time: 5ms
memory: 8632kb
Test #59:
score: 0
Accepted
time: 61ms
memory: 24872kb
Test #60:
score: 0
Accepted
time: 81ms
memory: 35128kb
Test #61:
score: 0
Accepted
time: 239ms
memory: 111424kb
Test #62:
score: 0
Accepted
time: 318ms
memory: 113572kb
Test #63:
score: 0
Accepted
time: 416ms
memory: 134732kb
Test #64:
score: 0
Accepted
time: 434ms
memory: 135336kb
Test #65:
score: 0
Accepted
time: 521ms
memory: 136460kb
Test #66:
score: 0
Accepted
time: 519ms
memory: 136472kb
Test #67:
score: 0
Accepted
time: 274ms
memory: 127376kb
Test #68:
score: 0
Accepted
time: 316ms
memory: 158376kb
Test #69:
score: 0
Accepted
time: 457ms
memory: 168288kb
Test #70:
score: 0
Accepted
time: 479ms
memory: 167148kb