QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#53534 | #2429. Conquer The World | not_so_organic | AC ✓ | 208ms | 119884kb | C++11 | 3.1kb | 2022-10-05 13:12:31 | 2022-10-05 13:12:31 |
Judging History
answer
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
#define rig register int
#define getcha() (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 20, stdin), SS == TT) ? EOF : *SS++)
#define isint(a) ((a >= '0') && (a <= '9'))
char BB[1 << 20], *SS = BB, *TT = BB;
#define ll long long
#define swwap(a, b) (a ^= b, b ^= a, a ^= b)
#define minn(a, b) (a > b ? b : a)
inline ll read() {
ll s = 0;
char ch = getcha();
while (!isint(ch))
ch = getcha();
while (isint(ch))
s = (s << 3) + (s << 1) + (ch ^ 48), ch = getcha();
return s;
}
const int maxn = 250005;
const long long inf = 1000000000009;
struct node {
int x, y, h, rtx, rty;
} u[maxn];
struct bian {
int to, ne, w;
} ee[maxn << 1];
int cnt;
inline void add(int fr, int to, int w) {
ee[++cnt].to = to, ee[cnt].ne = u[fr].h, ee[cnt].w = w, u[fr].h = cnt;
}
struct merheap {
long long v;
int sum, d, ls, rs;
} em[maxn << 5];
int tot;
int merge(int a, int b) {
if (!a || !b)
return a | b;
if (em[a].v > em[b].v)
swwap(a, b);
if (em[a].v == em[b].v)
em[a].sum += em[b].sum, b = merge(em[b].ls, em[b].rs);
em[a].rs = merge(em[a].rs, b);
if (em[em[a].rs].d > em[em[a].ls].d)
swwap(em[a].ls, em[a].rs);
em[a].d = em[em[a].rs].d + 1;
return a;
}
inline int ne(long long v, int sum) {
em[++tot].v = v, em[tot].sum = sum, em[tot].d = 1;
return tot;
}
long long ans = 0;
inline void pop(int a, int b, long long d) {
while (u[a].rtx && u[b].rty) {
long long v = em[u[a].rtx].v, vv = em[u[b].rty].v;
if (v + vv - 2 * d >= 0)
return;
int sum = minn(em[u[a].rtx].sum, em[u[b].rty].sum);
ans += sum * (v + vv - 2 * d);
em[u[a].rtx].sum -= sum, em[u[b].rty].sum -= sum;
if (!em[u[a].rtx].sum)
u[a].rtx = merge(em[u[a].rtx].ls, em[u[a].rtx].rs);
if (!em[u[b].rty].sum)
u[b].rty = merge(em[u[b].rty].ls, em[u[b].rty].rs);
u[a].rtx = merge(u[a].rtx, ne(-vv + 2 * d, sum)), u[b].rty = merge(u[b].rty, ne(-v + 2 * d, sum));
}
}
void dfs(int x, int fa, long long d) {
if (u[x].y)
ans += inf * u[x].y, u[x].rty = ne(d - inf, u[x].y);
if (u[x].x)
u[x].rtx = ne(d, u[x].x);
for (rig i = u[x].h; i; i = ee[i].ne) {
if (ee[i].to == fa)
continue;
dfs(ee[i].to, x, d + ee[i].w);
pop(x, ee[i].to, d), pop(ee[i].to, x, d);
u[x].rtx = merge(u[x].rtx, u[ee[i].to].rtx), u[x].rty = merge(u[x].rty, u[ee[i].to].rty);
}
}
int main() {
int n = read();
for (rig i = 1, u, v, w; i < n; ++i)
u = read(), v = read(), w = read(), add(u, v, w), add(v, u, w);
for (rig i = 1, tmp; i <= n; ++i) {
u[i].x = read(), u[i].y = read();
tmp = minn(u[i].x, u[i].y);
u[i].x -= tmp, u[i].y -= tmp;
}
dfs(1, 0, 0);
printf("%lld\n", ans);
return 0;
}
Details
Test #1:
score: 100
Accepted
time: 3ms
memory: 3656kb
Test #2:
score: 0
Accepted
time: 2ms
memory: 3656kb
Test #3:
score: 0
Accepted
time: 3ms
memory: 3668kb
Test #4:
score: 0
Accepted
time: 2ms
memory: 3660kb
Test #5:
score: 0
Accepted
time: 2ms
memory: 3760kb
Test #6:
score: 0
Accepted
time: 2ms
memory: 3656kb
Test #7:
score: 0
Accepted
time: 1ms
memory: 3728kb
Test #8:
score: 0
Accepted
time: 2ms
memory: 3660kb
Test #9:
score: 0
Accepted
time: 2ms
memory: 3808kb
Test #10:
score: 0
Accepted
time: 2ms
memory: 3756kb
Test #11:
score: 0
Accepted
time: 2ms
memory: 3732kb
Test #12:
score: 0
Accepted
time: 2ms
memory: 3796kb
Test #13:
score: 0
Accepted
time: 3ms
memory: 3808kb
Test #14:
score: 0
Accepted
time: 2ms
memory: 3840kb
Test #15:
score: 0
Accepted
time: 0ms
memory: 4000kb
Test #16:
score: 0
Accepted
time: 2ms
memory: 3784kb
Test #17:
score: 0
Accepted
time: 2ms
memory: 3948kb
Test #18:
score: 0
Accepted
time: 2ms
memory: 3836kb
Test #19:
score: 0
Accepted
time: 0ms
memory: 3624kb
Test #20:
score: 0
Accepted
time: 3ms
memory: 3908kb
Test #21:
score: 0
Accepted
time: 34ms
memory: 14332kb
Test #22:
score: 0
Accepted
time: 25ms
memory: 14112kb
Test #23:
score: 0
Accepted
time: 29ms
memory: 15496kb
Test #24:
score: 0
Accepted
time: 11ms
memory: 11208kb
Test #25:
score: 0
Accepted
time: 18ms
memory: 11960kb
Test #26:
score: 0
Accepted
time: 37ms
memory: 16172kb
Test #27:
score: 0
Accepted
time: 32ms
memory: 17080kb
Test #28:
score: 0
Accepted
time: 18ms
memory: 22228kb
Test #29:
score: 0
Accepted
time: 30ms
memory: 21396kb
Test #30:
score: 0
Accepted
time: 68ms
memory: 19656kb
Test #31:
score: 0
Accepted
time: 48ms
memory: 23340kb
Test #32:
score: 0
Accepted
time: 48ms
memory: 21156kb
Test #33:
score: 0
Accepted
time: 62ms
memory: 19752kb
Test #34:
score: 0
Accepted
time: 92ms
memory: 23340kb
Test #35:
score: 0
Accepted
time: 36ms
memory: 17612kb
Test #36:
score: 0
Accepted
time: 63ms
memory: 21548kb
Test #37:
score: 0
Accepted
time: 75ms
memory: 31608kb
Test #38:
score: 0
Accepted
time: 208ms
memory: 119884kb
Test #39:
score: 0
Accepted
time: 57ms
memory: 30304kb
Test #40:
score: 0
Accepted
time: 68ms
memory: 26788kb
Test #41:
score: 0
Accepted
time: 53ms
memory: 31424kb
Test #42:
score: 0
Accepted
time: 68ms
memory: 35712kb
Test #43:
score: 0
Accepted
time: 71ms
memory: 36848kb
Test #44:
score: 0
Accepted
time: 62ms
memory: 41740kb
Test #45:
score: 0
Accepted
time: 69ms
memory: 36540kb
Test #46:
score: 0
Accepted
time: 2ms
memory: 3796kb
Test #47:
score: 0
Accepted
time: 2ms
memory: 3728kb
Test #48:
score: 0
Accepted
time: 2ms
memory: 3652kb
Test #49:
score: 0
Accepted
time: 63ms
memory: 20700kb
Test #50:
score: 0
Accepted
time: 27ms
memory: 15520kb
Test #51:
score: 0
Accepted
time: 2ms
memory: 3732kb
Test #52:
score: 0
Accepted
time: 0ms
memory: 3764kb
Test #53:
score: 0
Accepted
time: 2ms
memory: 3652kb
Test #54:
score: 0
Accepted
time: 2ms
memory: 3700kb
Test #55:
score: 0
Accepted
time: 2ms
memory: 3664kb
Test #56:
score: 0
Accepted
time: 3ms
memory: 3652kb
Test #57:
score: 0
Accepted
time: 2ms
memory: 3880kb
Test #58:
score: 0
Accepted
time: 0ms
memory: 4584kb
Test #59:
score: 0
Accepted
time: 18ms
memory: 12936kb
Test #60:
score: 0
Accepted
time: 35ms
memory: 20236kb
Test #61:
score: 0
Accepted
time: 93ms
memory: 33652kb
Test #62:
score: 0
Accepted
time: 135ms
memory: 35556kb
Test #63:
score: 0
Accepted
time: 12ms
memory: 15464kb
Test #64:
score: 0
Accepted
time: 25ms
memory: 15488kb
Test #65:
score: 0
Accepted
time: 50ms
memory: 31072kb
Test #66:
score: 0
Accepted
time: 49ms
memory: 31000kb
Test #67:
score: 0
Accepted
time: 58ms
memory: 47936kb
Test #68:
score: 0
Accepted
time: 79ms
memory: 55232kb
Test #69:
score: 0
Accepted
time: 61ms
memory: 39064kb
Test #70:
score: 0
Accepted
time: 57ms
memory: 38412kb