QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#129573#4924. 蜘蛛爬树jrjyy0 0ms0kbC++203.7kb2023-07-22 20:51:092023-07-22 20:51:11

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-22 20:51:11]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2023-07-22 20:51:09]
  • 提交

answer

/* cth2021_12c_climb.cpp */
#include <bits/stdc++.h>

using i64 = long long;
constexpr int N = 2e5 + 5, Q = 2e5 + 5, S = 2e7;
constexpr i64 inf = 2e18;

struct Segment {
    int k; i64 b;
    i64 operator()(int x) const { return 1ll * k * x + b; }
};
struct Node {
    Node *l, *r;
    Segment v;
} *null;
Node *newNode() {
    static Node t[S], *p = t;
    return *p = *null, p++;
}
void modify(Node *&u, int l, int r, Segment x) {
    if (u == null) return u = newNode(), u->v = x, void();
    int m = (l + r) / 2;
    if (x(m) < u->v(m)) std::swap(x, u->v);
    if (r - l == 1) return;
    if (x(l) < u->v(l)) modify(u->l, l, m, x);
    else if (x(r - 1) < u->v(r - 1)) modify(u->r, m, r, x);
}
i64 query(Node *u, int l, int r, int p) {
    if (u == null) return inf;
    int m = (l + r) / 2; i64 res = u->v(p);
    if (p < m) res = std::min(res, query(u->l, l, m, p));
    else res = std::min(res, query(u->r, m, r, p));
    return res;
}

int n, m, q, a[N]; i64 ans[Q];
std::vector<std::pair<int, i64>> adj[N];

int fa[N], sz[N], in[N], top[N], cur; i64 dep[N];
void dfs1(int u) {
    auto it = std::find_if(adj[u].begin(), adj[u].end(), [&](auto x) { return x.first == fa[u]; });
    if (it != adj[u].end()) adj[u].erase(it);
    sz[u] = 1;
    for (auto &x : adj[u]) {
        auto [v, w] = x; fa[v] = u, dep[v] = dep[u] + w, dfs1(v);
        if (sz[v] > sz[adj[u].front().first]) std::swap(adj[u].front(), x);
    }
}
void dfs2(int u) {
    in[u] = cur++;
    for (auto [v, w] : adj[u]) top[v] = v == adj[u].front().first ? top[u] : v, dfs2(v);
}
int lca(int u, int v) {
    while (top[u] != top[v]) {
        if (in[u] < in[v]) v = fa[top[v]];
        else u = fa[top[u]];
    }
    return in[u] < in[v] ? u : v;
}

std::vector<std::array<int, 3>> qry[N]; int qk[Q]; i64 qd[Q];
int cnt;
void collect(int u, Node *&rt, i64 x) {
    ++cnt; assert(cnt <= 5000000);
    modify(rt, 0, m, Segment{a[u], 2 * dep[u] + x});
    for (auto [v, w] : adj[u]) collect(v, rt, x);
}
Node *rt[N];
void dfs3(int u) {
    rt[u] = u == top[u] ? null : rt[fa[u]];
    modify(rt[u], 0, m, Segment{a[u], 0});
    for (auto [v, w] : adj[u]) {
        if (v == adj[u].front().first) {
            for (auto [x, y] : adj[u]) if (x != v) collect(x, rt[u], -2 * dep[u]);
            for (auto [_l, _r, id] : qry[u]) {
                ans[id] = std::min(ans[id], query(rt[u], 0, m, qk[id]));
            }
        }
        dfs3(v);
    }
}

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    null = new Node{}, null->l = null->r = null, null->v = Segment{0, inf};

    std::cin >> n >> m >> q;
    for (int i = 0; i < n; ++i) std::cin >> a[i];
    for (int i = 0; i < n - 1; ++i) {
        int u, v; i64 w; std::cin >> u >> v >> w, --u, --v;
        adj[u].push_back({v, w}), adj[v].push_back({u, w});
    }
    
    fa[0] = -1, dfs1(0), dfs2(0);

    for (int i = 0; i < q; ++i) {
        i64 x, y; std::cin >> x >> y, --x, --y;
        int u = x % n, v = y % n; qk[i] = std::abs(x / n - y / n);
        // qd[i] = dep[u] + dep[v] - 2 * dep[lca(u, v)];
        while (top[u] != top[v]) {
            assert(u == 0);
            if (in[u] > in[v]) std::swap(u, v);
            // qry[top[v]].push_back({top[v], in[v] + 1, i}), v = fa[top[v]];
            qry[v].push_back({top[v], in[v] + 1, i}), v = fa[top[v]];
        }
        if (in[u] > in[v]) std::swap(u, v);
        // qry[top[u]].push_back({in[u], in[v] + 1, i});
        assert(u == 0);
        qry[v].push_back({in[u], in[v] + 1, i});
    }
    std::fill(ans, ans + q, inf);

    dfs3(0);

    for (int i = 0; i < q; ++i) std::cout << ans[i] + qd[i] << '\n';

    std::cerr << cnt << '\n';

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Dangerous Syscalls

Test #1:

score: 0
Dangerous Syscalls

input:

97 99 1000
763118300 558295517 80676328 362318619 473105413 468927175 311496507 936935430 176032966 304576040 308583326 681580095 549392233 518152994 829474320 751189715 542810029 587869244 878512027 530678371 832766129 535259635 799122942 596982955 884696876 605325753 495661541 506105495 561218313 ...

output:


result:


Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Dangerous Syscalls

Test #20:

score: 0
Dangerous Syscalls

input:

200000 20 200000
679416469 548913625 468159997 137709609 883140368 682558021 473174374 400192350 124143873 825920417 372498686 851213321 822264481 78195915 5427143 453304163 233551905 810910186 810046144 52603791 282167184 385032797 81387991 747194790 917579656 585184539 12659388 249218417 158295502...

output:


result:


Subtask #4:

score: 0
Dangerous Syscalls

Test #28:

score: 0
Dangerous Syscalls

input:

200000 1000000000 200000
28270302 472359923 262785485 923929785 393684160 761485431 72038469 116384740 426631758 437934930 610834083 455314140 734276543 903544756 220163018 756113376 732404264 947339315 109784905 625451008 794076307 818852312 758007217 124450858 674924509 311761991 507260538 7032362...

output:


result:


Subtask #5:

score: 0
Dangerous Syscalls

Test #36:

score: 0
Dangerous Syscalls

input:

199918 1000000000 199903
1496 2382 3896 3664 1177 1627 2821 4200 3074 3783 2069 4403 629 2610 4991 4074 3033 2798 4333 3501 3667 3064 663 2821 2818 458 2950 4020 2665 3578 63 4855 4941 3492 2423 4510 1489 1018 4829 1912 3133 3174 309 287 2909 4102 4296 4526 3170 3683 4960 4863 4738 2927 2405 3600 44...

output:


result:


Subtask #6:

score: 0
Dangerous Syscalls

Test #43:

score: 0
Dangerous Syscalls

input:

200000 1000000000 200000
81882094 47220813 43282454 17633207 52769165 4830673 31396360 64793163 9174729 36727563 71268262 24662923 40146030 1430053 62926106 30042905 1330107 81817720 98841078 87766129 51155045 23216268 79896310 66625868 87854925 42976560 86542933 28336449 34932261 19698851 584453 90...

output:


result:


Subtask #7:

score: 0
Skipped

Dependency #1:

0%

Subtask #8:

score: 0
Skipped

Dependency #1:

0%