Submission #648217

# Submission time Handle Problem Language Result Execution time Memory
648217 2022-10-05T17:37:25 Z dxz05 Werewolf (IOI18_werewolf) C++14
100 / 100
958 ms 173748 KB
#include "werewolf.h"
#include <bits/stdc++.h>

using namespace std;

struct ReachabilityTree {
    vector<int> p, sz;
    vector<vector<int>> g;
    int id;

    vector<int> tin, tout;
    int timer;

    ReachabilityTree(int n) {
        n++;

        int m = 2 * n + 10;

        id = n + 1;
        p.assign(m, 0);
        sz.assign(m, 1);
        g.resize(m);
        iota(p.begin(), p.end(), 0);

        tin.resize(m);
        tout.resize(m);
        timer = 0;
    }

    inline int find(int x) {
        return (x == p[x] ? x : p[x] = find(p[x]));
    }

    inline bool same(int x, int y) {
        return find(x) == find(y);
    }

    inline bool unite(int x, int y) {
        if (same(x, y)) return false;
        x = find(x);
        y = find(y);
        if (sz[x] > sz[y]) swap(x, y);

        p[x] = id;
        p[y] = id;
        sz[id] += sz[x] + sz[y];
        g[id].push_back(x);
        g[id].push_back(y);

        id++;

        return true;
    }

    void dfs(int v){
        tin[v] = tout[v] = timer++;
        for (int u : g[v]){
            dfs(u);
            tout[v] = tout[u];
        }
    }

    void run_dfs(){
        for (int i = 0; i < g.size(); i++){
            if (find(i) == i) dfs(i);
        }
    }

};

struct Fenwick_Tree{
    vector<int> f;
    Fenwick_Tree(int n){
        f.resize(n + 2);
    }

    void add(int i, int val){
        i++;
        while (i < (int)f.size()){
            f[i] += val;
            i += -i & i;
        }
    }

    int get(int i){
        i++;
        int res = 0;
        while (i > 0){
            res += f[i];
            i -= -i & i;
        }
        return res;
    }

    int get(int l, int r){
        return get(r) - get(l - 1);
    }
};


vector<int> check_validity(int N, vector<int> X, vector<int> Y, vector<int> S, vector<int> E, vector<int> L, vector<int> R) {
    int M = X.size(), Q = S.size();

    vector<vector<int>> g(N);
    for (int i = 0; i < M; i++){
        g[X[i]].push_back(Y[i]);
        g[Y[i]].push_back(X[i]);
    }

    vector<vector<int>> q1(N), q2(N);
    vector<int> r1(Q), r2(Q);
    for (int i = 0; i < Q; i++){
        q1[L[i]].push_back(i);
        q2[R[i]].push_back(i);
    }

    ReachabilityTree t1(N), t2(N);
    for (int i = N - 1; i >= 0; i--){
        for (int j : g[i]){
            if (j > i) t1.unite(i, j);
        }

        for (int j : q1[i]){
            r1[j] = t1.find(S[j]);
        }
    }

    for (int i = 0; i < N; i++){
        for (int j : g[i]){
            if (j < i) t2.unite(i, j);
        }

        for (int j : q2[i]){
            r2[j] = t2.find(E[j]);
        }
    }

    t1.run_dfs();
    t2.run_dfs();

    int m = 2 * N + 10;

    vector<vector<int>> points(m);
    for (int i = 0; i < N; i++){
        int x = t1.tin[i], y = t2.tin[i];
        points[x].push_back(y);
    }

    vector<vector<pair<int, int>>> queries(m);
    for (int i = 0; i < Q; i++){
        int x1 = t1.tin[r1[i]], x2 = t1.tout[r1[i]];
        int y1 = t2.tin[r2[i]], y2 = t2.tout[r2[i]];

        if (x1 > 0) queries[x1 - 1].emplace_back(y1, y2);
        queries[x2].emplace_back(y1, y2);
    }

    Fenwick_Tree f(m);

    vector<map<pair<int, int>, int>> mp(m);
    for (int x = 0; x < m; x++){
        for (int y : points[x]) f.add(y, 1);

        for (auto now : queries[x]){
            int y1 = now.first, y2 = now.second;
            mp[x][make_pair(y1, y2)] = f.get(y1, y2);
        }
    }

    vector<int> ans(Q, 0);
    for (int i = 0; i < Q; i++){
        int x1 = t1.tin[r1[i]], x2 = t1.tout[r1[i]];
        int y1 = t2.tin[r2[i]], y2 = t2.tout[r2[i]];

        int inside = mp[x2][make_pair(y1, y2)];
        if (x1 > 0) inside -= mp[x1 - 1][make_pair(y1, y2)];

        ans[i] = (inside > 0);
    }

    return ans;
}

Compilation message

werewolf.cpp: In member function 'void ReachabilityTree::run_dfs()':
werewolf.cpp:64:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |         for (int i = 0; i < g.size(); i++){
      |                         ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 1 ms 296 KB Output is correct
7 Correct 1 ms 304 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 1 ms 296 KB Output is correct
7 Correct 1 ms 304 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 7 ms 2740 KB Output is correct
11 Correct 7 ms 2772 KB Output is correct
12 Correct 7 ms 2772 KB Output is correct
13 Correct 7 ms 2868 KB Output is correct
14 Correct 7 ms 2900 KB Output is correct
15 Correct 8 ms 2516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 777 ms 167440 KB Output is correct
2 Correct 685 ms 158596 KB Output is correct
3 Correct 700 ms 154884 KB Output is correct
4 Correct 670 ms 152888 KB Output is correct
5 Correct 696 ms 155664 KB Output is correct
6 Correct 733 ms 166620 KB Output is correct
7 Correct 588 ms 144880 KB Output is correct
8 Correct 622 ms 158524 KB Output is correct
9 Correct 612 ms 149688 KB Output is correct
10 Correct 494 ms 140296 KB Output is correct
11 Correct 570 ms 146840 KB Output is correct
12 Correct 578 ms 142180 KB Output is correct
13 Correct 739 ms 164112 KB Output is correct
14 Correct 756 ms 164052 KB Output is correct
15 Correct 855 ms 164076 KB Output is correct
16 Correct 735 ms 164200 KB Output is correct
17 Correct 607 ms 144708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 1 ms 296 KB Output is correct
7 Correct 1 ms 304 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 7 ms 2740 KB Output is correct
11 Correct 7 ms 2772 KB Output is correct
12 Correct 7 ms 2772 KB Output is correct
13 Correct 7 ms 2868 KB Output is correct
14 Correct 7 ms 2900 KB Output is correct
15 Correct 8 ms 2516 KB Output is correct
16 Correct 777 ms 167440 KB Output is correct
17 Correct 685 ms 158596 KB Output is correct
18 Correct 700 ms 154884 KB Output is correct
19 Correct 670 ms 152888 KB Output is correct
20 Correct 696 ms 155664 KB Output is correct
21 Correct 733 ms 166620 KB Output is correct
22 Correct 588 ms 144880 KB Output is correct
23 Correct 622 ms 158524 KB Output is correct
24 Correct 612 ms 149688 KB Output is correct
25 Correct 494 ms 140296 KB Output is correct
26 Correct 570 ms 146840 KB Output is correct
27 Correct 578 ms 142180 KB Output is correct
28 Correct 739 ms 164112 KB Output is correct
29 Correct 756 ms 164052 KB Output is correct
30 Correct 855 ms 164076 KB Output is correct
31 Correct 735 ms 164200 KB Output is correct
32 Correct 607 ms 144708 KB Output is correct
33 Correct 918 ms 167544 KB Output is correct
34 Correct 397 ms 50052 KB Output is correct
35 Correct 898 ms 167136 KB Output is correct
36 Correct 900 ms 167268 KB Output is correct
37 Correct 874 ms 167040 KB Output is correct
38 Correct 855 ms 167056 KB Output is correct
39 Correct 835 ms 173748 KB Output is correct
40 Correct 707 ms 152972 KB Output is correct
41 Correct 777 ms 155788 KB Output is correct
42 Correct 694 ms 141096 KB Output is correct
43 Correct 871 ms 165512 KB Output is correct
44 Correct 867 ms 163000 KB Output is correct
45 Correct 924 ms 167664 KB Output is correct
46 Correct 958 ms 166092 KB Output is correct
47 Correct 844 ms 164396 KB Output is correct
48 Correct 738 ms 164044 KB Output is correct
49 Correct 786 ms 164180 KB Output is correct
50 Correct 778 ms 164068 KB Output is correct
51 Correct 708 ms 149140 KB Output is correct
52 Correct 738 ms 149128 KB Output is correct