Submission #374934

# Submission time Handle Problem Language Result Execution time Memory
374934 2021-03-08T15:10:03 Z valerikk Collapse (JOI18_collapse) C++17
100 / 100
8719 ms 29804 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 7;

int dsu[N], sz[N];
int comps;
vector<int> g[N];
bool vis[N];
int added;

void dfs(int v) {
    vis[v] = 1;
    for (int u : g[v]) {
        if (!vis[u]) {
            ++added;
            dfs(u);
        }
    }
}

int get(int v) {
    return dsu[v] == v ? v : dsu[v] = get(dsu[v]);
}

void unite(int v, int u) {
    v = get(v);
    u = get(u);
    if (v == u) return;
    --comps;
    if (sz[v] > sz[u]) swap(v, u);
    sz[u] += sz[v];
    dsu[v] = u;
}

void init(int n) {
    for (int i = 0; i < n; ++i) {
        dsu[i] = i;
        sz[i] = 1;
    }
    comps = n;
}

vector<int> solve(int n, vector<int> t, vector<int> x, vector<int> y, vector<int> w, vector<int> p) {
    int c = t.size(), q = w.size();
    for (int i = 0; i < c; ++i) {
        if (x[i] > y[i]) swap(x[i], y[i]);
    }
    vector<int> when(c, c), who(c, -1);
    map<pair<int, int>, int> lst;
    for (int i = 0; i < c; ++i) {
        if (t[i] == 0) {
            lst[{x[i], y[i]}] = i;
        } else {
            who[i] = lst[{x[i], y[i]}];
            when[lst[{x[i], y[i]}]] = i;
        }
    }
    int sq = sqrt(c);
    vector<int> d(q, 0);
    for (int l = 0; l < c; l += sq) {
        int r = min(c, l + sq);
        vector<int> here;
        vector<vector<int>> by_y(n + 1), by_p(n + 1);
        for (int i = 0; i < l; ++i) {
            if (t[i] == 1) continue;
            if (when[i] >= r) {
                by_y[y[i]].push_back(i);
            }
        }
        for (int i = l; i < r; ++i) {
            if (t[i] == 0) here.push_back(i); else here.push_back(who[i]);
        }
        sort(here.begin(), here.end());
        here.erase(unique(here.begin(), here.end()), here.end());
        for (int i = 0; i < q; ++i) {
            if (w[i] >= l && w[i] < r) by_p[p[i]].push_back(i);
        }
        init(n);
        for (int i = 0; i <= n; ++i) {
            for (int id : by_p[i]) {
                d[id] = comps - n + i;
                vector<pair<int, int>> edges;
                for (int edge_id : here) {
                    if (edge_id <= w[id] && w[id] < when[edge_id] && y[edge_id] < i) edges.push_back({get(x[edge_id]), get(y[edge_id])});
                }
                for (auto [u, v] : edges) {
                    g[u] = g[v] = {};
                    vis[u] = vis[v] = 0;
                }
                for (auto [u, v] : edges) {
                    g[u].push_back(v);
                    g[v].push_back(u);
                }
                added = 0;
                for (auto [u, v] : edges) {
                    if (!vis[u]) dfs(u);
                    if (!vis[v]) dfs(v);
                }
                d[id] -= added;
            }
            for (int edge_id : by_y[i]) unite(x[edge_id], y[edge_id]);
        }
    }
    return d;
}

vector<int> simulateCollapse(int n, vector<int> t, vector<int> x, vector<int> y, vector<int> w, vector<int> p) {
    int c = t.size(), q = w.size();
    for (int i = 0; i < q; ++i) ++p[i];
    auto d1 = solve(n, t, x, y, w, p);
    for (int i = 0; i < c; ++i) {
        x[i] = n - x[i] - 1;
        y[i] = n - y[i] - 1;
    }
    for (int i = 0; i < q; ++i) p[i] = n - p[i];
    auto d2 = solve(n, t, x, y, w, p);
    vector<int> d(q);
    for (int i = 0; i < q; ++i) d[i] = d1[i] + d2[i];
    return d;
}

#ifndef EVAL
int main() {
    freopen("input.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, c, q;
    cin >> n >> c >> q;
    vector<int> t(c), x(c), y(c);
    for (int i = 0; i < c; ++i) cin >> t[i] >> x[i] >> y[i];
    vector<int> w(q), p(q);
    for (int i = 0; i < q; ++i) cin >> w[i] >> p[i];
    auto d = simulateCollapse(n, t, x, y, w, p);
    for (int i = 0; i < q; ++i) cout << d[i] << '\n';
    return 0;
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 12 ms 3180 KB Output is correct
2 Correct 4 ms 2924 KB Output is correct
3 Correct 5 ms 2924 KB Output is correct
4 Correct 5 ms 2924 KB Output is correct
5 Correct 17 ms 3180 KB Output is correct
6 Correct 33 ms 3564 KB Output is correct
7 Correct 5 ms 3308 KB Output is correct
8 Correct 6 ms 3308 KB Output is correct
9 Correct 28 ms 3820 KB Output is correct
10 Correct 40 ms 3820 KB Output is correct
11 Correct 72 ms 4076 KB Output is correct
12 Correct 61 ms 3948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 6732 KB Output is correct
2 Correct 45 ms 6516 KB Output is correct
3 Correct 907 ms 11700 KB Output is correct
4 Correct 59 ms 6364 KB Output is correct
5 Correct 1154 ms 12340 KB Output is correct
6 Correct 257 ms 7260 KB Output is correct
7 Correct 2315 ms 19384 KB Output is correct
8 Correct 1289 ms 16384 KB Output is correct
9 Correct 52 ms 13084 KB Output is correct
10 Correct 69 ms 13360 KB Output is correct
11 Correct 243 ms 13700 KB Output is correct
12 Correct 2640 ms 24488 KB Output is correct
13 Correct 4678 ms 25576 KB Output is correct
14 Correct 8719 ms 27680 KB Output is correct
15 Correct 6491 ms 28044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 6732 KB Output is correct
2 Correct 58 ms 6344 KB Output is correct
3 Correct 57 ms 6504 KB Output is correct
4 Correct 63 ms 6364 KB Output is correct
5 Correct 123 ms 6620 KB Output is correct
6 Correct 279 ms 7260 KB Output is correct
7 Correct 1859 ms 16292 KB Output is correct
8 Correct 3773 ms 20772 KB Output is correct
9 Correct 90 ms 15244 KB Output is correct
10 Correct 277 ms 13936 KB Output is correct
11 Correct 6355 ms 29092 KB Output is correct
12 Correct 8346 ms 29488 KB Output is correct
13 Correct 6507 ms 28916 KB Output is correct
14 Correct 8325 ms 29804 KB Output is correct
15 Correct 6307 ms 28992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 3180 KB Output is correct
2 Correct 4 ms 2924 KB Output is correct
3 Correct 5 ms 2924 KB Output is correct
4 Correct 5 ms 2924 KB Output is correct
5 Correct 17 ms 3180 KB Output is correct
6 Correct 33 ms 3564 KB Output is correct
7 Correct 5 ms 3308 KB Output is correct
8 Correct 6 ms 3308 KB Output is correct
9 Correct 28 ms 3820 KB Output is correct
10 Correct 40 ms 3820 KB Output is correct
11 Correct 72 ms 4076 KB Output is correct
12 Correct 61 ms 3948 KB Output is correct
13 Correct 35 ms 6732 KB Output is correct
14 Correct 45 ms 6516 KB Output is correct
15 Correct 907 ms 11700 KB Output is correct
16 Correct 59 ms 6364 KB Output is correct
17 Correct 1154 ms 12340 KB Output is correct
18 Correct 257 ms 7260 KB Output is correct
19 Correct 2315 ms 19384 KB Output is correct
20 Correct 1289 ms 16384 KB Output is correct
21 Correct 52 ms 13084 KB Output is correct
22 Correct 69 ms 13360 KB Output is correct
23 Correct 243 ms 13700 KB Output is correct
24 Correct 2640 ms 24488 KB Output is correct
25 Correct 4678 ms 25576 KB Output is correct
26 Correct 8719 ms 27680 KB Output is correct
27 Correct 6491 ms 28044 KB Output is correct
28 Correct 35 ms 6732 KB Output is correct
29 Correct 58 ms 6344 KB Output is correct
30 Correct 57 ms 6504 KB Output is correct
31 Correct 63 ms 6364 KB Output is correct
32 Correct 123 ms 6620 KB Output is correct
33 Correct 279 ms 7260 KB Output is correct
34 Correct 1859 ms 16292 KB Output is correct
35 Correct 3773 ms 20772 KB Output is correct
36 Correct 90 ms 15244 KB Output is correct
37 Correct 277 ms 13936 KB Output is correct
38 Correct 6355 ms 29092 KB Output is correct
39 Correct 8346 ms 29488 KB Output is correct
40 Correct 6507 ms 28916 KB Output is correct
41 Correct 8325 ms 29804 KB Output is correct
42 Correct 6307 ms 28992 KB Output is correct
43 Correct 1182 ms 15156 KB Output is correct
44 Correct 2494 ms 19420 KB Output is correct
45 Correct 1413 ms 16472 KB Output is correct
46 Correct 3822 ms 20712 KB Output is correct
47 Correct 85 ms 15244 KB Output is correct
48 Correct 103 ms 13988 KB Output is correct
49 Correct 301 ms 13956 KB Output is correct
50 Correct 989 ms 15876 KB Output is correct
51 Correct 2883 ms 24816 KB Output is correct
52 Correct 4660 ms 26736 KB Output is correct
53 Correct 4180 ms 25708 KB Output is correct
54 Correct 5366 ms 26744 KB Output is correct
55 Correct 4961 ms 26864 KB Output is correct
56 Correct 5099 ms 27288 KB Output is correct
57 Correct 5783 ms 27956 KB Output is correct
58 Correct 7275 ms 28632 KB Output is correct
59 Correct 6480 ms 29444 KB Output is correct
60 Correct 8478 ms 29652 KB Output is correct