QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#71793#2429. Conquer The WorldHe_RenAC ✓500ms77300kbC++144.8kb2023-01-12 01:15:302023-01-12 01:15:34

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-12 01:15:34]
  • 评测
  • 测评结果:AC
  • 用时:500ms
  • 内存:77300kb
  • [2023-01-12 01:15:30]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128 LL;
typedef pair<int, int> pii;
const int MAXN = 2.5e5 + 5;

template<typename T>
void write(T x) {
    if (x >= 10)
        write(x / 10);

    cout << (char)(x % 10 + '0');
}

mt19937 rnd((unsigned long long)new char ^time(0));

namespace Treap {
struct Node {
    Node *ls, *rs;
    int fix, len, sumlen;
    LL val, tag;
    Node(void) {}
    Node(int _len, LL _val, Node *emp): ls(emp), rs(emp), fix(rnd()), len(_len), sumlen(len), val(_val), tag(0) {}
} *emp, pool[MAXN * 4], *curpool = pool;
void init(void) {
    emp = curpool++;
    *emp = Node(0, 0, emp);
}
Node *new_Node(int _len, LL _val) {
    Node *u = curpool++;
    *u = Node(_len, _val, emp);
    return u;
}

void push_up(Node *u) {
    u -> sumlen = u -> ls -> sumlen + u -> rs -> sumlen + u -> len;
}
void upd(Node *u, LL k) {
    if (u != emp)
        u -> val += k, u -> tag += k;
}
void push_down(Node *u) {
    if (u -> tag) {
        upd(u -> ls, u -> tag);
        upd(u -> rs, u -> tag);
        u -> tag = 0;
    }
}

void split_len(Node *u, Node *&l, Node *&r, int k) { // len <= k
    if (u == emp) {
        l = r = emp;
        return;
    }

    push_down(u);

    if (u -> ls -> sumlen + u -> len <= k) {
        l = u;
        split_len(u -> rs, l -> rs, r, k - u -> ls -> sumlen - u -> len);
        push_up(l);
    } else {
        r = u;
        split_len(u -> ls, l, r -> ls, k);
        push_up(r);
    }
}
void split_val(Node *u, Node *&l, Node *&r, LL k) { // val <= k
    if (u == emp) {
        l = r = emp;
        return;
    }

    push_down(u);

    if (u -> val <= k) {
        l = u;
        split_val(u -> rs, l -> rs, r, k);
        push_up(l);
    } else {
        r = u;
        split_val(u -> ls, l, r -> ls, k);
        push_up(r);
    }
}
void split_lef(Node *u, Node *&l, Node *&r) {
    if (u == emp) {
        l = r = emp;
        return;
    }

    push_down(u);

    if (u -> ls == emp) {
        l = u;
        r = u -> rs;
        l -> rs = emp;
        push_up(l);
    } else {
        r = u;
        split_lef(u -> ls, l, r -> ls);
        push_up(r);
    }
}
void merge(Node *&u, Node *l, Node *r) {
    if (l == emp) {
        u = r;
        return;
    }

    if (r == emp) {
        u = l;
        return;
    }

    push_down(l);
    push_down(r);

    if (l -> fix > r -> fix) {
        u = l;
        merge(u -> rs, l -> rs, r);
    } else {
        u = r;
        merge(u -> ls, l, r -> ls);
    }

    push_up(u);
}
void join(Node *&u, Node *v) {
    if (v == emp)
        return;

    if (u == emp) {
        u = v;
        return;
    }

    if (u -> fix < v -> fix)
        swap(u, v);

    push_down(u);
    push_down(v);
    Node *l, *r;
    split_val(v, l, r, u -> val);
    join(u -> ls, l);
    join(u -> rs, r);
    push_up(u);
}
pair<Node *, Node *> cut(Node *rt, int k) {
    Node *l, *mid, *r;
    split_len(rt, l, r, k);

    if (l -> sumlen == k)
        return {l, r};

    split_lef(r, mid, r);

    int delta = k - l -> sumlen;

    merge(l, l, new_Node(delta, mid -> val));

    mid -> len -= delta;

    push_up(mid);

    merge(r, mid, r);

    return {l, r};
}
LL getsum(Node *rt) {
    if (rt == emp)
        return 0;

    push_down(rt);
    return rt -> val * rt -> len + getsum(rt -> ls) + getsum(rt -> rs);
}
} using namespace Treap;

int a[MAXN], b[MAXN];
vector<pii> g[MAXN];

int sum[MAXN];
void dfs_sum(int u, int fa) {
    sum[u] = a[u] - b[u];

    for (auto it : g[u]) {
        int v = it.first;

        if (v == fa)
            continue;

        dfs_sum(v, u);
        sum[u] += sum[v];
    }
}

int d;

Node *f[MAXN];
LL f0[MAXN];

void dfs_f(int u, int fa) {
    f[u] = new_Node(d, 0);
    f0[u] = 0;

    for (auto it : g[u]) {
        int v = it.first, w = it.second;

        if (v == fa)
            continue;

        dfs_f(v, u);

        Node *l, *r;
        tie(l, r) = cut(f[v], max(0, min(d, sum[v])));

        f0[v] += (ll)abs(sum[v]) * w;
        upd(l, -w);
        upd(r, w);

        join(f[u], l);
        join(f[u], r);

        f[u] = cut(f[u], d).first;
        f0[u] += f0[v];
    }
}

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    for (int i = 1; i < n; ++i) {
        int u, v, w;
        cin >> u >> v >> w;
        g[u].emplace_back(v, w);
        g[v].emplace_back(u, w);
    }

    for (int i = 1; i <= n; ++i)
        cin >> a[i] >> b[i];

    Treap::init();

    dfs_sum(1, 0);
    d = sum[1];
    dfs_f(1, 0);

    LL ans = f0[1] + getsum(f[1]);

    write(ans);
    return 0;
}

Details

Test #1:

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

Test #2:

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

Test #3:

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

Test #4:

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

Test #5:

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

Test #6:

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

Test #7:

score: 0
Accepted
time: 5ms
memory: 13892kb

Test #8:

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

Test #9:

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

Test #10:

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

Test #11:

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

Test #12:

score: 0
Accepted
time: 7ms
memory: 13912kb

Test #13:

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

Test #14:

score: 0
Accepted
time: 5ms
memory: 13904kb

Test #15:

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

Test #16:

score: 0
Accepted
time: 6ms
memory: 13844kb

Test #17:

score: 0
Accepted
time: 8ms
memory: 13832kb

Test #18:

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

Test #19:

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

Test #20:

score: 0
Accepted
time: 5ms
memory: 16296kb

Test #21:

score: 0
Accepted
time: 241ms
memory: 40180kb

Test #22:

score: 0
Accepted
time: 232ms
memory: 39588kb

Test #23:

score: 0
Accepted
time: 255ms
memory: 43232kb

Test #24:

score: 0
Accepted
time: 116ms
memory: 30592kb

Test #25:

score: 0
Accepted
time: 170ms
memory: 33960kb

Test #26:

score: 0
Accepted
time: 286ms
memory: 45740kb

Test #27:

score: 0
Accepted
time: 302ms
memory: 52508kb

Test #28:

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

Test #29:

score: 0
Accepted
time: 364ms
memory: 60944kb

Test #30:

score: 0
Accepted
time: 276ms
memory: 53840kb

Test #31:

score: 0
Accepted
time: 384ms
memory: 64856kb

Test #32:

score: 0
Accepted
time: 381ms
memory: 61780kb

Test #33:

score: 0
Accepted
time: 322ms
memory: 56104kb

Test #34:

score: 0
Accepted
time: 292ms
memory: 55936kb

Test #35:

score: 0
Accepted
time: 270ms
memory: 44480kb

Test #36:

score: 0
Accepted
time: 268ms
memory: 44504kb

Test #37:

score: 0
Accepted
time: 318ms
memory: 58332kb

Test #38:

score: 0
Accepted
time: 334ms
memory: 74156kb

Test #39:

score: 0
Accepted
time: 413ms
memory: 62852kb

Test #40:

score: 0
Accepted
time: 405ms
memory: 62088kb

Test #41:

score: 0
Accepted
time: 299ms
memory: 47252kb

Test #42:

score: 0
Accepted
time: 297ms
memory: 48244kb

Test #43:

score: 0
Accepted
time: 283ms
memory: 49704kb

Test #44:

score: 0
Accepted
time: 296ms
memory: 57060kb

Test #45:

score: 0
Accepted
time: 305ms
memory: 54848kb

Test #46:

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

Test #47:

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

Test #48:

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

Test #49:

score: 0
Accepted
time: 481ms
memory: 71564kb

Test #50:

score: 0
Accepted
time: 452ms
memory: 43104kb

Test #51:

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

Test #52:

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

Test #53:

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

Test #54:

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

Test #55:

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

Test #56:

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

Test #57:

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

Test #58:

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

Test #59:

score: 0
Accepted
time: 73ms
memory: 28364kb

Test #60:

score: 0
Accepted
time: 309ms
memory: 57252kb

Test #61:

score: 0
Accepted
time: 239ms
memory: 44544kb

Test #62:

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

Test #63:

score: 0
Accepted
time: 391ms
memory: 43340kb

Test #64:

score: 0
Accepted
time: 452ms
memory: 43164kb

Test #65:

score: 0
Accepted
time: 425ms
memory: 43376kb

Test #66:

score: 0
Accepted
time: 411ms
memory: 43256kb

Test #67:

score: 0
Accepted
time: 309ms
memory: 62960kb

Test #68:

score: 0
Accepted
time: 500ms
memory: 73304kb

Test #69:

score: 0
Accepted
time: 310ms
memory: 77300kb

Test #70:

score: 0
Accepted
time: 308ms
memory: 74320kb