QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#129532#4924. 蜘蛛爬树jrjyy0 132ms37740kbC++203.5kb2023-07-22 20:24:582023-07-22 20:24:59

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:24:59]
  • 评测
  • 测评结果:0
  • 用时:132ms
  • 内存:37740kb
  • [2023-07-22 20:24:58]
  • 提交

answer

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

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

struct Segment {
    int k = 0; i64 b = inf;
    i64 operator()(int x) const { return 1ll * k * x + b; }
};
struct Node {
    Node *l = nullptr, *r = nullptr;
    Segment v;
} *null;
void modify(Node *&u, int l, int r, Segment x) {
    if (u == null) return u = new Node(*u), 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::lower_bound(adj[u].begin(), adj[u].end(), std::pair{fa[u], 0ll});
    if (it != adj[u].end() && it->first == fa[u]) 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];
void collect(int u, Node *&rt, i64 x) {
    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;

    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]) {
            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]];
            std::cerr << "QWQ\n";
        }
        if (in[u] > in[v]) std::swap(u, v);
        // qry[top[u]].push_back({in[u], in[v] + 1, i});
        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';

    return 0;
}

详细

Subtask #1:

score: 0
Memory Limit Exceeded

Test #1:

score: 0
Memory Limit Exceeded

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
Memory Limit Exceeded

Test #20:

score: 0
Memory Limit Exceeded

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
Memory Limit Exceeded

Test #28:

score: 0
Memory Limit Exceeded

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
Wrong Answer

Test #36:

score: 0
Wrong Answer
time: 132ms
memory: 37740kb

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:

2000000536044892765
1999999565026640228
1999999615215614157
2000000454251541890
2000000345406064521
1999999879005683040
1999999533760518734
2000000608283534201
2000000230459393940
1999999948654382863
1999999714737329167
1999999963887799924
2000000020677589746
2000000975315254377
2000000347213899551
...

result:

wrong answer 1st lines differ - expected: '1352416884531', found: '2000000536044892765'

Subtask #6:

score: 0
Memory Limit Exceeded

Test #43:

score: 0
Memory Limit Exceeded

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%