Submission #401793

# Submission time Handle Problem Language Result Execution time Memory
401793 2021-05-10T20:16:02 Z timmyfeng Collapse (JOI18_collapse) C++17
100 / 100
2417 ms 24080 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 100000, B = 512;

vector<array<int, 3>> queries[N];
vector<int> adj[N];
bool visited[N];
int dsu[N];

int find(int u) {
    if (dsu[u] < 0) {
        return u;
    } else {
        dsu[u] = find(dsu[u]);
        return dsu[u];
    }
}

bool unite(int u, int v) {
    u = find(u), v = find(v);
    if (u == v) {
        return false;
    }

    if (-dsu[u] > -dsu[v]) {
        swap(u, v);
    }

    dsu[v] += dsu[u];
    dsu[u] = v;
    return true;
}

void dfs(int u) {
    visited[u] = true;
    for (auto c : adj[u]) {
        if (!visited[c]) {
            dfs(c);
        }
    }
}

vector<int> simulateCollapse(int n, vector<int> t, vector<int> x, vector<int> y, vector<int> w, vector<int> p) {
    int m = t.size(), q = w.size();
    vector<int> ans(q, n);

    for (int k = 0; k < 2; ++k) {
        map<array<int, 2>, int> in;
        vector<array<int, 4>> edges;
        for (int i = 0; i < m; ++i) {
            if (x[i] < y[i]) {
                swap(x[i], y[i]);
            }

            if (t[i] == 0) {
                in[{x[i], y[i]}] = i;
            } else {
                edges.push_back({x[i], y[i], in[{x[i], y[i]}], i});
                in.erase({x[i], y[i]});
            }
        }

        for (auto [e, i] : in) {
            edges.push_back({e[0], e[1], i, m});
        }

        sort(edges.begin(), edges.end());

        for (int i = 0; i < q; ++i) {
            queries[w[i] - w[i] % B].push_back({p[i], w[i], i});
        }

        for (int i = 0; i < m; i += B) {
            sort(queries[i].begin(), queries[i].end());

            vector<array<int, 4>> partial;
            fill(dsu, dsu + n, -1);
            int count = 0;

            int j = 0;
            for (auto [l, s, id] : queries[i]) {
                while (j < (int) edges.size() && edges[j][0] <= l) {
                    auto [u, v, a, b] = edges[j++];
                    if (a <= i && min(m, i + B) <= b) {
                        count += unite(u, v);
                    } else if (!(b <= i || min(m, i + B) <= a)) {
                        partial.push_back({u, v, a, b});
                    }
                }

                vector<int> nodes;
                for (auto [u, v, a, b] : partial) {
                    if (a <= s && s < b) {
                        u = find(u), v = find(v);
                        adj[u].push_back(v);
                        adj[v].push_back(u);
                        nodes.push_back(u);
                        nodes.push_back(v);
                    }
                }

                int res = 0;
                for (auto u : nodes) {
                    if (!visited[u]) {
                        --res;
                        ++ans[id];
                        dfs(u);
                    }
                }

                for (auto u : nodes) {
                    if (visited[u]) {
                        visited[u] = false;
                        adj[u].clear();
                        ++res;
                        --ans[id];
                    }
                }

                ans[id] -= count;
            }

            queries[i].clear();     
        }

        for (int i = 0; i < m; ++i) {
            x[i] = n - 1 - x[i];
            y[i] = n - 1 - y[i];
        }

        for (int i = 0; i < q; ++i) {
            p[i] = n - 2 - p[i];
        }
    }

    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5452 KB Output is correct
2 Correct 7 ms 5268 KB Output is correct
3 Correct 8 ms 5196 KB Output is correct
4 Correct 10 ms 5196 KB Output is correct
5 Correct 15 ms 5384 KB Output is correct
6 Correct 43 ms 5828 KB Output is correct
7 Correct 6 ms 5324 KB Output is correct
8 Correct 8 ms 5324 KB Output is correct
9 Correct 19 ms 5692 KB Output is correct
10 Correct 50 ms 5576 KB Output is correct
11 Correct 79 ms 6092 KB Output is correct
12 Correct 66 ms 6132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 8636 KB Output is correct
2 Correct 73 ms 8652 KB Output is correct
3 Correct 224 ms 12612 KB Output is correct
4 Correct 133 ms 8772 KB Output is correct
5 Correct 437 ms 12568 KB Output is correct
6 Correct 523 ms 9412 KB Output is correct
7 Correct 782 ms 20656 KB Output is correct
8 Correct 569 ms 12584 KB Output is correct
9 Correct 53 ms 8684 KB Output is correct
10 Correct 93 ms 8720 KB Output is correct
11 Correct 698 ms 9228 KB Output is correct
12 Correct 690 ms 14252 KB Output is correct
13 Correct 1002 ms 17124 KB Output is correct
14 Correct 1363 ms 21864 KB Output is correct
15 Correct 1048 ms 21452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 8708 KB Output is correct
2 Correct 81 ms 8676 KB Output is correct
3 Correct 105 ms 8672 KB Output is correct
4 Correct 138 ms 8768 KB Output is correct
5 Correct 710 ms 8696 KB Output is correct
6 Correct 666 ms 9400 KB Output is correct
7 Correct 974 ms 18752 KB Output is correct
8 Correct 1350 ms 21728 KB Output is correct
9 Correct 61 ms 8740 KB Output is correct
10 Correct 1266 ms 9304 KB Output is correct
11 Correct 1832 ms 23756 KB Output is correct
12 Correct 2324 ms 24080 KB Output is correct
13 Correct 1888 ms 23908 KB Output is correct
14 Correct 2396 ms 24068 KB Output is correct
15 Correct 1785 ms 23804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5452 KB Output is correct
2 Correct 7 ms 5268 KB Output is correct
3 Correct 8 ms 5196 KB Output is correct
4 Correct 10 ms 5196 KB Output is correct
5 Correct 15 ms 5384 KB Output is correct
6 Correct 43 ms 5828 KB Output is correct
7 Correct 6 ms 5324 KB Output is correct
8 Correct 8 ms 5324 KB Output is correct
9 Correct 19 ms 5692 KB Output is correct
10 Correct 50 ms 5576 KB Output is correct
11 Correct 79 ms 6092 KB Output is correct
12 Correct 66 ms 6132 KB Output is correct
13 Correct 53 ms 8636 KB Output is correct
14 Correct 73 ms 8652 KB Output is correct
15 Correct 224 ms 12612 KB Output is correct
16 Correct 133 ms 8772 KB Output is correct
17 Correct 437 ms 12568 KB Output is correct
18 Correct 523 ms 9412 KB Output is correct
19 Correct 782 ms 20656 KB Output is correct
20 Correct 569 ms 12584 KB Output is correct
21 Correct 53 ms 8684 KB Output is correct
22 Correct 93 ms 8720 KB Output is correct
23 Correct 698 ms 9228 KB Output is correct
24 Correct 690 ms 14252 KB Output is correct
25 Correct 1002 ms 17124 KB Output is correct
26 Correct 1363 ms 21864 KB Output is correct
27 Correct 1048 ms 21452 KB Output is correct
28 Correct 51 ms 8708 KB Output is correct
29 Correct 81 ms 8676 KB Output is correct
30 Correct 105 ms 8672 KB Output is correct
31 Correct 138 ms 8768 KB Output is correct
32 Correct 710 ms 8696 KB Output is correct
33 Correct 666 ms 9400 KB Output is correct
34 Correct 974 ms 18752 KB Output is correct
35 Correct 1350 ms 21728 KB Output is correct
36 Correct 61 ms 8740 KB Output is correct
37 Correct 1266 ms 9304 KB Output is correct
38 Correct 1832 ms 23756 KB Output is correct
39 Correct 2324 ms 24080 KB Output is correct
40 Correct 1888 ms 23908 KB Output is correct
41 Correct 2396 ms 24068 KB Output is correct
42 Correct 1785 ms 23804 KB Output is correct
43 Correct 578 ms 13368 KB Output is correct
44 Correct 1148 ms 21496 KB Output is correct
45 Correct 833 ms 13276 KB Output is correct
46 Correct 1354 ms 21600 KB Output is correct
47 Correct 62 ms 8760 KB Output is correct
48 Correct 85 ms 8816 KB Output is correct
49 Correct 903 ms 9256 KB Output is correct
50 Correct 1359 ms 10728 KB Output is correct
51 Correct 967 ms 14740 KB Output is correct
52 Correct 1893 ms 16872 KB Output is correct
53 Correct 1584 ms 16392 KB Output is correct
54 Correct 2039 ms 19556 KB Output is correct
55 Correct 1708 ms 18780 KB Output is correct
56 Correct 1813 ms 20600 KB Output is correct
57 Correct 1821 ms 22060 KB Output is correct
58 Correct 2293 ms 22760 KB Output is correct
59 Correct 1844 ms 23984 KB Output is correct
60 Correct 2417 ms 24040 KB Output is correct