Submission #529223

# Submission time Handle Problem Language Result Execution time Memory
529223 2022-02-22T13:30:55 Z KoD Werewolf (IOI18_werewolf) C++17
100 / 100
595 ms 70804 KB
#include "werewolf.h"

#include <bits/stdc++.h>

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

template <class F> struct RecLambda : private F {
    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)...);
    }
};

struct UnionFind {
    vector<int> par;
    vector<vector<int>> child;
    UnionFind(const int n) : par(n, -1), child(n) {}
    int find(const int u) {
        return par[u] < 0 ? u : par[u] = find(par[u]);
    }
    int merge(int x, int y) {
        x = find(x), y = find(y);
        if (x == y) return x;
        const int z = (int)par.size();
        par.push_back(-1);
        child.emplace_back();
        par[x] = par[y] = z;
        child[z].push_back(x);
        child[z].push_back(y);
        return z;
    }
    vector<pair<int, int>> order() const {
        vector<pair<int, int>> ret(par.size());
        int timer = 0;
        RecLambda([&](auto&& dfs, const int u) -> void {
            ret[u].first = timer;
            if (child[u].empty()) {
                timer += 1;
            } else {
                for (const int v : child[u]) {
                    dfs(v);
                }
            }
            ret[u].second = timer;
        })((int)par.size() - 1);
        return ret;
    }
};

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

vector<int> check_validity(int N, vector<int> X, vector<int> Y, vector<int> S, vector<int> E, vector<int> L, vector<int> R) {
    const int M = (int)X.size();
    const int Q = (int)S.size();
    vector<vector<int>> graph(N);
    for (int i = 0; i < M; ++i) {
        graph[X[i]].push_back(Y[i]);
        graph[Y[i]].push_back(X[i]);
    }
    vector<int> xl(Q), xr(Q);
    vector<int> x(N);
    {
        vector<vector<int>> list(N);
        for (int i = 0; i < Q; ++i) {
            list[L[i]].push_back(i);
        }
        UnionFind human(N);
        vector<int> memo(Q);
        for (int i = N - 1; i >= 0; --i) {
            for (const int j : graph[i]) {
                if (j >= i) {
                    human.merge(i, j);
                }
            }
            for (const int j : list[i]) {
                memo[j] = human.find(S[j]);
            }
        }
        const auto vec = human.order();
        for (int i = 0; i < N; ++i) {
            x[i] = vec[i].first;
        }
        for (int i = 0; i < Q; ++i) {
            xl[i] = vec[memo[i]].first;
            xr[i] = vec[memo[i]].second;
        }
    }
    vector<int> yl(Q), yr(Q);
    vector<int> y(N);
    {
        vector<vector<int>> list(N);
        for (int i = 0; i < Q; ++i) {
            list[R[i]].push_back(i);
        }
        UnionFind wolf(N);
        vector<int> memo(Q);
        for (int i = 0; i < N; ++i) {
            for (const int j : graph[i]) {
                if (j <= i) {
                    wolf.merge(i, j);
                }
            }
            for (const int j : list[i]) {
                memo[j] = wolf.find(E[j]);
            }
        }
        const auto vec = wolf.order();
        for (int i = 0; i < N; ++i) {
            y[i] = vec[i].first;
        }
        for (int i = 0; i < Q; ++i) {
            yl[i] = vec[memo[i]].first;
            yr[i] = vec[memo[i]].second;
        }
    }
    vector<int> add(N);
    for (int i = 0; i < N; ++i) {
        add[x[i]] = y[i];
    }
    vector<int> count(Q);
    vector<vector<int>> start(N), end(N);
    for (int i = 0; i < Q; ++i) {
        start[xl[i]].push_back(i);
        end[xr[i] - 1].push_back(i);
    }
    Fenwick fen(N);
    for (int x = 0; x < N; ++x) {
        for (const int i : start[x]) {
            count[i] -= fen.fold(yl[i], yr[i]);
        }
        fen.add(add[x]);
        for (const int i : end[x]) {
            count[i] += fen.fold(yl[i], yr[i]);
        }
    }
    for (auto& x : count) {
        x = x > 0;
    }
    return count;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 288 KB Output is correct
3 Correct 1 ms 292 KB Output is correct
4 Correct 1 ms 288 KB Output is correct
5 Correct 1 ms 292 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 288 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 288 KB Output is correct
3 Correct 1 ms 292 KB Output is correct
4 Correct 1 ms 288 KB Output is correct
5 Correct 1 ms 292 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 288 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 5 ms 1220 KB Output is correct
11 Correct 5 ms 1200 KB Output is correct
12 Correct 5 ms 1212 KB Output is correct
13 Correct 9 ms 1184 KB Output is correct
14 Correct 5 ms 1212 KB Output is correct
15 Correct 5 ms 1228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 565 ms 62636 KB Output is correct
2 Correct 426 ms 65868 KB Output is correct
3 Correct 479 ms 63776 KB Output is correct
4 Correct 486 ms 62868 KB Output is correct
5 Correct 480 ms 62908 KB Output is correct
6 Correct 595 ms 62856 KB Output is correct
7 Correct 459 ms 61896 KB Output is correct
8 Correct 415 ms 65988 KB Output is correct
9 Correct 381 ms 64568 KB Output is correct
10 Correct 388 ms 61228 KB Output is correct
11 Correct 401 ms 61212 KB Output is correct
12 Correct 475 ms 63312 KB Output is correct
13 Correct 500 ms 65908 KB Output is correct
14 Correct 461 ms 65880 KB Output is correct
15 Correct 443 ms 65896 KB Output is correct
16 Correct 457 ms 65892 KB Output is correct
17 Correct 443 ms 61856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 288 KB Output is correct
3 Correct 1 ms 292 KB Output is correct
4 Correct 1 ms 288 KB Output is correct
5 Correct 1 ms 292 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 288 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 5 ms 1220 KB Output is correct
11 Correct 5 ms 1200 KB Output is correct
12 Correct 5 ms 1212 KB Output is correct
13 Correct 9 ms 1184 KB Output is correct
14 Correct 5 ms 1212 KB Output is correct
15 Correct 5 ms 1228 KB Output is correct
16 Correct 565 ms 62636 KB Output is correct
17 Correct 426 ms 65868 KB Output is correct
18 Correct 479 ms 63776 KB Output is correct
19 Correct 486 ms 62868 KB Output is correct
20 Correct 480 ms 62908 KB Output is correct
21 Correct 595 ms 62856 KB Output is correct
22 Correct 459 ms 61896 KB Output is correct
23 Correct 415 ms 65988 KB Output is correct
24 Correct 381 ms 64568 KB Output is correct
25 Correct 388 ms 61228 KB Output is correct
26 Correct 401 ms 61212 KB Output is correct
27 Correct 475 ms 63312 KB Output is correct
28 Correct 500 ms 65908 KB Output is correct
29 Correct 461 ms 65880 KB Output is correct
30 Correct 443 ms 65896 KB Output is correct
31 Correct 457 ms 65892 KB Output is correct
32 Correct 443 ms 61856 KB Output is correct
33 Correct 545 ms 63716 KB Output is correct
34 Correct 239 ms 36564 KB Output is correct
35 Correct 550 ms 66160 KB Output is correct
36 Correct 580 ms 63236 KB Output is correct
37 Correct 519 ms 65644 KB Output is correct
38 Correct 530 ms 63824 KB Output is correct
39 Correct 496 ms 69136 KB Output is correct
40 Correct 578 ms 70804 KB Output is correct
41 Correct 454 ms 65016 KB Output is correct
42 Correct 457 ms 64076 KB Output is correct
43 Correct 568 ms 69476 KB Output is correct
44 Correct 483 ms 65552 KB Output is correct
45 Correct 418 ms 70464 KB Output is correct
46 Correct 413 ms 70156 KB Output is correct
47 Correct 428 ms 65984 KB Output is correct
48 Correct 448 ms 65872 KB Output is correct
49 Correct 457 ms 65940 KB Output is correct
50 Correct 436 ms 65824 KB Output is correct
51 Correct 531 ms 70168 KB Output is correct
52 Correct 494 ms 70236 KB Output is correct