QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#339027#8235. Top ClusterrgnerdplayerWA 575ms134624kbC++203.2kb2024-02-26 16:51:492024-02-26 16:51:51

Judging History

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

  • [2024-02-26 16:51:51]
  • 评测
  • 测评结果:WA
  • 用时:575ms
  • 内存:134624kb
  • [2024-02-26 16:51:49]
  • 提交

answer

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

using i64 = long long;

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

    auto solve = [&]() {
        int n, q;
        cin >> n >> q;

        vector<int> w(n);
        for (int i = 0; i < n; i++) {
            cin >> w[i];
        }

        vector<int> o(n);
        iota(o.begin(), o.end(), 0);
        sort(o.begin(), o.end(), [&](int i, int j) { return w[i] < w[j]; });

        int sz = 0;
        while (sz < n && w[o[sz]] == sz) {
            sz++;
        }
        o.resize(sz);

        vector<vector<pair<int, int>>> adj(n);
        for (int i = 0; i < n - 1; i++) {
            int u, v, w;
            cin >> u >> v >> w;
            u--, v--;
            adj[u].emplace_back(v, w);
            adj[v].emplace_back(u, w);
        }

        vector<i64> pref(n);
        vector<int> dep(n), euler(2 * n - 1), pos(n);
        int cur = 0;

        auto dfs = [&](auto dfs, int u, int p) -> void {
            pos[u] = cur;
            euler[cur++] = u;
            for (auto [v, w] : adj[u]) {
                if (v == p) {
                    continue;
                }
                pref[v] = pref[u] + w;
                dep[v] = dep[u] + 1;
                dfs(dfs, v, u);
                euler[cur++] = u;
            }
        };
        dfs(dfs, 0, -1);

        int lg = __lg(2 * n - 1) + 1;
        vector st(lg + 1, vector<int>(2 * n - 1));
        st[0] = euler;

        for (int i = 1; i <= lg; i++) {
            for (int j = 0; j + (1 << i) <= 2 * n - 1; j++) {
                int u = st[i - 1][j], v = st[i - 1][j + (1 << i - 1)];
                st[i][j] = dep[u] < dep[v] ? u : v;
            }
        }

        auto lca = [&](int u, int v) {
            if (u == v) {
                return u;
            }
            tie(u, v) = minmax(pos[u], pos[v]);
            int k = __lg(v - u + 1);
            return st[k][u];
        };

        auto dist = [&](int u, int v) {
            int l = lca(u, v);
            return pref[u] + pref[v] - 2 * pref[l];
        };

        vector<pair<int, int>> diameter(sz);
        diameter[0] = {o[0], o[0]};

        for (int i = 1; i < sz; i++) {
            auto [u, v] = diameter[i - 1];
            i64 d = dist(u, v);
            i64 du = dist(o[i], u);
            i64 dv = dist(o[i], v);

            i64 mx = max({d, du, dv});

            if (mx == d) {
                diameter[i] = diameter[i - 1];
            } else if (mx == du) {
                diameter[i] = {o[i], u};
            } else if (mx == dv) {
                diameter[i] = {o[i], v};
            }
        }

        while (q--) {
            int x;
            i64 k;
            cin >> x >> k;
            x--;

            int ans = *ranges::partition_point(ranges::iota_view(0, sz + 1), [&](int mid) {
                if (mid == sz) {
                    return false;
                }
                auto [u, v] = diameter[mid];
                return max(dist(u, x), dist(v, x)) <= k;
            });

            cout << ans << '\n';
        }
    };
    
    solve();
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3544kb

input:

5 4
3 9 0 1 2
1 2 10
3 1 4
3 4 3
3 5 2
3 0
1 0
4 6
4 7

output:

1
0
3
4

result:

ok 4 number(s): "1 0 3 4"

Test #2:

score: -100
Wrong Answer
time: 575ms
memory: 134624kb

input:

500000 500000
350828 420188 171646 209344 4 999941289 289054 79183 999948352 427544 160827 138994 192204 108365 99596 999987124 292578 2949 384841 269390 999920664 315611 163146 51795 265839 34188 999939494 145387 366234 86466 220368 357231 347706 332064 279036 173185 5901 217061 112848 37915 377359...

output:

0
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
2499...

result:

wrong answer 36936th numbers differ - expected: '14', found: '17'