Submission #666156

# Submission time Handle Problem Language Result Execution time Memory
666156 2022-11-27T16:32:40 Z amsraman Werewolf (IOI18_werewolf) C++14
100 / 100
546 ms 75616 KB
#include <bits/stdc++.h>

using namespace std;

template <typename T>
struct FenwickTree {
    int n;
    vector<T> bit;
    FenwickTree(int n): n(n), bit(n, 0) {};
    FenwickTree(vector<T> & init): n((int) init.size()), bit((int) init.size()) {
        copy(init.begin(), init.end(), bit.begin());
        for(int i = 1; i <= n; i++) {
            if(i + (i & -i) <= n) {
                bit[i + (i & -i) - 1] += bit[i - 1];
            }
        }
    }
    T qry(int k) {
        T ret = 0;
        for(k++; k > 0; k -= k & -k) {
            ret += bit[k - 1];
        }
        return ret;
    }
    T qry(int l, int r) {
        return qry(r) - qry(l - 1);
    }
    void upd(int k, T x) {
        for(k++; k <= n; k += k & -k) {
            bit[k - 1] += x;
        }
    }
};

vector<int> link, sz, tour1, tour2;
vector<vector<int>> dsu_graph;

int find(int x) {
    return (x == link[x] ? x : link[x] = find(link[x]));
}

void unite(int x, int y) {
    x = find(x), y = find(y);
    if(x == y) return;
    if(sz[x] < sz[y]) swap(x, y);
    link[y] = x, sz[x] += sz[y], dsu_graph[x].push_back(y);
}

void dfs(int u) {
    tour1.push_back(u);
    for(int v: dsu_graph[u]) {
        dfs(v);
    }
}

vector<int> check_validity(int n, vector<int> x, vector<int> y, vector<int> s, vector<int> e, vector<int> l, vector<int> r) {
    int m = x.size(), q = s.size();
    link.resize(n);
    vector<int> pos1(n), pos2(n), num(q, 0), ans(q);
    vector<pair<int, int>> were(q), wolf(q);
    vector<vector<int>> g(n), rem(n), pts(n), add(n);
    FenwickTree<int> ft(n);
    for(int i = 0; i < m; i++) {
        g[x[i]].push_back(y[i]);
        g[y[i]].push_back(x[i]);
    }
    auto proc = [&](int start, int end, int step) { // wolf then were
        iota(link.begin(), link.end(), 0), sz.resize(n, 1), dsu_graph.resize(n);
        swap(tour1, tour2), swap(were, wolf);
        vector<vector<int>> handle(n);
        for(int i = 0; i < q; i++) {
            swap(s[i], e[i]), swap(l[i], r[i]);
            handle[l[i]].push_back(i);
        }
        for(int i = start; i != end; i += step) {
            for(int j: g[i]) {
                if(step * j < step * i) {
                    unite(i, j);
                }
            }
            for(int ind: handle[i]) {
                were[ind] = {find(s[ind]), sz[find(s[ind])]};
            }
        }
        dfs(find(0));
        sz.clear(), dsu_graph.clear();
    };
    proc(0, n, 1), proc(n - 1, -1, -1);
    for(int i = 0; i < n; i++) {
        pos1[tour1[i]] = i, pos2[tour2[i]] = i;
    }
    for(int i = 0; i < n; i++) {
        pts[pos1[tour2[i]]].push_back(i);
    }
    for(int i = 0; i < q; i++) {
        were[i] = {pos1[were[i].first], pos1[were[i].first] + were[i].second - 1};
        wolf[i] = {pos2[wolf[i].first], pos2[wolf[i].first] + wolf[i].second - 1};
        rem[were[i].first].push_back(i), add[were[i].second].push_back(i);
    }
    for(int i = 0; i < n; i++) {
        for(int ind: rem[i]) {
            num[ind] -= ft.qry(wolf[ind].first, wolf[ind].second);
        }
        for(int p: pts[i]) {
            ft.upd(p, 1);
        }
        for(int ind: add[i]) {
            num[ind] += ft.qry(wolf[ind].first, wolf[ind].second);
        }
    }
    for(int i = 0; i < q; i++) {
        ans[i] = num[i] > 0;
    }
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 5 ms 1244 KB Output is correct
11 Correct 5 ms 1236 KB Output is correct
12 Correct 7 ms 1340 KB Output is correct
13 Correct 5 ms 1336 KB Output is correct
14 Correct 5 ms 1244 KB Output is correct
15 Correct 5 ms 1364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 481 ms 69096 KB Output is correct
2 Correct 416 ms 68976 KB Output is correct
3 Correct 415 ms 68900 KB Output is correct
4 Correct 435 ms 69072 KB Output is correct
5 Correct 439 ms 69148 KB Output is correct
6 Correct 468 ms 68904 KB Output is correct
7 Correct 403 ms 68584 KB Output is correct
8 Correct 384 ms 68964 KB Output is correct
9 Correct 336 ms 68372 KB Output is correct
10 Correct 422 ms 69880 KB Output is correct
11 Correct 383 ms 69804 KB Output is correct
12 Correct 413 ms 68476 KB Output is correct
13 Correct 394 ms 68660 KB Output is correct
14 Correct 440 ms 68260 KB Output is correct
15 Correct 410 ms 68392 KB Output is correct
16 Correct 398 ms 68440 KB Output is correct
17 Correct 431 ms 68316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 5 ms 1244 KB Output is correct
11 Correct 5 ms 1236 KB Output is correct
12 Correct 7 ms 1340 KB Output is correct
13 Correct 5 ms 1336 KB Output is correct
14 Correct 5 ms 1244 KB Output is correct
15 Correct 5 ms 1364 KB Output is correct
16 Correct 481 ms 69096 KB Output is correct
17 Correct 416 ms 68976 KB Output is correct
18 Correct 415 ms 68900 KB Output is correct
19 Correct 435 ms 69072 KB Output is correct
20 Correct 439 ms 69148 KB Output is correct
21 Correct 468 ms 68904 KB Output is correct
22 Correct 403 ms 68584 KB Output is correct
23 Correct 384 ms 68964 KB Output is correct
24 Correct 336 ms 68372 KB Output is correct
25 Correct 422 ms 69880 KB Output is correct
26 Correct 383 ms 69804 KB Output is correct
27 Correct 413 ms 68476 KB Output is correct
28 Correct 394 ms 68660 KB Output is correct
29 Correct 440 ms 68260 KB Output is correct
30 Correct 410 ms 68392 KB Output is correct
31 Correct 398 ms 68440 KB Output is correct
32 Correct 431 ms 68316 KB Output is correct
33 Correct 483 ms 69212 KB Output is correct
34 Correct 256 ms 37292 KB Output is correct
35 Correct 499 ms 70028 KB Output is correct
36 Correct 513 ms 69104 KB Output is correct
37 Correct 466 ms 69636 KB Output is correct
38 Correct 516 ms 69560 KB Output is correct
39 Correct 482 ms 69940 KB Output is correct
40 Correct 526 ms 75616 KB Output is correct
41 Correct 482 ms 69260 KB Output is correct
42 Correct 409 ms 68532 KB Output is correct
43 Correct 546 ms 71900 KB Output is correct
44 Correct 444 ms 69348 KB Output is correct
45 Correct 412 ms 68052 KB Output is correct
46 Correct 429 ms 67776 KB Output is correct
47 Correct 395 ms 68656 KB Output is correct
48 Correct 410 ms 68484 KB Output is correct
49 Correct 402 ms 68584 KB Output is correct
50 Correct 432 ms 68556 KB Output is correct
51 Correct 483 ms 74712 KB Output is correct
52 Correct 498 ms 74648 KB Output is correct