Submission #448501

# Submission time Handle Problem Language Result Execution time Memory
448501 2021-07-30T10:16:48 Z lohacho Werewolf (IOI18_werewolf) C++14
100 / 100
1371 ms 127660 KB
#include "werewolf.h"
#include <bits/stdc++.h>
#define umi(x, y) (x = min(x, y))
#define uma(x, y) (x = max(x, y))
using namespace std;

struct Dsu{
    int n, re;
    vector<int> pr, rk, mn, es, ee;
    vector<vector<int>> way, lca;
    Dsu(int N, int RE):n(N), re(RE){
        pr.resize(n), rk.resize(n), es.resize(n), ee.resize(n);
        mn.resize(n), way.resize(n), lca.resize(n, vector<int>(20, -1));
        for(int i = 0; i < n; ++i){
            pr[i] = i, rk[i] = 1;
            if(re) mn[i] = -i;
            else mn[i] = i;
        }
    }
    inline int get(int x){
        return x == pr[x] ? x : pr[x] = get(pr[x]);
    }
    inline bool unite(int x, int y){
        x = get(x), y = get(y);
        if(x != y){
            if(mn[x] > mn[y]) swap(x, y);
            pr[y] = x, rk[x] += rk[y], umi(mn[x], mn[y]);
            way[x].push_back(y), lca[y][0] = x;
            return true;
        }
        return false;
    }
};

const int NS = (int)2e5 + 4;
int n, m, q;
vector<vector<int>> way(NS);
int cnt;

void makegraph(Dsu&gr){
    for(int i = 0; i < n; ++i){
        int now = n - 1 - i;
        if(gr.re) now = i;
        for(auto&nxt:way[now]){
            if(gr.mn[gr.get(nxt)] >= gr.mn[gr.get(now)]) gr.unite(now, nxt);
        }
    }
    for(int j = 1; j < 20; ++j){
        for(int i = 0; i < n; ++i){
            if(gr.lca[i][j - 1] != -1)
                 gr.lca[i][j] = gr.lca[gr.lca[i][j - 1]][j - 1];
        }
    }
}

void euler(int x, int pr, Dsu&gr){
    gr.es[x] = cnt++;
    for(auto&nxt:gr.way[x]){
        if(nxt == pr) continue;
        euler(nxt, x, gr);
    }
    gr.ee[x] = cnt - 1;
}

struct Data{
    int xl, xr = -1, yd, yu, num;
    Data(){}
    Data(int a, int b, int c, int d):xl(a), xr(b), yd(c), yu(d){}
    Data(int a, int b, int c, int d, int e):xl(a), xr(b), yd(c), yu(d), num(e){}
    bool operator<(const Data&r)const{
        return xl > r.xl || (xl == r.xl && xr < r.xr);
    }
};

struct Seg{
    int n;
    vector<int> tree;
    Seg(int N):n(N){
        tree.resize(n * 4, (int)1e9);
    }
    void add(int x, int l, int r, int val){
        tree[x] = val;
    }
    void push(int x, int l, int r, int pl, int pr, int val){
        if(pr < l || pl > r || pl > pr) return;
        if(l >= pl && r <= pr){
            add(x, l, r, val);
            return;
        }
        int mid = (l + r) >> 1;
        push(x * 2, l, mid, pl, pr, val), push(x * 2 + 1, mid + 1, r, pl, pr, val);
        tree[x] = min(tree[x * 2], tree[x * 2 + 1]);
    }
    int get(int x, int l, int r, int fl, int fr){
        if(fr < l || fl > r || fl > fr) return (int)1e9;
        if(l >= fl && r <= fr) return tree[x];
        int mid = (l + r) >> 1;
        return min(get(x * 2, l, mid, fl, fr), get(x * 2 + 1, mid + 1, r, fl, fr));
    }
};

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, m = (int)X.size(), q = (int)S.size();
    for(int i = 0; i < (int)X.size(); ++i){
        way[X[i]].push_back(Y[i]);
        way[Y[i]].push_back(X[i]);
    }
    Dsu mn(n, 0), mx(n, 1);
    makegraph(mn), makegraph(mx);
    euler(mn.get(0), -1, mn), cnt = 0, euler(mx.get(0), -1, mx);
    vector<Data> que;
    for(int i = 0; i < n; ++i){
        que.push_back(Data(mn.es[i], -1, mx.es[i], -1));
    }
    for(int i = 0; i < q; ++i){
        int spos = S[i];
        for(int j = 19; j >= 0; --j){
            if(mn.lca[spos][j] != -1 && mn.mn[mn.lca[spos][j]] >= L[i]){
                spos = mn.lca[spos][j];
            }
        }
        int epos = E[i];
        for(int j = 19; j >= 0; --j){
            if(mx.lca[epos][j] != -1 && mx.mn[mx.lca[epos][j]] >= -R[i]){
                epos = mx.lca[epos][j];
            }
        }
        que.push_back(Data(mn.es[spos], mn.ee[spos], mx.es[epos], mx.ee[epos], i));
    }
    sort(que.begin(), que.end());
    Seg tree(n + 4);
    vector<int> ans(q);
    for(auto&i:que){
        if(i.xr == -1){
            tree.push(1, 0, n - 1, i.yd, i.yd, i.xl);
        }
        else{
            int val = tree.get(1, 0, n - 1, i.yd, i.yu);
            if(val <= i.xr){
                ans[i.num] = 1;
            }
        }
    }
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5012 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 4 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 4 ms 4940 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 4 ms 4940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5012 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 4 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 4 ms 4940 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 4 ms 4940 KB Output is correct
10 Correct 12 ms 6724 KB Output is correct
11 Correct 14 ms 6700 KB Output is correct
12 Correct 10 ms 6604 KB Output is correct
13 Correct 11 ms 6808 KB Output is correct
14 Correct 11 ms 6880 KB Output is correct
15 Correct 13 ms 6732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 857 ms 113348 KB Output is correct
2 Correct 1152 ms 118056 KB Output is correct
3 Correct 1026 ms 114932 KB Output is correct
4 Correct 843 ms 113692 KB Output is correct
5 Correct 859 ms 113492 KB Output is correct
6 Correct 854 ms 113444 KB Output is correct
7 Correct 863 ms 113332 KB Output is correct
8 Correct 1106 ms 117936 KB Output is correct
9 Correct 879 ms 114996 KB Output is correct
10 Correct 663 ms 113640 KB Output is correct
11 Correct 710 ms 113540 KB Output is correct
12 Correct 727 ms 113436 KB Output is correct
13 Correct 1304 ms 123868 KB Output is correct
14 Correct 1370 ms 123888 KB Output is correct
15 Correct 1298 ms 123832 KB Output is correct
16 Correct 1308 ms 123984 KB Output is correct
17 Correct 877 ms 113464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5012 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 4 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 4 ms 4940 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 4 ms 4940 KB Output is correct
10 Correct 12 ms 6724 KB Output is correct
11 Correct 14 ms 6700 KB Output is correct
12 Correct 10 ms 6604 KB Output is correct
13 Correct 11 ms 6808 KB Output is correct
14 Correct 11 ms 6880 KB Output is correct
15 Correct 13 ms 6732 KB Output is correct
16 Correct 857 ms 113348 KB Output is correct
17 Correct 1152 ms 118056 KB Output is correct
18 Correct 1026 ms 114932 KB Output is correct
19 Correct 843 ms 113692 KB Output is correct
20 Correct 859 ms 113492 KB Output is correct
21 Correct 854 ms 113444 KB Output is correct
22 Correct 863 ms 113332 KB Output is correct
23 Correct 1106 ms 117936 KB Output is correct
24 Correct 879 ms 114996 KB Output is correct
25 Correct 663 ms 113640 KB Output is correct
26 Correct 710 ms 113540 KB Output is correct
27 Correct 727 ms 113436 KB Output is correct
28 Correct 1304 ms 123868 KB Output is correct
29 Correct 1370 ms 123888 KB Output is correct
30 Correct 1298 ms 123832 KB Output is correct
31 Correct 1308 ms 123984 KB Output is correct
32 Correct 877 ms 113464 KB Output is correct
33 Correct 1108 ms 114056 KB Output is correct
34 Correct 374 ms 40936 KB Output is correct
35 Correct 1290 ms 117524 KB Output is correct
36 Correct 1036 ms 114264 KB Output is correct
37 Correct 1162 ms 116464 KB Output is correct
38 Correct 1053 ms 115164 KB Output is correct
39 Correct 1101 ms 127308 KB Output is correct
40 Correct 1358 ms 124924 KB Output is correct
41 Correct 953 ms 115864 KB Output is correct
42 Correct 798 ms 114260 KB Output is correct
43 Correct 1342 ms 123232 KB Output is correct
44 Correct 1130 ms 116412 KB Output is correct
45 Correct 984 ms 127660 KB Output is correct
46 Correct 994 ms 127292 KB Output is correct
47 Correct 1371 ms 123984 KB Output is correct
48 Correct 1318 ms 123716 KB Output is correct
49 Correct 1267 ms 123960 KB Output is correct
50 Correct 1343 ms 123852 KB Output is correct
51 Correct 1202 ms 124880 KB Output is correct
52 Correct 1162 ms 124844 KB Output is correct