Submission #964455

# Submission time Handle Problem Language Result Execution time Memory
964455 2024-04-16T23:23:52 Z TAhmed33 Werewolf (IOI18_werewolf) C++17
100 / 100
1080 ms 172636 KB
#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4e5 + 25;
const int B = 21;
pair <int, int> edges[MAXN];
array <int, 4> queries[MAXN]; 
int ans[MAXN]; 
int n, m, q;
struct DSU {
    int par[MAXN];
    void init (int n) {
        for (int i = 1; i <= n; i++) par[i] = i;
    }
    int find (int x) {
        return par[x] == x ? x : par[x] = find(par[x]);
    }
    bool merge (int x, int y) {
        x = find(x), y = find(y);
        if (x == y) return 0;
        if (x > y) swap(x, y);
        par[x] = y;
        return 1;
    }
} dd;
struct tree {
    int p[MAXN]; vector <int> adj[MAXN];
    int cnt;
    int in[MAXN], out[MAXN], tt, dp[MAXN];
    int sparse[B][MAXN];
    void init (int n) {
        cnt = n;
    }
    void link (int a, int b) {
        cnt++; p[a] = p[b] = cnt;
        adj[cnt].push_back(a); 
        adj[cnt].push_back(b);
    }
    void dfs (int pos, bool c) {
        if (adj[pos].empty()) {
            dp[pos] = pos;
            return;
        }
        for (auto j : adj[pos]) {
            dfs(j, c);
        }
        if (!c) {
            if (dp[adj[pos][0]] > dp[adj[pos][1]]) swap(adj[pos][0], adj[pos][1]);
            dp[pos] = dp[adj[pos][0]];
        } else {
            if (dp[adj[pos][0]] < dp[adj[pos][1]]) swap(adj[pos][0], adj[pos][1]);
            dp[pos] = dp[adj[pos][0]];
        }
    }
    void dfs2 (int pos) {
        if (adj[pos].empty()) {
            in[pos] = out[pos] = ++tt;
            return;
        }
        for (auto j : adj[pos]) {
            dfs2(j);
        }
        in[pos] = in[adj[pos][0]];
        out[pos] = out[adj[pos][1]];
    }
    void build (bool c) {
        dfs(cnt, c);
        dfs2(cnt);
        for (int i = 1; i <= cnt; i++) {
            sparse[0][i] = p[i];
        }
        for (int i = 1; i < B; i++) {
            for (int j = 1; j <= cnt; j++) {
                sparse[i][j] = sparse[i - 1][sparse[i - 1][j]];
            }
        }
    }
    pair <int, int> get (int x, int y, bool c) {
        for (int i = B - 1; i >= 0; i--) {
            if (!sparse[i][x]) continue;
            if (!c) {
                if (dp[sparse[i][x]] >= y) x = sparse[i][x]; 
            } else {
                if (dp[sparse[i][x]] <= y) x = sparse[i][x];
            }
        }
        return {in[x], out[x]};
    }
} cur[2];
struct BIT {
    int tree[MAXN];
    void add (int x, int y) {
        for (; x < MAXN; x += x & (-x)) tree[x] += y;
    }
    int sum (int x) {
        int ret = 0;
        for (; x > 0; x -= x & (-x)) ret += tree[x];
        return ret;
    }
    int sum (int l, int r) {
        return sum(r) - sum(l - 1);
    }
} pp;
vector <array <int, 4>> events[MAXN];
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 = (int)X.size(); q = S.size();
    for (int i = 0; i < m; i++) {
        X[i]++; Y[i]++;
    }
    for (int i = 0; i < q; i++) {
        S[i]++; E[i]++; L[i]++; R[i]++;
    }
    for (int i = 1; i <= m; i++) {
        edges[i] = {X[i - 1], Y[i - 1]};
        if (edges[i].first > edges[i].second) swap(edges[i].first, edges[i].second);
    }
    for (int i = 1; i <= q; i++) {
        queries[i] = {S[i - 1], E[i - 1], L[i - 1], R[i - 1]};
    }
    sort(edges + 1, edges + m + 1, [] (pair <int, int> &a, pair <int, int> &b) {
        return a.first > b.first;
    });
    dd.init(2 * n - 1); cur[0].init(n);
    for (int i = 1; i <= m; i++) {
        int a = dd.find(edges[i].first), b = dd.find(edges[i].second);
        if (a == b) continue;
        cur[0].link(a, b);
        dd.merge(a, b); dd.merge(a, cur[0].cnt);
    }
    sort(edges + 1, edges + m + 1, [] (pair <int, int> &a, pair <int, int> &b) {
        return a.second < b.second;
    });
    dd.init(2 * n - 1); cur[1].init(n);
    for (int i = 1; i <= m; i++) {
        int a = dd.find(edges[i].first), b = dd.find(edges[i].second);
        if (a == b) continue;
        cur[1].link(a, b);
        dd.merge(a, b); dd.merge(a, cur[1].cnt);
    }
    cur[0].build(0); cur[1].build(1);
    for (int i = 1; i <= n; i++) {
        int x = cur[0].in[i], y = cur[1].in[i];
        events[x].push_back({0, y, y, 0});
    }
    for (int i = 1; i <= q; i++) {
        if (queries[i][0] < queries[i][2] || queries[i][1] > queries[i][3]) {
            ans[i] = 0;
            continue;
        }
        auto g = cur[0].get(queries[i][0], queries[i][2], 0);
        auto h = cur[1].get(queries[i][1], queries[i][3], 1);
        events[g.first - 1].push_back({-1, h.first, h.second, i});
        events[g.second].push_back({1, h.first, h.second, i});
    }
    for (int i = 1; i <= n; i++) {
        for (auto j : events[i]) {
            if (j[0] == 0) {
                pp.add(j[1], 1);
            }
        }
        for (auto j : events[i]) {
            if (j[0] != 0) {
                int g = pp.sum(j[1], j[2]);
                ans[j[3]] += j[0] * g;
            }
        }
    }
    vector <int> ret;
    for (int i = 1; i <= q; i++) ret.push_back(ans[i] != 0);
    return ret;
}   
# Verdict Execution time Memory Grader output
1 Correct 62 ms 115280 KB Output is correct
2 Correct 19 ms 115292 KB Output is correct
3 Correct 17 ms 115368 KB Output is correct
4 Correct 18 ms 115292 KB Output is correct
5 Correct 16 ms 115288 KB Output is correct
6 Correct 17 ms 115292 KB Output is correct
7 Correct 16 ms 115292 KB Output is correct
8 Correct 16 ms 115524 KB Output is correct
9 Correct 17 ms 115292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 115280 KB Output is correct
2 Correct 19 ms 115292 KB Output is correct
3 Correct 17 ms 115368 KB Output is correct
4 Correct 18 ms 115292 KB Output is correct
5 Correct 16 ms 115288 KB Output is correct
6 Correct 17 ms 115292 KB Output is correct
7 Correct 16 ms 115292 KB Output is correct
8 Correct 16 ms 115524 KB Output is correct
9 Correct 17 ms 115292 KB Output is correct
10 Correct 21 ms 116060 KB Output is correct
11 Correct 24 ms 116008 KB Output is correct
12 Correct 21 ms 116060 KB Output is correct
13 Correct 21 ms 116060 KB Output is correct
14 Correct 21 ms 116016 KB Output is correct
15 Correct 21 ms 115984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 838 ms 156904 KB Output is correct
2 Correct 760 ms 168076 KB Output is correct
3 Correct 615 ms 165176 KB Output is correct
4 Correct 546 ms 164164 KB Output is correct
5 Correct 636 ms 164148 KB Output is correct
6 Correct 698 ms 165500 KB Output is correct
7 Correct 562 ms 164072 KB Output is correct
8 Correct 605 ms 168108 KB Output is correct
9 Correct 428 ms 164024 KB Output is correct
10 Correct 336 ms 163212 KB Output is correct
11 Correct 360 ms 163044 KB Output is correct
12 Correct 423 ms 163256 KB Output is correct
13 Correct 807 ms 167324 KB Output is correct
14 Correct 833 ms 167400 KB Output is correct
15 Correct 866 ms 167420 KB Output is correct
16 Correct 827 ms 167348 KB Output is correct
17 Correct 581 ms 164032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 115280 KB Output is correct
2 Correct 19 ms 115292 KB Output is correct
3 Correct 17 ms 115368 KB Output is correct
4 Correct 18 ms 115292 KB Output is correct
5 Correct 16 ms 115288 KB Output is correct
6 Correct 17 ms 115292 KB Output is correct
7 Correct 16 ms 115292 KB Output is correct
8 Correct 16 ms 115524 KB Output is correct
9 Correct 17 ms 115292 KB Output is correct
10 Correct 21 ms 116060 KB Output is correct
11 Correct 24 ms 116008 KB Output is correct
12 Correct 21 ms 116060 KB Output is correct
13 Correct 21 ms 116060 KB Output is correct
14 Correct 21 ms 116016 KB Output is correct
15 Correct 21 ms 115984 KB Output is correct
16 Correct 838 ms 156904 KB Output is correct
17 Correct 760 ms 168076 KB Output is correct
18 Correct 615 ms 165176 KB Output is correct
19 Correct 546 ms 164164 KB Output is correct
20 Correct 636 ms 164148 KB Output is correct
21 Correct 698 ms 165500 KB Output is correct
22 Correct 562 ms 164072 KB Output is correct
23 Correct 605 ms 168108 KB Output is correct
24 Correct 428 ms 164024 KB Output is correct
25 Correct 336 ms 163212 KB Output is correct
26 Correct 360 ms 163044 KB Output is correct
27 Correct 423 ms 163256 KB Output is correct
28 Correct 807 ms 167324 KB Output is correct
29 Correct 833 ms 167400 KB Output is correct
30 Correct 866 ms 167420 KB Output is correct
31 Correct 827 ms 167348 KB Output is correct
32 Correct 581 ms 164032 KB Output is correct
33 Correct 1080 ms 166200 KB Output is correct
34 Correct 260 ms 152952 KB Output is correct
35 Correct 1067 ms 168460 KB Output is correct
36 Correct 1062 ms 166080 KB Output is correct
37 Correct 1070 ms 167880 KB Output is correct
38 Correct 1052 ms 166584 KB Output is correct
39 Correct 897 ms 171380 KB Output is correct
40 Correct 683 ms 172636 KB Output is correct
41 Correct 527 ms 166300 KB Output is correct
42 Correct 405 ms 164636 KB Output is correct
43 Correct 803 ms 172060 KB Output is correct
44 Correct 709 ms 168008 KB Output is correct
45 Correct 518 ms 169728 KB Output is correct
46 Correct 519 ms 169992 KB Output is correct
47 Correct 935 ms 167816 KB Output is correct
48 Correct 852 ms 167632 KB Output is correct
49 Correct 829 ms 167432 KB Output is correct
50 Correct 808 ms 167380 KB Output is correct
51 Correct 551 ms 172524 KB Output is correct
52 Correct 521 ms 172520 KB Output is correct