Submission #379304

# Submission time Handle Problem Language Result Execution time Memory
379304 2021-03-17T23:42:29 Z rocks03 Werewolf (IOI18_werewolf) C++14
100 / 100
1702 ms 200780 KB
//#pragma GCC target("avx2")
//#pragma GCC optimization("O3")
//#pragma GCC optimization("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define pb push_back
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << ": " << x << " "
#define nl cout << "\n"
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

class Dsu{
public:
    vector<int> p;
    void init(int n){
        p.resize(n);
        iota(all(p), 0);
    }
    int find(int x){
        if(x == p[x]) return x;
        return (p[x] = find(p[x]));
    }
    bool unite(int a, int b){
        a = find(a), b = find(b);
        if(a == b) return false;
        p[b] = a;
        return true;
    }
};

class MergeSortTree{
public:
    vector<vector<int>> st;
    MergeSortTree(int N, vector<int>& y){
        st.resize(4 * N);
        build(0, 0, N - 1, y);
    }
    void merge(int i){
        int cl = 2 * i + 1, cr = 2 * i + 2;
        for(int x : st[cl]) st[i].pb(x);
        for(int x : st[cr]) st[i].pb(x);
        sort(all(st[i]));
    }
    void build(int i, int l, int r, vector<int>& y){
        if(l == r){
            if(y[l] != -1)
                st[i].pb(y[l]);
        } else{
            int m = (l + r) / 2;
            build(2 * i + 1, l, m, y);
            build(2 * i + 2, m + 1, r, y);
            merge(i);
        }
    }
    int query(int i, int l, int r, int qlx, int qrx, int qly, int qry){
        if(qlx <= l && r <= qrx){
            int p = lower_bound(all(st[i]), qly) - st[i].begin();
            if(p < SZ(st[i]) && qly <= st[i][p] && st[i][p] <= qry){
                return 1;
            }
            return 0;
        }
        if(qrx < l || qlx > r)
            return 0;
        int m = (l + r) / 2;
        int ans1 = query(2 * i + 1, l, m, qlx, qrx, qly, qry);
        int ans2 = query(2 * i + 2, m + 1, r, qlx, qrx, qly, qry);
        return max(ans1, ans2);
    }
};

const int MAXN = 1e6;
int N, M, Q;
vector<int> g[MAXN];
int timer, tin[MAXN], tou[MAXN];

void dfs(int v, int p){
    tin[v] = timer++;
    for(int u : g[v]){
        if(u ^ p){
            dfs(u, v);
        }
    }
    tou[v] = timer - 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){
    ::N = N; M = SZ(X), Q = SZ(S);
    int root; Dsu dsu;
    auto add_edge = [&](int u, int v){
        u = dsu.find(u), v = dsu.find(v);
        dsu.unite(root, u);
        g[root].pb(u);
        if(dsu.unite(root, v)){
            g[root].pb(v);
        }
        root++;
    };
    vector<int> edge[N];
    rep(i, 0, M){
        edge[X[i]].pb(Y[i]);
        edge[Y[i]].pb(X[i]);
    }
    vector<int> q[N];
    dsu.init(N + M); root = N;
    rep(i, 0, Q){
        q[L[i]].pb(i);
    }
    vector<int> a(Q);
    per(l, N - 1, 0){
        for(int u : edge[l]){
            if(u >= l)
                add_edge(l, u);
        }
        for(int i : q[l]){
            a[i] = dsu.find(S[i]);
        }
        q[l].clear();
    }
    timer = 0;
    rep(v, 0, N + M){
        if(dsu.find(v) == v){
            dfs(v, -1);
        }
    }
    vector<pii> p1(N + M);
    rep(v, 0, N + M){
        p1[v] = {tin[v], tou[v]};
    }
    rep(i, 0, N + M){
        g[i].clear();
    }
    dsu.init(N + M); root = N;
    rep(i, 0, Q){
        q[R[i]].pb(i);
    }
    vector<int> b(Q);
    rep(r, 0, N){
        for(int u : edge[r]){
            if(u <= r)
                add_edge(r, u);
        }
        for(int i : q[r]){
            b[i] = dsu.find(E[i]);
        }
        q[r].clear();
    }
    timer = 0;
    rep(v, 0, N + M){
        if(dsu.find(v) == v){
            dfs(v, -1);
        }
    }
    vector<pii> p2(N + M);
    rep(v, 0, N + M){
        p2[v] = {tin[v], tou[v]};
    }
    vector<int> y(N + M, -1);
    rep(v, 0, N){
        y[p1[v].ff] = p2[v].ff;
    }
    MergeSortTree mst(N + M, y);
    vector<int> ans(Q);
    rep(i, 0, Q){
        int s = a[i], t = b[i];
        ans[i] = mst.query(0, 0, N + M - 1, p1[s].ff, p1[s].ss, p2[t].ff, p2[t].ss);
    }
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 24044 KB Output is correct
2 Correct 15 ms 23916 KB Output is correct
3 Correct 15 ms 23916 KB Output is correct
4 Correct 16 ms 23788 KB Output is correct
5 Correct 15 ms 23916 KB Output is correct
6 Correct 15 ms 23916 KB Output is correct
7 Correct 15 ms 23916 KB Output is correct
8 Correct 18 ms 23916 KB Output is correct
9 Correct 15 ms 23916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 24044 KB Output is correct
2 Correct 15 ms 23916 KB Output is correct
3 Correct 15 ms 23916 KB Output is correct
4 Correct 16 ms 23788 KB Output is correct
5 Correct 15 ms 23916 KB Output is correct
6 Correct 15 ms 23916 KB Output is correct
7 Correct 15 ms 23916 KB Output is correct
8 Correct 18 ms 23916 KB Output is correct
9 Correct 15 ms 23916 KB Output is correct
10 Correct 26 ms 25836 KB Output is correct
11 Correct 26 ms 25708 KB Output is correct
12 Correct 24 ms 25708 KB Output is correct
13 Correct 25 ms 25836 KB Output is correct
14 Correct 24 ms 25836 KB Output is correct
15 Correct 27 ms 26348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1060 ms 154592 KB Output is correct
2 Correct 1238 ms 159072 KB Output is correct
3 Correct 1148 ms 155872 KB Output is correct
4 Correct 1048 ms 155016 KB Output is correct
5 Correct 1042 ms 154592 KB Output is correct
6 Correct 1056 ms 154452 KB Output is correct
7 Correct 964 ms 152672 KB Output is correct
8 Correct 1197 ms 159328 KB Output is correct
9 Correct 876 ms 155488 KB Output is correct
10 Correct 912 ms 154080 KB Output is correct
11 Correct 923 ms 154228 KB Output is correct
12 Correct 847 ms 154208 KB Output is correct
13 Correct 1024 ms 159840 KB Output is correct
14 Correct 1028 ms 159828 KB Output is correct
15 Correct 1037 ms 159980 KB Output is correct
16 Correct 1052 ms 159840 KB Output is correct
17 Correct 950 ms 152828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 24044 KB Output is correct
2 Correct 15 ms 23916 KB Output is correct
3 Correct 15 ms 23916 KB Output is correct
4 Correct 16 ms 23788 KB Output is correct
5 Correct 15 ms 23916 KB Output is correct
6 Correct 15 ms 23916 KB Output is correct
7 Correct 15 ms 23916 KB Output is correct
8 Correct 18 ms 23916 KB Output is correct
9 Correct 15 ms 23916 KB Output is correct
10 Correct 26 ms 25836 KB Output is correct
11 Correct 26 ms 25708 KB Output is correct
12 Correct 24 ms 25708 KB Output is correct
13 Correct 25 ms 25836 KB Output is correct
14 Correct 24 ms 25836 KB Output is correct
15 Correct 27 ms 26348 KB Output is correct
16 Correct 1060 ms 154592 KB Output is correct
17 Correct 1238 ms 159072 KB Output is correct
18 Correct 1148 ms 155872 KB Output is correct
19 Correct 1048 ms 155016 KB Output is correct
20 Correct 1042 ms 154592 KB Output is correct
21 Correct 1056 ms 154452 KB Output is correct
22 Correct 964 ms 152672 KB Output is correct
23 Correct 1197 ms 159328 KB Output is correct
24 Correct 876 ms 155488 KB Output is correct
25 Correct 912 ms 154080 KB Output is correct
26 Correct 923 ms 154228 KB Output is correct
27 Correct 847 ms 154208 KB Output is correct
28 Correct 1024 ms 159840 KB Output is correct
29 Correct 1028 ms 159828 KB Output is correct
30 Correct 1037 ms 159980 KB Output is correct
31 Correct 1052 ms 159840 KB Output is correct
32 Correct 950 ms 152828 KB Output is correct
33 Correct 1233 ms 155656 KB Output is correct
34 Correct 581 ms 140140 KB Output is correct
35 Correct 1402 ms 163872 KB Output is correct
36 Correct 1201 ms 156228 KB Output is correct
37 Correct 1411 ms 160076 KB Output is correct
38 Correct 1287 ms 158048 KB Output is correct
39 Correct 1056 ms 163168 KB Output is correct
40 Correct 1317 ms 200780 KB Output is correct
41 Correct 1285 ms 158560 KB Output is correct
42 Correct 983 ms 155616 KB Output is correct
43 Correct 1702 ms 185568 KB Output is correct
44 Correct 1365 ms 160224 KB Output is correct
45 Correct 1130 ms 165216 KB Output is correct
46 Correct 1334 ms 163168 KB Output is correct
47 Correct 1044 ms 160992 KB Output is correct
48 Correct 1012 ms 159968 KB Output is correct
49 Correct 1054 ms 160992 KB Output is correct
50 Correct 1006 ms 160096 KB Output is correct
51 Correct 1206 ms 198240 KB Output is correct
52 Correct 1227 ms 198112 KB Output is correct