QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#335672 | #2429. Conquer The World | cz_yxx | TL | 454ms | 8036kb | C++20 | 4.1kb | 2024-02-23 19:19:28 | 2024-02-23 19:19:29 |
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;
tr[x].slp = tr[x].lt, tr[x].sum = 1ll * tr[x].siz * tr[x].lt;
}
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 ;
}
if (x == 0){
L = 0, R = u; 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)swap(L, R);
int ll, rr; splitval(R, tr[L].slp, ll, rr);
tr[L].ls = merge(tr[L].ls, ll), tr[L].rs = merge(tr[L].rs, rr);
up(L); return L;
}
struct root{
int rt; ll rlx, rly;
}rt[N];
void mg(int x, int v){
rt[x].rt = merge(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 = merge(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];
}
// cout<<x<<" :::\n";
// cout<<rt[x].rt<<" "<< rt[x].rlx<<" "<<rt[x].rly<<endl;
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;
}
// cout<<rt[x].rt<<" "<< rt[x].rlx<<" "<<rt[x].rly<<endl;
// out(rt[x].rt); printf("\n");
}
ll ans;
void getans(int x){
if (x == 0)return ;
down(x);
ans += tr[x].slp;
getans(tr[x].ls), getans(tr[x].rs);
}
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;
// getans(ll); getans(md);
printf("%lld\n", ans);
return 0;
}
Details
Test #1:
score: 100
Accepted
time: 0ms
memory: 7960kb
Test #2:
score: 0
Accepted
time: 1ms
memory: 6012kb
Test #3:
score: 0
Accepted
time: 2ms
memory: 7924kb
Test #4:
score: 0
Accepted
time: 1ms
memory: 5852kb
Test #5:
score: 0
Accepted
time: 1ms
memory: 4032kb
Test #6:
score: 0
Accepted
time: 36ms
memory: 8032kb
Test #7:
score: 0
Accepted
time: 18ms
memory: 8036kb
Test #8:
score: 0
Accepted
time: 454ms
memory: 6708kb
Test #9:
score: -100
Time Limit Exceeded