답안 #511200

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
511200 2022-01-15T11:59:53 Z Victor Janjetina (COCI21_janjetina) C++17
110 / 110
941 ms 26856 KB
// #pragma GCC target ("avx,avx2,fma")
// #pragma GCC optimize ("Ofast,inline") // O1 - O2 - O3 - Os - Ofast
// #pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define trav(a, x) for (auto &a : x)

#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define pb push_back
#define debug(x) cout << #x << " = " << x << endl

#define umap unordered_map
#define uset unordered_set

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;

const int INF = 1'000'000'007;

struct Node {
    Node *l, *r;
    int lo, hi, val, mset = INF, madd = 0;
    Node(int L, int R) : lo(L), hi(R) {
        val = 0;
        if (hi - lo != 1) {
            int mid = (lo + hi) / 2;
            l = new Node(lo, mid);
            r = new Node(mid, hi);
        }
    }

    int query(int L, int R) {
        if (hi <= L || R <= lo) return 0;
        if (L <= lo && hi <= R) return val;
        push();
        return l->query(L, R) + r->query(L, R);
    }

    void add(int L, int R, int x) {
        if (hi <= L || R <= lo) return;
        if (L <= lo && hi <= R) {
            if (mset != INF)
                mset += x;
            else
                madd += x;

            val += x * (hi - lo);
            return;
        }
        push();
        l->add(L, R, x);
        r->add(L, R, x);
        val = l->val + r->val;
    }

    void set(int L, int R, int x) {
        if (hi <= L || R <= lo) return;
        if (L <= lo && hi <= R) {
            mset = x;
            val = x * (hi - lo);
            madd = 0;
            return;
        }
        push();
        l->set(L, R, x);
        r->set(L, R, x);
        val = l->val + r->val;
    }

    void push() {
        if (mset != INF) {
            l->set(lo, hi, mset);
            r->set(lo, hi, mset);
            mset = INF;

        } else if (madd) {
            l->add(lo, hi, madd);
            r->add(lo, hi, madd);
            madd = 0;
        }
    }
};

bitset<100005> removed;
Node *segtree;
int n, k, subsize[100005], centroid, cnt = 0;
vii graph[100005];

void dfs(int u, int p) {
    subsize[u] = 1;
    int mx = 0;
    trav(edge, graph[u]) {
        int v, w;
        tie(v, w) = edge;
        if (v != p && !removed[v]) {
            dfs(v, u);
            subsize[u] += subsize[v];
            mx = max(mx, subsize[v]);
        }
    }

    if (subsize[u] >= (cnt + 1) / 2 && mx <= cnt / 2) centroid = u;
}

ll pairs(vii &paths) {
    sort(all(paths));
    segtree->set(0, n, 0);

    ll ans = 0;
    trav(path, paths) {
        int mx, dist;
        tie(mx, dist) = path;
        // cout << "mx = " << mx << " dist = " << dist << " pairs = " << segtree->query(0, mx - dist - k) << endl << endl;
        ans += segtree->query(0, mx - dist - k);
        segtree->add(dist, dist + 1, 1);
    }

    return ans;
}

ll ans = 0;
vii paths, subpaths;
void dfs2(int u, int p, int mx_road, int dist) {
    paths.emplace_back(mx_road, dist);
    if (u != centroid) subpaths.emplace_back(mx_road, dist);

    trav(edge, graph[u]) {
        int v, w;
        tie(v, w) = edge;
        if (v != p && !removed[v]) {
            dfs2(v, u, max(mx_road, w), dist + 1);
            if (u == centroid) {
                ans -= pairs(subpaths);
                subpaths.clear();
            }
        }
    }
}

void predfs(int u,int p) {
    ++cnt;
    trav(edge,graph[u]){
        int v=edge.first;
        if(v!=p&&!removed[v])predfs(v,u);
    }
}

void centroid_decomp(int u) {
    cnt=0;
    predfs(u,u);
    dfs(u, u);
    if (subsize[u] == 1) return;
    assert(!removed[centroid]);

    dfs2(centroid, centroid, 0, 0);
    ans += pairs(paths);
    paths.clear();

    removed[centroid] = 1;
    trav(edge, graph[centroid]) {
        int v, w;
        tie(v, w) = edge;
        if (!removed[v]) centroid_decomp(v);
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);

    cin >> n >> k;
    segtree = new Node(0, n);
    --k;
    rep(i, 1, n) {
        int u, v, w;
        cin >> u >> v >> w;
        graph[--u].emplace_back(--v, w);
        graph[v].emplace_back(u, w);
    }

    centroid_decomp(0);
    cout << ans * 2 << endl;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 5 ms 2892 KB Output is correct
5 Correct 3 ms 2892 KB Output is correct
6 Correct 4 ms 2892 KB Output is correct
7 Correct 3 ms 2892 KB Output is correct
8 Correct 5 ms 2892 KB Output is correct
9 Correct 2 ms 2764 KB Output is correct
10 Correct 3 ms 2832 KB Output is correct
11 Correct 3 ms 2764 KB Output is correct
12 Correct 3 ms 2764 KB Output is correct
13 Correct 4 ms 2764 KB Output is correct
14 Correct 4 ms 2800 KB Output is correct
15 Correct 3 ms 2828 KB Output is correct
16 Correct 3 ms 2764 KB Output is correct
17 Correct 3 ms 2764 KB Output is correct
18 Correct 4 ms 2764 KB Output is correct
19 Correct 4 ms 2764 KB Output is correct
20 Correct 3 ms 2764 KB Output is correct
21 Correct 4 ms 2836 KB Output is correct
22 Correct 4 ms 2764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 2 ms 2676 KB Output is correct
4 Correct 5 ms 2892 KB Output is correct
5 Correct 38 ms 4936 KB Output is correct
6 Correct 439 ms 14740 KB Output is correct
7 Correct 523 ms 26424 KB Output is correct
8 Correct 861 ms 26808 KB Output is correct
9 Correct 580 ms 26404 KB Output is correct
10 Correct 880 ms 26808 KB Output is correct
11 Correct 517 ms 26348 KB Output is correct
12 Correct 878 ms 26836 KB Output is correct
13 Correct 523 ms 26424 KB Output is correct
14 Correct 865 ms 26780 KB Output is correct
15 Correct 849 ms 26508 KB Output is correct
16 Correct 921 ms 26652 KB Output is correct
17 Correct 905 ms 26684 KB Output is correct
18 Correct 940 ms 26652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 5 ms 2892 KB Output is correct
5 Correct 3 ms 2892 KB Output is correct
6 Correct 4 ms 2892 KB Output is correct
7 Correct 3 ms 2892 KB Output is correct
8 Correct 5 ms 2892 KB Output is correct
9 Correct 2 ms 2764 KB Output is correct
10 Correct 3 ms 2832 KB Output is correct
11 Correct 3 ms 2764 KB Output is correct
12 Correct 3 ms 2764 KB Output is correct
13 Correct 4 ms 2764 KB Output is correct
14 Correct 4 ms 2800 KB Output is correct
15 Correct 3 ms 2828 KB Output is correct
16 Correct 3 ms 2764 KB Output is correct
17 Correct 3 ms 2764 KB Output is correct
18 Correct 4 ms 2764 KB Output is correct
19 Correct 4 ms 2764 KB Output is correct
20 Correct 3 ms 2764 KB Output is correct
21 Correct 4 ms 2836 KB Output is correct
22 Correct 4 ms 2764 KB Output is correct
23 Correct 1 ms 2636 KB Output is correct
24 Correct 1 ms 2636 KB Output is correct
25 Correct 2 ms 2676 KB Output is correct
26 Correct 5 ms 2892 KB Output is correct
27 Correct 38 ms 4936 KB Output is correct
28 Correct 439 ms 14740 KB Output is correct
29 Correct 523 ms 26424 KB Output is correct
30 Correct 861 ms 26808 KB Output is correct
31 Correct 580 ms 26404 KB Output is correct
32 Correct 880 ms 26808 KB Output is correct
33 Correct 517 ms 26348 KB Output is correct
34 Correct 878 ms 26836 KB Output is correct
35 Correct 523 ms 26424 KB Output is correct
36 Correct 865 ms 26780 KB Output is correct
37 Correct 849 ms 26508 KB Output is correct
38 Correct 921 ms 26652 KB Output is correct
39 Correct 905 ms 26684 KB Output is correct
40 Correct 940 ms 26652 KB Output is correct
41 Correct 2 ms 2652 KB Output is correct
42 Correct 503 ms 26332 KB Output is correct
43 Correct 941 ms 26856 KB Output is correct
44 Correct 534 ms 26404 KB Output is correct
45 Correct 873 ms 26692 KB Output is correct
46 Correct 507 ms 26356 KB Output is correct
47 Correct 866 ms 26764 KB Output is correct
48 Correct 575 ms 26484 KB Output is correct
49 Correct 895 ms 26680 KB Output is correct
50 Correct 871 ms 26552 KB Output is correct
51 Correct 928 ms 26748 KB Output is correct
52 Correct 176 ms 19180 KB Output is correct
53 Correct 266 ms 19584 KB Output is correct
54 Correct 169 ms 19264 KB Output is correct
55 Correct 372 ms 19488 KB Output is correct
56 Correct 370 ms 19584 KB Output is correct
57 Correct 407 ms 19296 KB Output is correct
58 Correct 622 ms 19260 KB Output is correct
59 Correct 580 ms 19556 KB Output is correct
60 Correct 733 ms 19384 KB Output is correct
61 Correct 658 ms 19512 KB Output is correct
62 Correct 336 ms 19028 KB Output is correct
63 Correct 506 ms 19420 KB Output is correct
64 Correct 602 ms 19332 KB Output is correct
65 Correct 12 ms 3444 KB Output is correct
66 Correct 1 ms 2636 KB Output is correct