QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#335687#2429. Conquer The Worldcz_yxxWA 4ms8084kbC++204.5kb2024-02-23 19:42:102024-02-23 19:42:11

Judging History

你现在查看的是最新测评结果

  • [2024-02-23 19:42:11]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:8084kb
  • [2024-02-23 19:42:10]
  • 提交

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 ;
    }
    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;
// }
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 (tr[L].siz < tr[R].siz)swap(L, R);
    if (R == 0)return L;
    down(R);
    merg(L, tr[R].ls); 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];
    }
    // 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;
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: 1ms
memory: 5852kb

Test #2:

score: 0
Accepted
time: 0ms
memory: 6040kb

Test #3:

score: 0
Accepted
time: 1ms
memory: 8016kb

Test #4:

score: 0
Accepted
time: 0ms
memory: 6104kb

Test #5:

score: 0
Accepted
time: 1ms
memory: 5920kb

Test #6:

score: -100
Wrong Answer
time: 4ms
memory: 8084kb