Submission #809050

# Submission time Handle Problem Language Result Execution time Memory
809050 2023-08-05T14:54:22 Z math_rabbit_1028 Werewolf (IOI18_werewolf) C++14
100 / 100
1428 ms 257564 KB
#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
int n, m, q;
vector<int> adj[202020];

struct disjoint {
    int root[202020], num[202020];
    int r, mn[404040], mx[404040], par[404040];
    vector<int> child[404040];
    int _find(int v) {
        if (v == root[v]) return v;
        else return root[v] = _find(root[v]);
    }
    void _union(int v, int u) {
        v = _find(v);
        u = _find(u);
        if (v == u) return;
        if (v > u) swap(u, v);
        root[u] = v;
        child[r].push_back(num[v]);
        child[r].push_back(num[u]);
        par[num[v]] = par[num[u]] = r;
        mn[r] = min(mn[num[v]], mn[num[u]]);
        mx[r] = max(mx[num[v]], mx[num[u]]);
        num[v] = r;
        r++;
    }

    int ord[808080], lt[404040], rt[404040], s, inv[202020];
    void ett() {
        s = 0;
        DFS(r - 1);
        for (int i = 1; i <= s; i++) {
            if (ord[i] < n) inv[ord[i]] = i;
        }
    }
    void DFS(int v) {
        ord[++s] = v;
        lt[v] = s;
        for (int i = 0; i < child[v].size(); i++) {
            int next = child[v][i];
            DFS(next);
            ord[++s] = v;
        }
        rt[v] = s;
    }

    int table[404040][20];
    void sparse() {
        for (int i = 0; i < r; i++) table[i][0] = par[i];
        for (int k = 1; k < 20; k++) {
            for (int i = 0; i < r; i++) {
                if (table[i][k - 1] < 0) table[i][k] = -1;
                else table[i][k] = table[table[i][k - 1]][k - 1];
            }
        }
    }

    pair<int, int> get_segment(int st, int limit, int f) {
        if (f) {
            int now = st;
            for (int k = 19; k >= 0; k--) {
                if (table[now][k] != -1 && mn[table[now][k]] >= limit) now = table[now][k];
            }
            return {lt[now], rt[now]};
        }
        else {
            int now = st;
            for (int k = 19; k >= 0; k--) {
                if (table[now][k] != -1 && mx[table[now][k]] <= limit) now = table[now][k];
            }
            return {lt[now], rt[now]};
        }
    }
} left_tree, right_tree;

void make_left_tree() {
    for (int i = 0; i < n; i++) left_tree.root[i] = i;
    for (int i = 0; i < n; i++) left_tree.num[i] = i;
    for (int i = 0; i < n; i++) left_tree.mn[i] = left_tree.mx[i] = i;
    left_tree.r = n;

    for (int i = n - 1; i >= 0; i--) {
        for (int j = 0; j < adj[i].size(); j++) {
            int v = adj[i][j];
            if (v >= i) left_tree._union(i, v);
        }
    }
    left_tree.par[left_tree.r - 1] = -1;
}
void make_right_tree() {
    for (int i = 0; i < n; i++) right_tree.root[i] = i;
    for (int i = 0; i < n; i++) right_tree.num[i] = i;
    for (int i = 0; i < n; i++) right_tree.mn[i] = right_tree.mx[i] = i;
    right_tree.r = n;

    for (int i = 0; i <= n - 1; i++) {
        for (int j = 0; j < adj[i].size(); j++) {
            int v = adj[i][j];
            if (v <= i) right_tree._union(i, v);
        }
    }
    right_tree.par[right_tree.r - 1] = -1;
}

vector< pair<int, int> > p;
struct node {
    int sum;
    int lt, rt;
};
vector<node> tree;
vector< pair<int, int> > pst_root;

void init(int v, int st, int ed) {
    tree.push_back({0, 2 * v, 2 * v + 1});
    if (st == ed) return;
    int mid = (st + ed) / 2;
    init(2 * v, st, mid);
    init(2 * v + 1, mid + 1, ed);
    tree[v].sum = tree[tree[v].lt].sum + tree[tree[v].rt].sum;
}

void update(int v, int st, int ed, int idx, int val) {
    if (st > idx || ed < idx) return;
    if (st == ed) {
        tree[v].sum += 1;
        return;
    }
    int mid = (st + ed) / 2;
    if (idx <= mid) {
        tree.push_back(tree[tree[v].lt]);
        tree[v].lt = tree.size() - 1;
        update(tree[v].lt, st, mid, idx, val);
    }
    else {
        tree.push_back(tree[tree[v].rt]);
        tree[v].rt = tree.size() - 1;
        update(tree[v].rt, mid + 1, ed, idx, val);
    }
    tree[v].sum = tree[tree[v].lt].sum + tree[tree[v].rt].sum;
}

int get(int v, int st, int ed, int lt, int rt) {
    if (st > rt || ed < lt) return 0;
    if (lt <= st && ed <= rt) return tree[v].sum;
    int mid = (st + ed) / 2;
    return get(tree[v].lt, st, mid, lt, rt) + get(tree[v].rt, mid + 1, ed, lt, rt);
}

vector<int> ans;
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 = X.size();
    for (int i = 0; i < m; i++) {
        adj[X[i]].push_back(Y[i]);
        adj[Y[i]].push_back(X[i]);
    }

    make_left_tree();
    left_tree.ett();
    left_tree.sparse();

    make_right_tree();
    right_tree.ett();
    right_tree.sparse();

    q = S.size();
    for (int i = 0; i < q; i++) ans.push_back(0);

    pst_root.push_back({0, 0});
    tree.push_back({0, 0, 0});
    init(pst_root.back().second, 1, 4 * n);
    for (int i = 0; i < n; i++) p.push_back({left_tree.inv[i], right_tree.inv[i]});
    sort(p.begin(), p.end());
    for (int i = 0; i < n; i++) {
        tree.push_back(tree[pst_root.back().second]);
        pst_root.push_back({p[i].first, tree.size() - 1});
        update(pst_root.back().second, 1, 4 * n, p[i].second, 1);
    }

    //cout << get(pst_root.back().second, 1, 4 * n, 1, 4 * n) << "\n";

    for (int i = 0; i < q; i++) {
        int s = S[i], e = E[i], l = L[i], r = R[i];
        pair<int, int> left_segment = left_tree.get_segment(s, l, 1);
        pair<int, int> right_segment = right_tree.get_segment(e, r, 0);
        /*
        int ch[3030];
        for (int j = 0; j < n; j++) ch[j] = 0;
        for (int j = left_segment.first; j <= left_segment.second; j++)
            if (left_tree.ord[j] < n) ch[left_tree.ord[j]] = 1;
        for (int j = right_segment.first; j <= right_segment.second; j++)
            if (right_tree.ord[j] < n && ch[right_tree.ord[j]] == 1) ans[i] = 1;
        */
        int cnt = 0;
        int ed = lower_bound(pst_root.begin(), pst_root.end(), make_pair(left_segment.second + 1, 0)) - pst_root.begin();
        if (ed > 0) cnt += get(pst_root[ed - 1].second, 1, 4 * n, right_segment.first, right_segment.second);
        int st = lower_bound(pst_root.begin(), pst_root.end(), make_pair(left_segment.first, 0)) - pst_root.begin();
        if (st > 0) cnt -= get(pst_root[st - 1].second, 1, 4 * n, right_segment.first, right_segment.second);
        if (cnt > 0) ans[i] = 1;
    }

    return ans;
}

Compilation message

werewolf.cpp: In member function 'void disjoint::DFS(int)':
werewolf.cpp:41:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |         for (int i = 0; i < child[v].size(); i++) {
      |                         ~~^~~~~~~~~~~~~~~~~
werewolf.cpp: In function 'void make_left_tree()':
werewolf.cpp:85:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |         for (int j = 0; j < adj[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~
werewolf.cpp: In function 'void make_right_tree()':
werewolf.cpp:99:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |         for (int j = 0; j < adj[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 24148 KB Output is correct
2 Correct 13 ms 24148 KB Output is correct
3 Correct 12 ms 24192 KB Output is correct
4 Correct 13 ms 24084 KB Output is correct
5 Correct 13 ms 24268 KB Output is correct
6 Correct 13 ms 24148 KB Output is correct
7 Correct 13 ms 24268 KB Output is correct
8 Correct 12 ms 24148 KB Output is correct
9 Correct 13 ms 24148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 24148 KB Output is correct
2 Correct 13 ms 24148 KB Output is correct
3 Correct 12 ms 24192 KB Output is correct
4 Correct 13 ms 24084 KB Output is correct
5 Correct 13 ms 24268 KB Output is correct
6 Correct 13 ms 24148 KB Output is correct
7 Correct 13 ms 24268 KB Output is correct
8 Correct 12 ms 24148 KB Output is correct
9 Correct 13 ms 24148 KB Output is correct
10 Correct 20 ms 27608 KB Output is correct
11 Correct 20 ms 27736 KB Output is correct
12 Correct 20 ms 27568 KB Output is correct
13 Correct 21 ms 27608 KB Output is correct
14 Correct 23 ms 27608 KB Output is correct
15 Correct 20 ms 27608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 904 ms 244472 KB Output is correct
2 Correct 997 ms 249136 KB Output is correct
3 Correct 908 ms 247240 KB Output is correct
4 Correct 833 ms 246304 KB Output is correct
5 Correct 787 ms 246236 KB Output is correct
6 Correct 834 ms 246080 KB Output is correct
7 Correct 738 ms 246096 KB Output is correct
8 Correct 917 ms 249172 KB Output is correct
9 Correct 726 ms 247144 KB Output is correct
10 Correct 601 ms 246248 KB Output is correct
11 Correct 625 ms 246196 KB Output is correct
12 Correct 637 ms 246168 KB Output is correct
13 Correct 1097 ms 249172 KB Output is correct
14 Correct 1060 ms 249240 KB Output is correct
15 Correct 1073 ms 249228 KB Output is correct
16 Correct 1076 ms 249228 KB Output is correct
17 Correct 739 ms 246072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 24148 KB Output is correct
2 Correct 13 ms 24148 KB Output is correct
3 Correct 12 ms 24192 KB Output is correct
4 Correct 13 ms 24084 KB Output is correct
5 Correct 13 ms 24268 KB Output is correct
6 Correct 13 ms 24148 KB Output is correct
7 Correct 13 ms 24268 KB Output is correct
8 Correct 12 ms 24148 KB Output is correct
9 Correct 13 ms 24148 KB Output is correct
10 Correct 20 ms 27608 KB Output is correct
11 Correct 20 ms 27736 KB Output is correct
12 Correct 20 ms 27568 KB Output is correct
13 Correct 21 ms 27608 KB Output is correct
14 Correct 23 ms 27608 KB Output is correct
15 Correct 20 ms 27608 KB Output is correct
16 Correct 904 ms 244472 KB Output is correct
17 Correct 997 ms 249136 KB Output is correct
18 Correct 908 ms 247240 KB Output is correct
19 Correct 833 ms 246304 KB Output is correct
20 Correct 787 ms 246236 KB Output is correct
21 Correct 834 ms 246080 KB Output is correct
22 Correct 738 ms 246096 KB Output is correct
23 Correct 917 ms 249172 KB Output is correct
24 Correct 726 ms 247144 KB Output is correct
25 Correct 601 ms 246248 KB Output is correct
26 Correct 625 ms 246196 KB Output is correct
27 Correct 637 ms 246168 KB Output is correct
28 Correct 1097 ms 249172 KB Output is correct
29 Correct 1060 ms 249240 KB Output is correct
30 Correct 1073 ms 249228 KB Output is correct
31 Correct 1076 ms 249228 KB Output is correct
32 Correct 739 ms 246072 KB Output is correct
33 Correct 1310 ms 251960 KB Output is correct
34 Correct 290 ms 52548 KB Output is correct
35 Correct 1428 ms 254028 KB Output is correct
36 Correct 1070 ms 251476 KB Output is correct
37 Correct 1334 ms 253548 KB Output is correct
38 Correct 1164 ms 251948 KB Output is correct
39 Correct 1237 ms 257464 KB Output is correct
40 Correct 982 ms 256792 KB Output is correct
41 Correct 956 ms 253160 KB Output is correct
42 Correct 653 ms 251480 KB Output is correct
43 Correct 1318 ms 256132 KB Output is correct
44 Correct 1161 ms 253532 KB Output is correct
45 Correct 874 ms 257564 KB Output is correct
46 Correct 994 ms 257468 KB Output is correct
47 Correct 1116 ms 254196 KB Output is correct
48 Correct 1067 ms 254048 KB Output is correct
49 Correct 1105 ms 254200 KB Output is correct
50 Correct 1075 ms 254116 KB Output is correct
51 Correct 840 ms 257544 KB Output is correct
52 Correct 824 ms 257428 KB Output is correct