QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#335700 | #2429. Conquer The World | cz_yxx | AC ✓ | 3540ms | 105284kb | C++20 | 4.3kb | 2024-02-23 19:58:43 | 2024-02-23 19:58:44 |
Judging History
answer
//私は猫です
#include <bits/stdc++.h>
using namespace std;
// #define int long long
int read(){
int xx = 0, f = 1; char ch = getchar();
for (;!isdigit(ch); ch = getchar())
f = (ch == '-' ? -1 : 1);
for (; isdigit(ch); ch = getchar())
xx = (xx << 3) + (xx << 1) + ch - '0';
return xx * f;
}
typedef long long ll;
const int N = 250100, M = 2000100;
int n, a[N];
struct edge{
int v; ll w;
};
vector <edge> e[N];
int cnt = 0;
struct node{
ll slp, lt; int ls, rs, siz;
bool fl; ll sum;
int pri;
}tr[M << 1];
int addp(ll x){
tr[++cnt] = node{x, 0, 0, 0, 1, false, x, rand() * rand()};
return cnt;
}
void up(int x){
tr[x].siz = tr[tr[x].ls].siz + tr[tr[x].rs].siz + 1;
tr[x].sum = tr[tr[x].ls].sum + tr[tr[x].rs].sum + tr[x].slp;
}
void add(int x, ll val){
if (x == 0)return ;
tr[x].slp += val, tr[x].lt += val, tr[x].sum += 1ll * tr[x].siz * val;
}
void upd(int x){
if (x == 0)return ;
tr[x].slp = 0; tr[x].lt = 0, tr[x].sum = 0;
tr[x].fl = true;
}
void down(int x){
if (tr[x].fl == true){
upd(tr[x].ls), upd(tr[x].rs);
tr[x].fl = false;
}
if (tr[x].lt != 0){
add(tr[x].ls, tr[x].lt), add(tr[x].rs, tr[x].lt);
tr[x].lt = 0;
}
}
void splitsiz(int u, int x, int &L, int &R){
if (u == 0){
L = R = 0; return ;
}
down(u);
if (tr[tr[u].ls].siz + 1 <= x){
L = u; splitsiz(tr[u].rs, x - tr[tr[u].ls].siz - 1, tr[u].rs, R);
}
else {
R = u; splitsiz(tr[u].ls, x, L, tr[u].ls);
}
up(u);
}
void splitval(int u, ll x, int &L, int &R){
if (u == 0){
L = R = 0; return ;
}
down(u);
if (tr[u].slp <= x){
L = u; splitval(tr[u].rs, x, tr[u].rs, R);
}
else {
R = u; splitval(tr[u].ls, x, L, tr[u].ls);
}
up(u);
}
int merge(int L, int R){
if (L == 0 || R == 0)return L + R;
down(L), down(R);
if (tr[L].pri >= tr[R].pri){
tr[L].rs = merge(tr[L].rs, R); up(L); return L;
}
else {
tr[R].ls = merge(L, tr[R].ls); up(R); return R;
}
}
// int merg(int L, int R){
// if (L == 0 || R == 0)return L + R;
// down(L), down(R);
// if (tr[L].pri < tr[R].pri)swap(L, R);
// int ll, rr; splitval(R, tr[L].slp, ll, rr);
// tr[L].ls = merg(tr[L].ls, ll), tr[L].rs = merg(tr[L].rs, rr);
// up(L); return L;
// }
int merg(int L, int R){
if (tr[L].siz < tr[R].siz)swap(L, R);
if (R == 0)return L;
down(R);
L = merg(L, tr[R].ls); L = merg(L, tr[R].rs);
tr[R].ls = tr[R].rs = 0, tr[R].siz = 1, tr[R].sum = tr[R].slp;
int ll, rr; splitval(L, tr[R].slp, ll, rr);
L = merge(merge(ll, R), rr); return L;
}
struct root{
int rt; ll rlx, rly;
}rt[N];
void mg(int x, int v){
rt[x].rt = merg(rt[x].rt, rt[v].rt);
rt[x].rlx += rt[v].rlx;
rt[x].rly += rt[v].rly;
}
void out(int x){
if (x == 0)return ;
down(x);
out(tr[x].ls);
printf("%lld ", tr[x].slp);
out(tr[x].rs);
}
void dfs(int x, int fa, ll c){
rt[x] = root{0, 0, 0};
for (edge vv : e[x])if (vv.v != fa){
dfs(vv.v, x, vv.w); mg(x, vv.v);
}
int ll, rr;
if (a[x] > 0){
for (int i = 1; i <= a[x]; ++i)rt[x].rt = merg(rt[x].rt, addp(0));
splitval(rt[x].rt, 0, ll, rr);
rt[x].rly += tr[ll].sum; upd(ll);
rt[x].rt = merge(ll, rr);
}
else {
splitval(rt[x].rt, 0, ll, rr);
rt[x].rly += tr[ll].sum; upd(ll);
rt[x].rt = merge(ll, rr); rt[x].rlx += a[x];
}
if (x != fa){
splitsiz(rt[x].rt, -rt[x].rlx, ll, rr);
add(ll, -c), add(rr, c); rt[x].rt = merge(ll, rr);
rt[x].rly -= rt[x].rlx * c;
}
}
ll ans;
signed main(){
srand(time(0));
n = read();
for (int i = 1, u, v, w; i < n; ++i){
u = read(), v = read(), w = read();
e[u].push_back(edge{v, w});
e[v].push_back(edge{u, w});
}
for (int i = 1, u, v; i <= n; ++i){
u = read(), v = read(); a[i] = u - v;
}
dfs(1, 1, 0);
ans = rt[1].rly; int ll, rr, md;
splitsiz(rt[1].rt, -rt[1].rlx, ll, rr); splitval(rr, 0, md, rr);
ans += tr[ll].sum + tr[md].sum;
printf("%lld\n", ans);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 5976kb
Test #2:
score: 0
Accepted
time: 1ms
memory: 3948kb
Test #3:
score: 0
Accepted
time: 1ms
memory: 5972kb
Test #4:
score: 0
Accepted
time: 1ms
memory: 6096kb
Test #5:
score: 0
Accepted
time: 1ms
memory: 3932kb
Test #6:
score: 0
Accepted
time: 4ms
memory: 6092kb
Test #7:
score: 0
Accepted
time: 2ms
memory: 8036kb
Test #8:
score: 0
Accepted
time: 0ms
memory: 6280kb
Test #9:
score: 0
Accepted
time: 385ms
memory: 34584kb
Test #10:
score: 0
Accepted
time: 207ms
memory: 22284kb
Test #11:
score: 0
Accepted
time: 500ms
memory: 54320kb
Test #12:
score: 0
Accepted
time: 1ms
memory: 4112kb
Test #13:
score: 0
Accepted
time: 1ms
memory: 4052kb
Test #14:
score: 0
Accepted
time: 1ms
memory: 4192kb
Test #15:
score: 0
Accepted
time: 5ms
memory: 4372kb
Test #16:
score: 0
Accepted
time: 4ms
memory: 7968kb
Test #17:
score: 0
Accepted
time: 11ms
memory: 6812kb
Test #18:
score: 0
Accepted
time: 53ms
memory: 8120kb
Test #19:
score: 0
Accepted
time: 337ms
memory: 20472kb
Test #20:
score: 0
Accepted
time: 1926ms
memory: 51260kb
Test #21:
score: 0
Accepted
time: 56ms
memory: 27068kb
Test #22:
score: 0
Accepted
time: 55ms
memory: 27544kb
Test #23:
score: 0
Accepted
time: 71ms
memory: 30092kb
Test #24:
score: 0
Accepted
time: 40ms
memory: 18472kb
Test #25:
score: 0
Accepted
time: 40ms
memory: 21248kb
Test #26:
score: 0
Accepted
time: 74ms
memory: 29992kb
Test #27:
score: 0
Accepted
time: 1612ms
memory: 61376kb
Test #28:
score: 0
Accepted
time: 675ms
memory: 41600kb
Test #29:
score: 0
Accepted
time: 2169ms
memory: 77484kb
Test #30:
score: 0
Accepted
time: 3308ms
memory: 79176kb
Test #31:
score: 0
Accepted
time: 2981ms
memory: 78700kb
Test #32:
score: 0
Accepted
time: 3021ms
memory: 78936kb
Test #33:
score: 0
Accepted
time: 3203ms
memory: 79132kb
Test #34:
score: 0
Accepted
time: 3008ms
memory: 80116kb
Test #35:
score: 0
Accepted
time: 284ms
memory: 79380kb
Test #36:
score: 0
Accepted
time: 263ms
memory: 78848kb
Test #37:
score: 0
Accepted
time: 3540ms
memory: 80116kb
Test #38:
score: 0
Accepted
time: 859ms
memory: 103872kb
Test #39:
score: 0
Accepted
time: 310ms
memory: 94740kb
Test #40:
score: 0
Accepted
time: 814ms
memory: 76688kb
Test #41:
score: 0
Accepted
time: 303ms
memory: 81584kb
Test #42:
score: 0
Accepted
time: 293ms
memory: 82124kb
Test #43:
score: 0
Accepted
time: 329ms
memory: 82460kb
Test #44:
score: 0
Accepted
time: 370ms
memory: 89152kb
Test #45:
score: 0
Accepted
time: 353ms
memory: 86564kb
Test #46:
score: 0
Accepted
time: 1ms
memory: 5984kb
Test #47:
score: 0
Accepted
time: 200ms
memory: 53092kb
Test #48:
score: 0
Accepted
time: 1ms
memory: 3948kb
Test #49:
score: 0
Accepted
time: 2092ms
memory: 76904kb
Test #50:
score: 0
Accepted
time: 60ms
memory: 29492kb
Test #51:
score: 0
Accepted
time: 1ms
memory: 3888kb
Test #52:
score: 0
Accepted
time: 1ms
memory: 3948kb
Test #53:
score: 0
Accepted
time: 0ms
memory: 5924kb
Test #54:
score: 0
Accepted
time: 1ms
memory: 5928kb
Test #55:
score: 0
Accepted
time: 1ms
memory: 3948kb
Test #56:
score: 0
Accepted
time: 1ms
memory: 7956kb
Test #57:
score: 0
Accepted
time: 0ms
memory: 3976kb
Test #58:
score: 0
Accepted
time: 8ms
memory: 9020kb
Test #59:
score: 0
Accepted
time: 259ms
memory: 26972kb
Test #60:
score: 0
Accepted
time: 802ms
memory: 45952kb
Test #61:
score: 0
Accepted
time: 1960ms
memory: 69476kb
Test #62:
score: 0
Accepted
time: 2226ms
memory: 78076kb
Test #63:
score: 0
Accepted
time: 234ms
memory: 75368kb
Test #64:
score: 0
Accepted
time: 230ms
memory: 75404kb
Test #65:
score: 0
Accepted
time: 247ms
memory: 75604kb
Test #66:
score: 0
Accepted
time: 842ms
memory: 74676kb
Test #67:
score: 0
Accepted
time: 294ms
memory: 93628kb
Test #68:
score: 0
Accepted
time: 697ms
memory: 101864kb
Test #69:
score: 0
Accepted
time: 299ms
memory: 105284kb
Test #70:
score: 0
Accepted
time: 308ms
memory: 104352kb