QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#467943 | #2429. Conquer The World | xuqin | AC ✓ | 521ms | 76928kb | C++14 | 2.5kb | 2024-07-08 18:23:26 | 2024-07-08 18:23:27 |
Judging History
answer
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cassert>
#include<cmath>
#include<utility>
#define eb emplace_back
using namespace std;
const int maxn=2.5e5+10, inf=1e9+10;
const double eps=1e-10;
typedef long long LL;
typedef pair<int, int> pii;
inline int read() {
int x=0, f=1; char c=getchar();
for(; c<'0'||c>'9'; c=getchar()) if(c=='-') f=0;
for(; c>='0'&&c<='9'; c=getchar()) x=x*10+c-'0';
return f?x:-x;
}
struct pj{
int y, nxt, d;
} g[maxn<<1];
int len, la[maxn];
inline void add_e(int x, int y, int d) {g[++len].y=y; g[len].nxt=la[x]; la[x]=len; g[len].d=d;}
int a[maxn]; LL dis[maxn];
void dfs(int x, int F) {
for(int i=la[x]; i; i=g[i].nxt) {
int y=g[i].y;
if(y!=F) dis[y]=dis[x]+g[i].d, dfs(y, x);
}
}
const LL M=5e12;
struct tg{
int lc, rc, d; LL val;
} tr[2000020]; int cnt;
inline int add_n(LL val) {
int u=++cnt;
tr[u].val=val; tr[u].d=1;
tr[u].lc=tr[u].rc=0;
return u;
}
int merge(int x, int y) {
if(x==0||y==0) return x^y;
if(tr[x].val>tr[y].val) swap(x, y);
tr[x].rc=merge(tr[x].rc, y);
if(tr[tr[x].rc].d>tr[tr[x].lc].d) swap(tr[x].lc, tr[x].rc);
tr[x].d=tr[tr[x].rc].d+1;
return x;
}
//small
int up[maxn], dn[maxn];
LL ans;
void sol(int x, int F) {
// printf("x=%d dis[x]=%lld\n", x, dis[x]);
for(int i=la[x]; i; i=g[i].nxt) {
int y=g[i].y;
if(y!=F) sol(y, x), up[x]=merge(up[x], up[y]), dn[x]=merge(dn[x], dn[y]);
}
if(a[x]>0) {
for(int i=1; i<=a[x]; ++i) up[x]=merge(up[x], add_n(dis[x]));
} else if(a[x]<0) {
for(int i=1; i<=-a[x]; ++i) dn[x]=merge(dn[x], add_n(dis[x]-M));
}
while(up[x]&&dn[x]&&tr[up[x]].val+tr[dn[x]].val-2*dis[x]<0) {
int u=up[x], v=dn[x];
LL p=tr[u].val, q=tr[v].val;
ans+=p+q-2*dis[x];
up[x]=merge(tr[up[x]].lc, tr[up[x]].rc);
dn[x]=merge(tr[dn[x]].lc, tr[dn[x]].rc);
tr[u].val=2*dis[x]-q; tr[v].val=2*dis[x]-p;
tr[u].d=tr[v].d=1; tr[u].lc=tr[u].rc=tr[v].lc=tr[v].rc=0;
up[x]=merge(up[x], u); dn[x]=merge(dn[x], v);
}
}
int main() {
int n=read();
for(int i=1, x, y, d; i<n; ++i) {
x=read(), y=read(), d=read();
add_e(x, y, d); add_e(y, x, d);
}
int s=0;
for(int i=1; i<=n; ++i) {
a[i]=read(), a[i]-=read();
if(a[i]<0) s+=-a[i];
}
dfs(1, 0);
sol(1, 0);
printf("%lld", ans+M*s);
return 0;
}
Details
Test #1:
score: 100
Accepted
time: 0ms
memory: 3580kb
Test #2:
score: 0
Accepted
time: 0ms
memory: 3588kb
Test #3:
score: 0
Accepted
time: 0ms
memory: 3660kb
Test #4:
score: 0
Accepted
time: 0ms
memory: 1512kb
Test #5:
score: 0
Accepted
time: 0ms
memory: 3592kb
Test #6:
score: 0
Accepted
time: 0ms
memory: 3664kb
Test #7:
score: 0
Accepted
time: 0ms
memory: 3548kb
Test #8:
score: 0
Accepted
time: 1ms
memory: 3960kb
Test #9:
score: 0
Accepted
time: 32ms
memory: 16668kb
Test #10:
score: 0
Accepted
time: 53ms
memory: 20412kb
Test #11:
score: 0
Accepted
time: 82ms
memory: 28680kb
Test #12:
score: 0
Accepted
time: 0ms
memory: 3640kb
Test #13:
score: 0
Accepted
time: 0ms
memory: 1612kb
Test #14:
score: 0
Accepted
time: 0ms
memory: 3580kb
Test #15:
score: 0
Accepted
time: 1ms
memory: 3828kb
Test #16:
score: 0
Accepted
time: 1ms
memory: 1740kb
Test #17:
score: 0
Accepted
time: 1ms
memory: 5912kb
Test #18:
score: 0
Accepted
time: 3ms
memory: 3092kb
Test #19:
score: 0
Accepted
time: 10ms
memory: 8896kb
Test #20:
score: 0
Accepted
time: 88ms
memory: 36836kb
Test #21:
score: 0
Accepted
time: 37ms
memory: 12196kb
Test #22:
score: 0
Accepted
time: 31ms
memory: 12052kb
Test #23:
score: 0
Accepted
time: 41ms
memory: 13224kb
Test #24:
score: 0
Accepted
time: 22ms
memory: 11088kb
Test #25:
score: 0
Accepted
time: 27ms
memory: 10744kb
Test #26:
score: 0
Accepted
time: 42ms
memory: 13572kb
Test #27:
score: 0
Accepted
time: 46ms
memory: 29716kb
Test #28:
score: 0
Accepted
time: 53ms
memory: 23952kb
Test #29:
score: 0
Accepted
time: 63ms
memory: 38248kb
Test #30:
score: 0
Accepted
time: 154ms
memory: 55172kb
Test #31:
score: 0
Accepted
time: 97ms
memory: 43844kb
Test #32:
score: 0
Accepted
time: 69ms
memory: 40312kb
Test #33:
score: 0
Accepted
time: 70ms
memory: 38592kb
Test #34:
score: 0
Accepted
time: 295ms
memory: 45660kb
Test #35:
score: 0
Accepted
time: 128ms
memory: 38468kb
Test #36:
score: 0
Accepted
time: 162ms
memory: 40668kb
Test #37:
score: 0
Accepted
time: 177ms
memory: 56968kb
Test #38:
score: 0
Accepted
time: 284ms
memory: 76332kb
Test #39:
score: 0
Accepted
time: 283ms
memory: 70744kb
Test #40:
score: 0
Accepted
time: 194ms
memory: 46156kb
Test #41:
score: 0
Accepted
time: 189ms
memory: 51688kb
Test #42:
score: 0
Accepted
time: 157ms
memory: 45004kb
Test #43:
score: 0
Accepted
time: 140ms
memory: 46608kb
Test #44:
score: 0
Accepted
time: 155ms
memory: 48708kb
Test #45:
score: 0
Accepted
time: 191ms
memory: 55604kb
Test #46:
score: 0
Accepted
time: 0ms
memory: 3460kb
Test #47:
score: 0
Accepted
time: 79ms
memory: 28788kb
Test #48:
score: 0
Accepted
time: 0ms
memory: 3456kb
Test #49:
score: 0
Accepted
time: 63ms
memory: 38116kb
Test #50:
score: 0
Accepted
time: 33ms
memory: 13204kb
Test #51:
score: 0
Accepted
time: 0ms
memory: 1516kb
Test #52:
score: 0
Accepted
time: 0ms
memory: 1520kb
Test #53:
score: 0
Accepted
time: 0ms
memory: 3580kb
Test #54:
score: 0
Accepted
time: 0ms
memory: 3620kb
Test #55:
score: 0
Accepted
time: 0ms
memory: 1444kb
Test #56:
score: 0
Accepted
time: 0ms
memory: 3488kb
Test #57:
score: 0
Accepted
time: 0ms
memory: 1580kb
Test #58:
score: 0
Accepted
time: 0ms
memory: 2244kb
Test #59:
score: 0
Accepted
time: 38ms
memory: 13756kb
Test #60:
score: 0
Accepted
time: 41ms
memory: 22776kb
Test #61:
score: 0
Accepted
time: 196ms
memory: 54788kb
Test #62:
score: 0
Accepted
time: 286ms
memory: 58204kb
Test #63:
score: 0
Accepted
time: 251ms
memory: 58164kb
Test #64:
score: 0
Accepted
time: 240ms
memory: 58184kb
Test #65:
score: 0
Accepted
time: 520ms
memory: 59128kb
Test #66:
score: 0
Accepted
time: 521ms
memory: 59176kb
Test #67:
score: 0
Accepted
time: 183ms
memory: 65904kb
Test #68:
score: 0
Accepted
time: 215ms
memory: 75936kb
Test #69:
score: 0
Accepted
time: 291ms
memory: 76928kb
Test #70:
score: 0
Accepted
time: 275ms
memory: 76496kb