QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#546724#2429. Conquer The Worldarnold518AC ✓409ms123620kbC++176.2kb2024-09-04 12:25:342024-09-04 12:25:35

Judging History

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

  • [2024-09-04 12:25:35]
  • 评测
  • 测评结果:AC
  • 用时:409ms
  • 内存:123620kb
  • [2024-09-04 12:25:34]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 25e4;

int N;
vector<pii> adj[MAXN+10];
int A[MAXN+10];

struct Node
{
    int sz;
    int par, lc, rc;

    ll xlen, xsum, slp;
    ll lazy;
    Node(ll x, ll s)
    {
        sz=1;
        par=lc=rc=0;
        xlen=xsum=x; slp=s;
        lazy=0;
    }
    Node()
    {
        par=lc=rc=sz=0;
        xlen=xsum=slp=0;
        lazy=0;
    }
};

vector<Node> NS;
int newNode() { NS.push_back(Node()); return NS.size()-1; }
int newNode(ll x, ll s) { NS.push_back(Node(x, s)); return NS.size()-1; }

void recalc(int node)
{
    if(node==0) return;
    int l=NS[node].lc, r=NS[node].rc;

    NS[node].sz=NS[l].sz+NS[r].sz+1;
    NS[node].xsum=NS[l].xsum+NS[r].xsum+NS[node].xlen;
}

void apply2(int node, ll upd)
{
    if(node==0) return;
    NS[node].lazy+=upd;
    NS[node].slp+=upd;
}

void prop(int node)
{
    if(node==0) return;
    if(NS[node].lazy==0) return;
    apply2(NS[node].lc, NS[node].lazy);
    apply2(NS[node].rc, NS[node].lazy);
    NS[node].lazy=0;
}

void prop_anc(int x)
{
    if(NS[x].par!=0) prop_anc(NS[x].par);
    prop(x);
}

void rotate(int x)
{
    assert(x!=0 && NS[x].par!=0);
    int p=NS[x].par, q;
    if(NS[p].lc==x)
    {
        NS[p].lc=q=NS[x].rc;
        NS[x].rc=p;
    }
    else
    {
        NS[p].rc=q=NS[x].lc;
        NS[x].lc=p;
    }
    NS[x].par=NS[p].par;
    NS[p].par=x;
    if(q) NS[q].par=p;
    if(NS[x].par!=0)
    {
        if(NS[NS[x].par].lc==p) NS[NS[x].par].lc=x;
        else if(NS[NS[x].par].rc==p) NS[NS[x].par].rc=x;
    }
    recalc(p);
    recalc(x);
}

void splay(int x)
{
    if(x==0) return;
    prop_anc(x);
    while(NS[x].par)
    {
        int p=NS[x].par, q=NS[p].par;
        if(q) rotate((NS[p].lc==x)==(NS[q].lc==p) ? p : x);
        rotate(x);    
    }
}

void insert(int node, int x)
{
    if(node==0) return;
    prop(node);
    if(NS[node].slp<=NS[x].slp)
    {
        if(NS[node].rc==0)
        {
            NS[node].rc=x;
            NS[x].par=node;
        }
        else insert(NS[node].rc, x);
    }
    else
    {
        if(NS[node].lc==0)
        {
            NS[node].lc=x;
            NS[x].par=node;
        }
        else insert(NS[node].lc, x);
    }
    recalc(node);
}

void get(int node, int &root)
{
    if(node==0) return;
    prop(node);
    get(NS[node].lc, root);
    int p=newNode(NS[node].xlen, NS[node].slp);
    insert(root, p);
    splay(p);
    root=p;
    get(NS[node].rc, root);
}

int find_kth(int node, ll k)
{
    if(node==0) return 0;
    assert(0<=k && k<=NS[node].xsum);
    prop(node);
    ll p=NS[NS[node].rc].xsum;
    if(k<p) return find_kth(NS[node].rc, k);
    if(p+NS[node].xlen<k) return find_kth(NS[node].lc, k-p-NS[node].xlen);
    return node;
}

int find_right(int node)
{
    if(node==0) return 0;
    prop(node);
    if(NS[node].rc==0) return node;
    else return find_right(NS[node].rc);
}

int find_slp(int node)
{
    if(node==0) return 0;
    prop(node);
    if(NS[node].slp>0)
    {
        int ret=find_slp(NS[node].lc);
        if(ret) return ret;
        return node;
    }
    else return find_slp(NS[node].rc);
}

int split(int &root, ll k)
{
    assert(0<=k && k<=NS[root].xsum);
    int p=find_kth(root, k);
    splay(p);
    root=p;

    ll t=NS[NS[root].rc].xsum;
    if(k==t) return NS[root].rc;
    else if(k==t+NS[root].xlen)
    {
        if(NS[root].lc==0) return root;
        int q=find_right(NS[root].lc);
        splay(q);
        root=q;
        return p;
    }
    else
    {
        int q=newNode(0, 0);
        NS[q]=NS[p];
        int l=NS[p].lc, r=NS[p].rc;
        NS[q].lc=0; NS[r].par=q;
        NS[p].rc=q; NS[q].par=p;
        NS[q].xlen=k-t;
        NS[p].xlen=t+NS[p].xlen-k;
        recalc(q); recalc(p);
        return q;
    }
}

ll f(int node)
{
    if(node==0) return 0;
    prop(node);
    return NS[node].xlen*NS[node].slp + f(NS[node].lc) + f(NS[node].rc);
}

pll P[MAXN+10];
int X[MAXN+10];

void print(int node)
{
    if(node==0) return;
    prop(node);
    print(NS[node].rc);
    printf("SLP %lld / XLEN %lld / XSUM %lld\n", NS[node].slp, NS[node].xlen, NS[node].xsum);
    print(NS[node].lc);
}

void dfs(int now, int bef, int p)
{
    X[now]=0;
    P[now]={0, 0};
    for(auto [nxt, w] : adj[now])
    {
        if(nxt==bef) continue;
        dfs(nxt, now, w);
        if(NS[X[now]].sz<NS[X[nxt]].sz)
        {
            swap(P[now], P[nxt]);
            swap(X[now], X[nxt]);
        }
        P[now].first+=P[nxt].first; P[now].second+=P[nxt].second;
        get(X[nxt], X[now]);
    }

    // printf("\n");
    // printf("AFTER MINKOWSKI %d\n", now);
    // printf("NODE %d : X %lld / Y %lld\n", now, P[now].first, P[now].second);
    // print(X[now]);
    // printf("\n");

    P[now].first+=A[now];
    P[now].second+=abs(P[now].first)*p;
    if(P[now].first<=0) apply2(X[now], -p);
    else if(P[now].first<=NS[X[now]].xsum)
    {
        int t=split(X[now], P[now].first);
        prop_anc(t);
        apply2(t, p+p);
        apply2(X[now], -p);
    }
    else
    {
        int t=newNode(P[now].first-NS[X[now]].xsum, 0);
        insert(X[now], t);
        splay(t);
        X[now]=t;
        apply2(X[now], p);
    }
    int a=find_slp(X[now]);
    splay(a);
    X[now]=a;
    NS[NS[a].lc].par=0;
    NS[a].lc=0;
    recalc(X[now]);

    // printf("NODE %d : X %lld / Y %lld\n", now, P[now].first, P[now].second);
    // print(X[now]);
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    
    cin >> N;
    for(int i=1; i<N; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
    }
    for(int i=1; i<=N; i++)
    {
        int a, b;
        cin >> a >> b;
        A[i]=a-b;
    }

    newNode(); // NIL;
    dfs(1, 1, 0);

    int t=split(X[1], min(NS[X[1]].xsum, P[1].first));

    // printf("FINAL %d : X %lld / Y %lld\n", 1, P[1].first, P[1].second);
    // print(b);
    cout << P[1].second-f(t) << "\n";
}

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 11960kb

Test #2:

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

Test #3:

score: 0
Accepted
time: 3ms
memory: 12924kb

Test #4:

score: 0
Accepted
time: 3ms
memory: 11780kb

Test #5:

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

Test #6:

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

Test #7:

score: 0
Accepted
time: 3ms
memory: 11620kb

Test #8:

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

Test #9:

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

Test #10:

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

Test #11:

score: 0
Accepted
time: 2ms
memory: 13048kb

Test #12:

score: 0
Accepted
time: 3ms
memory: 12568kb

Test #13:

score: 0
Accepted
time: 3ms
memory: 11564kb

Test #14:

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

Test #15:

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

Test #16:

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

Test #17:

score: 0
Accepted
time: 4ms
memory: 11928kb

Test #18:

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

Test #19:

score: 0
Accepted
time: 3ms
memory: 12252kb

Test #20:

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

Test #21:

score: 0
Accepted
time: 112ms
memory: 23672kb

Test #22:

score: 0
Accepted
time: 107ms
memory: 23532kb

Test #23:

score: 0
Accepted
time: 131ms
memory: 24768kb

Test #24:

score: 0
Accepted
time: 55ms
memory: 21108kb

Test #25:

score: 0
Accepted
time: 83ms
memory: 24804kb

Test #26:

score: 0
Accepted
time: 151ms
memory: 28316kb

Test #27:

score: 0
Accepted
time: 244ms
memory: 71856kb

Test #28:

score: 0
Accepted
time: 171ms
memory: 46608kb

Test #29:

score: 0
Accepted
time: 284ms
memory: 74628kb

Test #30:

score: 0
Accepted
time: 167ms
memory: 39048kb

Test #31:

score: 0
Accepted
time: 293ms
memory: 76316kb

Test #32:

score: 0
Accepted
time: 274ms
memory: 75508kb

Test #33:

score: 0
Accepted
time: 227ms
memory: 52504kb

Test #34:

score: 0
Accepted
time: 205ms
memory: 51664kb

Test #35:

score: 0
Accepted
time: 120ms
memory: 26092kb

Test #36:

score: 0
Accepted
time: 130ms
memory: 26092kb

Test #37:

score: 0
Accepted
time: 169ms
memory: 51472kb

Test #38:

score: 0
Accepted
time: 187ms
memory: 47152kb

Test #39:

score: 0
Accepted
time: 158ms
memory: 38132kb

Test #40:

score: 0
Accepted
time: 264ms
memory: 51576kb

Test #41:

score: 0
Accepted
time: 132ms
memory: 29192kb

Test #42:

score: 0
Accepted
time: 138ms
memory: 29820kb

Test #43:

score: 0
Accepted
time: 145ms
memory: 32324kb

Test #44:

score: 0
Accepted
time: 162ms
memory: 41968kb

Test #45:

score: 0
Accepted
time: 162ms
memory: 36016kb

Test #46:

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

Test #47:

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

Test #48:

score: 0
Accepted
time: 3ms
memory: 12984kb

Test #49:

score: 0
Accepted
time: 409ms
memory: 123620kb

Test #50:

score: 0
Accepted
time: 132ms
memory: 24684kb

Test #51:

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

Test #52:

score: 0
Accepted
time: 2ms
memory: 11332kb

Test #53:

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

Test #54:

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

Test #55:

score: 0
Accepted
time: 3ms
memory: 14520kb

Test #56:

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

Test #57:

score: 0
Accepted
time: 3ms
memory: 14544kb

Test #58:

score: 0
Accepted
time: 9ms
memory: 13064kb

Test #59:

score: 0
Accepted
time: 36ms
memory: 18680kb

Test #60:

score: 0
Accepted
time: 258ms
memory: 73512kb

Test #61:

score: 0
Accepted
time: 138ms
memory: 26428kb

Test #62:

score: 0
Accepted
time: 377ms
memory: 122952kb

Test #63:

score: 0
Accepted
time: 86ms
memory: 24584kb

Test #64:

score: 0
Accepted
time: 77ms
memory: 24628kb

Test #65:

score: 0
Accepted
time: 84ms
memory: 24460kb

Test #66:

score: 0
Accepted
time: 372ms
memory: 51176kb

Test #67:

score: 0
Accepted
time: 160ms
memory: 38656kb

Test #68:

score: 0
Accepted
time: 172ms
memory: 57208kb

Test #69:

score: 0
Accepted
time: 160ms
memory: 46692kb

Test #70:

score: 0
Accepted
time: 182ms
memory: 46096kb