Submission #116232

# Submission time Handle Problem Language Result Execution time Memory
116232 2019-06-11T11:35:19 Z zubec Werewolf (IOI18_werewolf) C++14
100 / 100
1236 ms 104564 KB
#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;

const int N = 200100;

vector <vector <int> > g, g1, g2;

int dsu[2][N], fenw[N], n;

int tin[2][N], tout[2][N], tiktak, up[2][20][N], pos[N];

int dsu_get(int pr, int v){
    if (v == dsu[pr][v])
        return v;
    return dsu[pr][v] = dsu_get(pr, dsu[pr][v]);
}

void upd(int v){
    for (int i = v; i <= n; i += (i & (-i))){
        ++fenw[i];
    }
}

int get(int v){
    int ans = 0;
    for (int i = v; i >= 1; i -= (i & (-i))){
        ans += fenw[i];
    }
    return ans;
}

void dfs(vector <vector <int> > &g, int cur, int v, int p){
    tin[cur][v] = ++tiktak;
    up[cur][0][v] = p;
    for (int i = 1; i < 20; i++){
        up[cur][i][v] = up[cur][i-1][up[cur][i-1][v]];
    }
    for (int i = 0; i < g[v].size(); i++){
        int to = g[v][i];
        dfs(g, cur, to, v);
    }
    tout[cur][v] = tiktak;
}

std::vector<int> check_validity(int N, std::vector<int> X, std::vector<int> Y,
                                std::vector<int> S, std::vector<int> E,
                                std::vector<int> L, std::vector<int> R) {
    n = N;
    g.resize(n);
    g1.resize(n);
    g2.resize(n);
    for (int i = 0; i <= n; i++){
        dsu[0][i] = dsu[1][i] = i;
        fenw[i] = 0;
    }
    int Q = S.size();
    vector<int> A(Q);

    for (int i = 0; i < X.size(); i++){
        g[X[i]].push_back(Y[i]);
        g[Y[i]].push_back(X[i]);
    }

    for (int i = n-1; i >= 0; i--){
        for (int j = 0; j < g[i].size(); j++){
            int to = g[i][j];
            if (to < i)
                continue;
            int a = dsu_get(0, i);
            int b = dsu_get(0, to);
            if (a == b)
                continue;
            dsu[0][b] = a;
            g1[a].push_back(b);
        }
    }
    for (int i = 0; i < n; i++){
        for (int j = 0; j < g[i].size(); j++){
            int to = g[i][j];
            if (i < to)
                continue;
            int a = dsu_get(1, i);
            int b = dsu_get(1, to);
            if (a == b)
                continue;
            dsu[1][b] = a;
            g2[a].push_back(b);
        }
    }
    tiktak = 0;
    dfs(g1, 0, dsu_get(0, 0), dsu_get(0, 0));
    tiktak = 0;
    dfs(g2, 1, dsu_get(1, 0), dsu_get(1, 0));

    vector <pair<int, pair<int, int> > > zapr;

    for (int i = 0; i < Q; i++){
        int s = S[i], e = E[i], l = L[i], r = R[i];
        for (int i = 19; i >= 0; i--){
            if (up[0][i][s] >= l)
                s = up[0][i][s];
            if (up[1][i][e] <= r)
                e = up[1][i][e];
        }
        zapr.push_back({tin[0][s]-1, {-(i+1), e} });
        zapr.push_back({tout[0][s], {(i+1), e}});
    }
    for (int i = 0; i < n; i++){
        pos[tin[0][i]-1] = tin[1][i];
    }
    sort(zapr.begin(), zapr.end());
    int ptr = 0;
    for (int i = 0; i < zapr.size(); i++){
        int tim = zapr[i].first, id = zapr[i].second.first, v = zapr[i].second.second;
        while(ptr < tim){
            upd(pos[ptr]);
            ++ptr;
        }
        if (id < 0)
            A[abs(id)-1] -= (get(tout[1][v])-get(tin[1][v]-1)); else
            A[abs(id)-1] += (get(tout[1][v])-get(tin[1][v]-1));
    }
    for (int i = 0; i < Q; i++){
        A[i] = (A[i] > 0);
    }


    return A;
}

Compilation message

werewolf.cpp: In function 'void dfs(std::vector<std::vector<int> >&, int, int, int)':
werewolf.cpp:39:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[v].size(); i++){
                     ~~^~~~~~~~~~~~~
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:60:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < X.size(); i++){
                     ~~^~~~~~~~~~
werewolf.cpp:66:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 0; j < g[i].size(); j++){
                         ~~^~~~~~~~~~~~~
werewolf.cpp:79:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 0; j < g[i].size(); j++){
                         ~~^~~~~~~~~~~~~
werewolf.cpp:114:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < zapr.size(); i++){
                     ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 640 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 2 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 640 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 2 ms 640 KB Output is correct
10 Correct 8 ms 2048 KB Output is correct
11 Correct 8 ms 2048 KB Output is correct
12 Correct 8 ms 1920 KB Output is correct
13 Correct 9 ms 2264 KB Output is correct
14 Correct 8 ms 2176 KB Output is correct
15 Correct 9 ms 2148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1192 ms 82484 KB Output is correct
2 Correct 980 ms 88132 KB Output is correct
3 Correct 944 ms 84508 KB Output is correct
4 Correct 992 ms 83020 KB Output is correct
5 Correct 1022 ms 82592 KB Output is correct
6 Correct 1062 ms 82532 KB Output is correct
7 Correct 1126 ms 82408 KB Output is correct
8 Correct 741 ms 88288 KB Output is correct
9 Correct 561 ms 84364 KB Output is correct
10 Correct 518 ms 82792 KB Output is correct
11 Correct 561 ms 82680 KB Output is correct
12 Correct 585 ms 82408 KB Output is correct
13 Correct 874 ms 94284 KB Output is correct
14 Correct 930 ms 94336 KB Output is correct
15 Correct 910 ms 94436 KB Output is correct
16 Correct 967 ms 94432 KB Output is correct
17 Correct 1132 ms 82420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 640 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 2 ms 640 KB Output is correct
10 Correct 8 ms 2048 KB Output is correct
11 Correct 8 ms 2048 KB Output is correct
12 Correct 8 ms 1920 KB Output is correct
13 Correct 9 ms 2264 KB Output is correct
14 Correct 8 ms 2176 KB Output is correct
15 Correct 9 ms 2148 KB Output is correct
16 Correct 1192 ms 82484 KB Output is correct
17 Correct 980 ms 88132 KB Output is correct
18 Correct 944 ms 84508 KB Output is correct
19 Correct 992 ms 83020 KB Output is correct
20 Correct 1022 ms 82592 KB Output is correct
21 Correct 1062 ms 82532 KB Output is correct
22 Correct 1126 ms 82408 KB Output is correct
23 Correct 741 ms 88288 KB Output is correct
24 Correct 561 ms 84364 KB Output is correct
25 Correct 518 ms 82792 KB Output is correct
26 Correct 561 ms 82680 KB Output is correct
27 Correct 585 ms 82408 KB Output is correct
28 Correct 874 ms 94284 KB Output is correct
29 Correct 930 ms 94336 KB Output is correct
30 Correct 910 ms 94436 KB Output is correct
31 Correct 967 ms 94432 KB Output is correct
32 Correct 1132 ms 82420 KB Output is correct
33 Correct 1236 ms 88984 KB Output is correct
34 Correct 348 ms 32236 KB Output is correct
35 Correct 1170 ms 92648 KB Output is correct
36 Correct 1171 ms 88716 KB Output is correct
37 Correct 1137 ms 91548 KB Output is correct
38 Correct 1204 ms 89520 KB Output is correct
39 Correct 1054 ms 104564 KB Output is correct
40 Correct 1151 ms 96584 KB Output is correct
41 Correct 833 ms 90472 KB Output is correct
42 Correct 709 ms 88680 KB Output is correct
43 Correct 1164 ms 96920 KB Output is correct
44 Correct 898 ms 90984 KB Output is correct
45 Correct 813 ms 104272 KB Output is correct
46 Correct 799 ms 104128 KB Output is correct
47 Correct 932 ms 99208 KB Output is correct
48 Correct 1006 ms 99172 KB Output is correct
49 Correct 927 ms 99048 KB Output is correct
50 Correct 970 ms 98916 KB Output is correct
51 Correct 1048 ms 95812 KB Output is correct
52 Correct 1047 ms 95660 KB Output is correct