답안 #401794

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
401794 2021-05-10T20:17:11 Z timmyfeng Collapse (JOI18_collapse) C++17
100 / 100
2760 ms 24280 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 100000, B = 128;

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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5324 KB Output is correct
2 Correct 6 ms 5196 KB Output is correct
3 Correct 8 ms 5196 KB Output is correct
4 Correct 9 ms 5196 KB Output is correct
5 Correct 14 ms 5392 KB Output is correct
6 Correct 23 ms 5836 KB Output is correct
7 Correct 6 ms 5264 KB Output is correct
8 Correct 7 ms 5196 KB Output is correct
9 Correct 16 ms 5476 KB Output is correct
10 Correct 27 ms 5580 KB Output is correct
11 Correct 33 ms 5956 KB Output is correct
12 Correct 30 ms 5908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 8600 KB Output is correct
2 Correct 70 ms 8596 KB Output is correct
3 Correct 237 ms 12436 KB Output is correct
4 Correct 129 ms 8592 KB Output is correct
5 Correct 344 ms 12448 KB Output is correct
6 Correct 205 ms 9340 KB Output is correct
7 Correct 648 ms 20496 KB Output is correct
8 Correct 391 ms 12748 KB Output is correct
9 Correct 52 ms 8588 KB Output is correct
10 Correct 92 ms 8664 KB Output is correct
11 Correct 273 ms 9164 KB Output is correct
12 Correct 486 ms 13992 KB Output is correct
13 Correct 675 ms 16832 KB Output is correct
14 Correct 882 ms 21700 KB Output is correct
15 Correct 731 ms 21428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 8588 KB Output is correct
2 Correct 78 ms 8520 KB Output is correct
3 Correct 105 ms 8528 KB Output is correct
4 Correct 131 ms 8596 KB Output is correct
5 Correct 273 ms 8260 KB Output is correct
6 Correct 255 ms 9332 KB Output is correct
7 Correct 994 ms 18768 KB Output is correct
8 Correct 1736 ms 21488 KB Output is correct
9 Correct 62 ms 8644 KB Output is correct
10 Correct 358 ms 9160 KB Output is correct
11 Correct 2146 ms 23700 KB Output is correct
12 Correct 2725 ms 24048 KB Output is correct
13 Correct 2161 ms 23824 KB Output is correct
14 Correct 2760 ms 24072 KB Output is correct
15 Correct 2090 ms 23724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5324 KB Output is correct
2 Correct 6 ms 5196 KB Output is correct
3 Correct 8 ms 5196 KB Output is correct
4 Correct 9 ms 5196 KB Output is correct
5 Correct 14 ms 5392 KB Output is correct
6 Correct 23 ms 5836 KB Output is correct
7 Correct 6 ms 5264 KB Output is correct
8 Correct 7 ms 5196 KB Output is correct
9 Correct 16 ms 5476 KB Output is correct
10 Correct 27 ms 5580 KB Output is correct
11 Correct 33 ms 5956 KB Output is correct
12 Correct 30 ms 5908 KB Output is correct
13 Correct 50 ms 8600 KB Output is correct
14 Correct 70 ms 8596 KB Output is correct
15 Correct 237 ms 12436 KB Output is correct
16 Correct 129 ms 8592 KB Output is correct
17 Correct 344 ms 12448 KB Output is correct
18 Correct 205 ms 9340 KB Output is correct
19 Correct 648 ms 20496 KB Output is correct
20 Correct 391 ms 12748 KB Output is correct
21 Correct 52 ms 8588 KB Output is correct
22 Correct 92 ms 8664 KB Output is correct
23 Correct 273 ms 9164 KB Output is correct
24 Correct 486 ms 13992 KB Output is correct
25 Correct 675 ms 16832 KB Output is correct
26 Correct 882 ms 21700 KB Output is correct
27 Correct 731 ms 21428 KB Output is correct
28 Correct 47 ms 8588 KB Output is correct
29 Correct 78 ms 8520 KB Output is correct
30 Correct 105 ms 8528 KB Output is correct
31 Correct 131 ms 8596 KB Output is correct
32 Correct 273 ms 8260 KB Output is correct
33 Correct 255 ms 9332 KB Output is correct
34 Correct 994 ms 18768 KB Output is correct
35 Correct 1736 ms 21488 KB Output is correct
36 Correct 62 ms 8644 KB Output is correct
37 Correct 358 ms 9160 KB Output is correct
38 Correct 2146 ms 23700 KB Output is correct
39 Correct 2725 ms 24048 KB Output is correct
40 Correct 2161 ms 23824 KB Output is correct
41 Correct 2760 ms 24072 KB Output is correct
42 Correct 2090 ms 23724 KB Output is correct
43 Correct 686 ms 13208 KB Output is correct
44 Correct 1498 ms 21264 KB Output is correct
45 Correct 771 ms 13276 KB Output is correct
46 Correct 1726 ms 21352 KB Output is correct
47 Correct 59 ms 8644 KB Output is correct
48 Correct 83 ms 8664 KB Output is correct
49 Correct 345 ms 9292 KB Output is correct
50 Correct 438 ms 10632 KB Output is correct
51 Correct 865 ms 14680 KB Output is correct
52 Correct 1392 ms 16936 KB Output is correct
53 Correct 1310 ms 16264 KB Output is correct
54 Correct 1740 ms 19168 KB Output is correct
55 Correct 1660 ms 18788 KB Output is correct
56 Correct 1804 ms 20520 KB Output is correct
57 Correct 2004 ms 22000 KB Output is correct
58 Correct 2400 ms 22504 KB Output is correct
59 Correct 2113 ms 23652 KB Output is correct
60 Correct 2701 ms 24280 KB Output is correct