Submission #515599

# Submission time Handle Problem Language Result Execution time Memory
515599 2022-01-19T10:24:43 Z KoD Janjetina (COCI21_janjetina) C++17
110 / 110
401 ms 14888 KB
#include <bits/stdc++.h>

using std::vector;
using std::array;
using std::pair;
using std::tuple;

template <class F> struct RecLambda : private F {
    explicit RecLambda(F&& f) : F(std::forward<F>(f)) {}
    template <class... Args> decltype(auto) operator()(Args&&... args) const {
        return F::operator()(*this, std::forward<Args>(args)...);
    }
};

using ll = long long;

struct Fenwick {
    int size;
    vector<int> data;
    explicit Fenwick(const int n) : size(n), data(n + 1) {}
    void add(int i, const int x) {
        i += 1;
        while (i <= size) {
            data[i] += x;
            i += i & -i;
        }
    }
    int pref(int i) const {
        i += 1;
        int ret = 0;
        while (i > 0) {
            ret += data[i];
            i -= i & -i;
        }
        return ret;
    }
};

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int N, K;
    std::cin >> N >> K;
    vector<vector<pair<int, int>>> graph(N);    
    for (int i = 1; i < N; ++i) {
        int x, y, w;
        std::cin >> x >> y >> w;
        x -= 1, y -= 1;
        graph[x].emplace_back(y, w);
        graph[y].emplace_back(x, w);
    }
    Fenwick fen(N);
    const auto count = [&](vector<pair<int, int>>& yx) {
        std::sort(yx.begin(), yx.end());
        ll ret = 0;
        for (const auto& [y, x] : yx) {
            ret += fen.pref(std::min(N - 1, y - x - K));
            fen.add(x, 1);
        }
        for (const auto& [y, x] : yx) {
            fen.add(x, -1);
        }
        return ret;
    };
    vector<char> dead(N);
    vector<int> subtree(N);
    ll ans = 0;
    RecLambda([&](auto&& dfs, const int root) -> void {
        RecLambda([&](auto&& dfs, const int u, const int p) -> void {
            subtree[u] = 1;
            for (const auto& [v, w] : graph[u]) {
                if (!dead[v] and v != p) {
                    dfs(v, u);
                    subtree[u] += subtree[v];
                }
            }
        })(root, -1);
        const int cent = RecLambda([&](auto&& dfs, const int u, const int p) -> int {
            for (const auto& [v, w] : graph[u]) {
                if (!dead[v] and v != p and subtree[v] * 2 > subtree[root]) {
                    return dfs(v, u);
                }
            }
            return u;
        })(root, -1);
        vector<pair<int, int>> list;
        vector<pair<int, int>> all;
        const auto enumerate = RecLambda([&](auto&& dfs, const int u, const int p, const int x, const int y) -> void {
            if (y - x >= K) {
                ans += 1;
            }
            list.emplace_back(y, x);
            all.emplace_back(y, x);
            for (const auto& [v, w] : graph[u]) {
                if (!dead[v] and v != p) {
                    dfs(v, u, x + 1, std::max(y, w));
                }
            }
        });
        for (const auto& [u, w] : graph[cent]) {
            if (!dead[u]) {
                list.clear();
                enumerate(u, cent, 1, w);
                ans -= count(list);
            }
        }
        ans += count(all);
        dead[cent] = true;
        for (const auto& [u, w] : graph[cent]) {
            if (!dead[u]) {
                dfs(u);
            }
        }
    })(0);
    std::cout << ans * 2 << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 3 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 2 ms 316 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 1 ms 396 KB Output is correct
16 Correct 2 ms 320 KB Output is correct
17 Correct 2 ms 408 KB Output is correct
18 Correct 2 ms 320 KB Output is correct
19 Correct 2 ms 316 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 2 ms 320 KB Output is correct
22 Correct 2 ms 328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 23 ms 1612 KB Output is correct
6 Correct 166 ms 6664 KB Output is correct
7 Correct 336 ms 12988 KB Output is correct
8 Correct 360 ms 13052 KB Output is correct
9 Correct 357 ms 13088 KB Output is correct
10 Correct 382 ms 12940 KB Output is correct
11 Correct 306 ms 12968 KB Output is correct
12 Correct 368 ms 13004 KB Output is correct
13 Correct 315 ms 12960 KB Output is correct
14 Correct 370 ms 13008 KB Output is correct
15 Correct 372 ms 13000 KB Output is correct
16 Correct 361 ms 13080 KB Output is correct
17 Correct 368 ms 13008 KB Output is correct
18 Correct 391 ms 13048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 3 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 2 ms 316 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 1 ms 396 KB Output is correct
16 Correct 2 ms 320 KB Output is correct
17 Correct 2 ms 408 KB Output is correct
18 Correct 2 ms 320 KB Output is correct
19 Correct 2 ms 316 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 2 ms 320 KB Output is correct
22 Correct 2 ms 328 KB Output is correct
23 Correct 0 ms 204 KB Output is correct
24 Correct 0 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 23 ms 1612 KB Output is correct
28 Correct 166 ms 6664 KB Output is correct
29 Correct 336 ms 12988 KB Output is correct
30 Correct 360 ms 13052 KB Output is correct
31 Correct 357 ms 13088 KB Output is correct
32 Correct 382 ms 12940 KB Output is correct
33 Correct 306 ms 12968 KB Output is correct
34 Correct 368 ms 13004 KB Output is correct
35 Correct 315 ms 12960 KB Output is correct
36 Correct 370 ms 13008 KB Output is correct
37 Correct 372 ms 13000 KB Output is correct
38 Correct 361 ms 13080 KB Output is correct
39 Correct 368 ms 13008 KB Output is correct
40 Correct 391 ms 13048 KB Output is correct
41 Correct 0 ms 332 KB Output is correct
42 Correct 313 ms 14356 KB Output is correct
43 Correct 395 ms 14888 KB Output is correct
44 Correct 314 ms 14448 KB Output is correct
45 Correct 386 ms 14844 KB Output is correct
46 Correct 325 ms 14356 KB Output is correct
47 Correct 377 ms 14840 KB Output is correct
48 Correct 320 ms 14512 KB Output is correct
49 Correct 401 ms 14840 KB Output is correct
50 Correct 380 ms 14588 KB Output is correct
51 Correct 365 ms 14728 KB Output is correct
52 Correct 117 ms 10304 KB Output is correct
53 Correct 127 ms 10672 KB Output is correct
54 Correct 102 ms 10240 KB Output is correct
55 Correct 167 ms 10584 KB Output is correct
56 Correct 142 ms 10604 KB Output is correct
57 Correct 334 ms 11232 KB Output is correct
58 Correct 330 ms 11280 KB Output is correct
59 Correct 273 ms 11620 KB Output is correct
60 Correct 326 ms 11684 KB Output is correct
61 Correct 326 ms 11660 KB Output is correct
62 Correct 238 ms 10852 KB Output is correct
63 Correct 256 ms 11300 KB Output is correct
64 Correct 297 ms 11216 KB Output is correct
65 Correct 9 ms 888 KB Output is correct
66 Correct 0 ms 204 KB Output is correct