답안 #425748

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
425748 2021-06-13T10:54:32 Z KoD 늑대인간 (IOI18_werewolf) C++17
100 / 100
926 ms 93004 KB
#include <bits/stdc++.h>
#include "werewolf.h"

template <class T> using Vec = std::vector<T>;

struct UnionFind {
    Vec<int> par;
    Vec<Vec<int>> children;
    UnionFind(const int n) : par(n), children(n) {
        std::iota(par.begin(), par.end(), 0);
    }
    int find(const int u) {
        return par[u] == u ? u : par[u] = find(par[u]);
    }
    void absorb(int u, int v) {
        u = find(u);
        v = find(v);
        if (u == v) {
            return;
        }
        par[v] = u;
        children[u].push_back(v);
    }
    std::pair<Vec<int>, Vec<std::pair<int, int>>> build(const int root) {
        const int n = (int) par.size();
        Vec<int> ord;
        ord.reserve(n);
        Vec<std::pair<int, int>> range(n);
        int timer = 0;
        auto dfs = [&](auto&& dfs, const int u) -> void {
            range[u].first = timer++;
            ord.push_back(u);
            for (const auto v : children[u]) {
                dfs(dfs, v);
            }
            range[u].second = timer;
        };
        dfs(dfs, root);
        return std::make_pair(std::move(ord), std::move(range));
    }
};

struct Fenwick {
    Vec<int> data;
    Fenwick(const int n): data(n + 1) {}
    void add(int i, const int x) {
        i += 1;
        while (i < (int) data.size()) {
            data[i] += x;
            i += i & -i;
        }
    }
    int get(int i) const {
        int x = 0;
        while (i > 0) {
            x += data[i];
            i -= i & -i;
        }
        return x;
    }
    int fold(const int l, const int r) const {
        return get(r) - get(l);
    }
};

Vec<int> check_validity(int N, Vec<int> X, Vec<int> Y, Vec<int> S, Vec<int> E, Vec<int> L, Vec<int> R) {
    const int M = (int) X.size();
    const int Q = (int) S.size();
    Vec<Vec<int>> graph(N);
    for (int i = 0; i < M; ++i) {
        graph[X[i]].push_back(Y[i]);
        graph[Y[i]].push_back(X[i]);
    }
    UnionFind asc(N), dsc(N);
    Vec<Vec<int>> rqs(N), lqs(N);
    for (int i = 0; i < Q; ++i) {
        rqs[R[i]].push_back(i);
        lqs[L[i]].push_back(i);
    }
    Vec<int> rvert(Q), lvert(Q);
    for (int u = 0; u < N; ++u) {
        for (const auto v : graph[u]) {
            if (v < u) {
                asc.absorb(u, v);
            }
        }
        for (const auto q : rqs[u]) {
            rvert[q] = asc.find(E[q]);
        }
    }
    for (int u = N - 1; u >= 0; --u) {
        for (const auto v : graph[u]) {
            if (u < v) {
                dsc.absorb(u, v);
            }
        }
        for (const auto q : lqs[u]) {
            lvert[q] = dsc.find(S[q]);
        }
    }
    const auto [rord, rrange] = asc.build(N - 1);
    const auto [lord, lrange] = dsc.build(0);
    Vec<int> linv(N);
    for (int i = 0; i < N; ++i) {
        linv[lord[i]] = i;
    }
    Vec<Vec<int>> sub(N), add(N);
    for (int i = 0; i < Q; ++i) {
        const auto [l, r] = rrange[rvert[i]];
        sub[l].push_back(i);
        add[r - 1].push_back(i);
    }
    Fenwick fen(N);
    Vec<int> ret(Q);
    for (int i = 0; i < N; ++i) {
        for (const auto q : sub[i]) {
            const auto [u, d] = lrange[lvert[q]];
            ret[q] -= fen.fold(u, d);
        }
        fen.add(linv[rord[i]], 1);
        for (const auto q : add[i]) {
            const auto [u, d] = lrange[lvert[q]];
            ret[q] += fen.fold(u, d);
        }
    }
    for (auto& x : ret) {
        if (x > 0) {
            x = 1;
        }
    }
    return ret;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 268 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 288 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 268 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 288 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 6 ms 1584 KB Output is correct
11 Correct 6 ms 1484 KB Output is correct
12 Correct 6 ms 1484 KB Output is correct
13 Correct 8 ms 1612 KB Output is correct
14 Correct 6 ms 1612 KB Output is correct
15 Correct 7 ms 1668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 926 ms 78572 KB Output is correct
2 Correct 763 ms 87536 KB Output is correct
3 Correct 704 ms 85444 KB Output is correct
4 Correct 785 ms 84844 KB Output is correct
5 Correct 804 ms 85024 KB Output is correct
6 Correct 708 ms 86212 KB Output is correct
7 Correct 594 ms 82980 KB Output is correct
8 Correct 665 ms 87492 KB Output is correct
9 Correct 605 ms 84664 KB Output is correct
10 Correct 553 ms 82888 KB Output is correct
11 Correct 610 ms 83456 KB Output is correct
12 Correct 652 ms 84156 KB Output is correct
13 Correct 832 ms 92724 KB Output is correct
14 Correct 729 ms 93004 KB Output is correct
15 Correct 671 ms 92692 KB Output is correct
16 Correct 644 ms 92640 KB Output is correct
17 Correct 558 ms 82992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 268 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 288 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 6 ms 1584 KB Output is correct
11 Correct 6 ms 1484 KB Output is correct
12 Correct 6 ms 1484 KB Output is correct
13 Correct 8 ms 1612 KB Output is correct
14 Correct 6 ms 1612 KB Output is correct
15 Correct 7 ms 1668 KB Output is correct
16 Correct 926 ms 78572 KB Output is correct
17 Correct 763 ms 87536 KB Output is correct
18 Correct 704 ms 85444 KB Output is correct
19 Correct 785 ms 84844 KB Output is correct
20 Correct 804 ms 85024 KB Output is correct
21 Correct 708 ms 86212 KB Output is correct
22 Correct 594 ms 82980 KB Output is correct
23 Correct 665 ms 87492 KB Output is correct
24 Correct 605 ms 84664 KB Output is correct
25 Correct 553 ms 82888 KB Output is correct
26 Correct 610 ms 83456 KB Output is correct
27 Correct 652 ms 84156 KB Output is correct
28 Correct 832 ms 92724 KB Output is correct
29 Correct 729 ms 93004 KB Output is correct
30 Correct 671 ms 92692 KB Output is correct
31 Correct 644 ms 92640 KB Output is correct
32 Correct 558 ms 82992 KB Output is correct
33 Correct 888 ms 87208 KB Output is correct
34 Correct 321 ms 37136 KB Output is correct
35 Correct 775 ms 89200 KB Output is correct
36 Correct 745 ms 87392 KB Output is correct
37 Correct 862 ms 88620 KB Output is correct
38 Correct 824 ms 87872 KB Output is correct
39 Correct 758 ms 92908 KB Output is correct
40 Correct 739 ms 92780 KB Output is correct
41 Correct 721 ms 85860 KB Output is correct
42 Correct 622 ms 83596 KB Output is correct
43 Correct 848 ms 92612 KB Output is correct
44 Correct 745 ms 86624 KB Output is correct
45 Correct 722 ms 92956 KB Output is correct
46 Correct 675 ms 92816 KB Output is correct
47 Correct 712 ms 92972 KB Output is correct
48 Correct 754 ms 92664 KB Output is correct
49 Correct 757 ms 92764 KB Output is correct
50 Correct 694 ms 92644 KB Output is correct
51 Correct 695 ms 89792 KB Output is correct
52 Correct 720 ms 89868 KB Output is correct