Submission #595262

# Submission time Handle Problem Language Result Execution time Memory
595262 2022-07-13T13:52:27 Z SlavicG Werewolf (IOI18_werewolf) C++17
100 / 100
629 ms 89860 KB
#include "werewolf.h"
#include "bits/stdc++.h"
using namespace std;

const int N = 2e5 + 10;
int fen[N];
void modif(int u, int val) {
    for(int i = u + 1; i < N; i += i & -i) fen[i] += val;
}
int query(int l, int r) {
    int ans = 0;
    for(int i = r + 1; i > 0; i -= i & -i) ans += fen[i];
    for(int i = l; i > 0; i -= i & -i) ans -= fen[i];
    return ans;
}

struct dsu {
    vector<int> p;
    dsu(int n) : p(n) {
        iota(p.begin(), p.end(), 0);
    }
    int get(int a) {return p[a] = (a == p[a] ? a : get(p[a]));}
    void uni(int a, int b) {
        a = get(a), b = get(b);
        p[a] = b;
    }
};

void dfs(int u, vector<vector<int>>& adj, vector<int>& in, vector<int>& s, int& t) {
    in[u] = t++;
    s[u] = 1;
    for(int v: adj[u]) {
        dfs(v, adj, in, s, t);
        s[u] += s[v];
    }
}

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 Q = S.size(), A = 0, B = 0;
    vector<vector<int>> adj(n), g1(n), g2(n);
    vector<int> root1(Q), root2(Q), ss[n], ee[n], in1(n), in2(n), s1(n), s2(n);
    for(int i = 0; i < Q; ++i) ss[L[i]].push_back(i), ee[R[i]].push_back(i);
    for(int i = 0; i < (int)X.size(); ++i) adj[X[i]].push_back(Y[i]), adj[Y[i]].push_back(X[i]);
    dsu d1(n), d2(n);
    for(int i = n - 1; i >= 0; --i) {
        for(int j: adj[i]) {
            if(i < j) {
                if(d1.get(j) != i) {
                    g1[i].push_back(d1.get(j));
                    d1.uni(j, i);
                }
            }
        }
        for(int j: ss[i]) root1[j] = d1.get(S[j]);
    }
    for(int i = 0; i < n; ++i) {
        for(int j: adj[i]) {
            if(i > j) {
                if(d2.get(j) != i) {
                    g2[i].push_back(d2.get(j));
                    d2.uni(j, i);
                }
            }
        }
        for(int j: ee[i]) root2[j] = d2.get(E[j]);
    }
    dfs(0, g1, in1, s1, A);
    dfs(n - 1, g2, in2, s2, B);
    vector<int> y[n], queries[n];
    vector<int> cnt(Q, 0);

    //[in1(nodeL), in2(nodeR)], [in1(node) + s1(node) - 1, in2(node) + s2(node) - 1]
    for(int i = 0; i < Q; ++i) {
        int node = root1[i];
        queries[in1[node]].push_back(-i - 1);
        queries[in1[node] + s1[node] - 1].push_back(i);
    }
    for(int i = 0; i < n; ++i) {
        y[in1[i]].push_back(in2[i]);
    }
    for(int i = 0; i < n; ++i) {
        for(int x: queries[i]) {
            if(x >= 0) continue;
            int j = -x - 1;
            int nodeL = root1[j], nodeR = root2[j];
            cnt[j] -= query(in2[nodeR], in2[nodeR] + s2[nodeR] - 1);
        }
        for(int j: y[i]) {
            modif(j, 1);
        }
        for(int x: queries[i]) {
            if(x < 0) continue;
            int j = x;
            int nodeL = root1[j], nodeR = root2[j];
            cnt[j] += query(in2[nodeR], in2[nodeR] + s2[nodeR] - 1);
        }
    }

    for(int i = 0; i < Q; ++i) cnt[i] = cnt[i] > 0;
    return cnt;
}

Compilation message

werewolf.cpp: In function 'std::vector<int> check_validity(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
werewolf.cpp:85:17: warning: unused variable 'nodeL' [-Wunused-variable]
   85 |             int nodeL = root1[j], nodeR = root2[j];
      |                 ^~~~~
werewolf.cpp:94:17: warning: unused variable 'nodeL' [-Wunused-variable]
   94 |             int nodeL = root1[j], nodeR = root2[j];
      |                 ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 5 ms 1492 KB Output is correct
11 Correct 5 ms 1492 KB Output is correct
12 Correct 5 ms 1556 KB Output is correct
13 Correct 5 ms 1620 KB Output is correct
14 Correct 5 ms 1620 KB Output is correct
15 Correct 6 ms 1620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 571 ms 82080 KB Output is correct
2 Correct 464 ms 80164 KB Output is correct
3 Correct 454 ms 79956 KB Output is correct
4 Correct 476 ms 80276 KB Output is correct
5 Correct 447 ms 80416 KB Output is correct
6 Correct 546 ms 81472 KB Output is correct
7 Correct 476 ms 78400 KB Output is correct
8 Correct 450 ms 80176 KB Output is correct
9 Correct 394 ms 78368 KB Output is correct
10 Correct 396 ms 78188 KB Output is correct
11 Correct 419 ms 78536 KB Output is correct
12 Correct 441 ms 79124 KB Output is correct
13 Correct 448 ms 85504 KB Output is correct
14 Correct 464 ms 85408 KB Output is correct
15 Correct 477 ms 85428 KB Output is correct
16 Correct 481 ms 85476 KB Output is correct
17 Correct 440 ms 78500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 5 ms 1492 KB Output is correct
11 Correct 5 ms 1492 KB Output is correct
12 Correct 5 ms 1556 KB Output is correct
13 Correct 5 ms 1620 KB Output is correct
14 Correct 5 ms 1620 KB Output is correct
15 Correct 6 ms 1620 KB Output is correct
16 Correct 571 ms 82080 KB Output is correct
17 Correct 464 ms 80164 KB Output is correct
18 Correct 454 ms 79956 KB Output is correct
19 Correct 476 ms 80276 KB Output is correct
20 Correct 447 ms 80416 KB Output is correct
21 Correct 546 ms 81472 KB Output is correct
22 Correct 476 ms 78400 KB Output is correct
23 Correct 450 ms 80176 KB Output is correct
24 Correct 394 ms 78368 KB Output is correct
25 Correct 396 ms 78188 KB Output is correct
26 Correct 419 ms 78536 KB Output is correct
27 Correct 441 ms 79124 KB Output is correct
28 Correct 448 ms 85504 KB Output is correct
29 Correct 464 ms 85408 KB Output is correct
30 Correct 477 ms 85428 KB Output is correct
31 Correct 481 ms 85476 KB Output is correct
32 Correct 440 ms 78500 KB Output is correct
33 Correct 574 ms 81712 KB Output is correct
34 Correct 240 ms 25724 KB Output is correct
35 Correct 597 ms 82264 KB Output is correct
36 Correct 569 ms 82124 KB Output is correct
37 Correct 586 ms 81960 KB Output is correct
38 Correct 609 ms 82260 KB Output is correct
39 Correct 575 ms 89860 KB Output is correct
40 Correct 545 ms 83888 KB Output is correct
41 Correct 497 ms 80172 KB Output is correct
42 Correct 447 ms 78444 KB Output is correct
43 Correct 629 ms 84004 KB Output is correct
44 Correct 563 ms 80612 KB Output is correct
45 Correct 447 ms 86224 KB Output is correct
46 Correct 420 ms 86308 KB Output is correct
47 Correct 483 ms 85556 KB Output is correct
48 Correct 478 ms 85368 KB Output is correct
49 Correct 465 ms 85416 KB Output is correct
50 Correct 461 ms 85476 KB Output is correct
51 Correct 557 ms 80732 KB Output is correct
52 Correct 551 ms 80804 KB Output is correct