Submission #425001

# Submission time Handle Problem Language Result Execution time Memory
425001 2021-06-12T12:20:00 Z CodePlatina Collapse (JOI18_collapse) C++17
100 / 100
7866 ms 420980 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <map>
#include <set>
#include <cstdlib>
#include <unordered_set>
#define pii pair<int, int>
#define piii pair<int, pii>
#define pll pair<long long, long long>
#define plll pair<long long, pll>
#define tiii tuple<int, int, int>
#define tiiii tuple<int, int, int, int>
#define ff first
#define ss second
#define ee ss.ff
#define rr ss.ss
#define DEBUG
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
const int INF = (int)1e9 + 7;
const int Q = 320;

using namespace std;

vector<pii> eg;
vector<pii> lf;
map<pii, int> M;
vector<int> lsu[101010];
vector<int> lsd[101010];
vector<pii> qr[101010];

struct UF
{
    int par[101010];
    short rnk[101010];
    int pnt;
    int cnt, __cnt;
    vector<pii> rec;

    UF(void) : pnt(0), cnt(0), __cnt(0), rec() { for(int i = 0; i < 101010; ++i) par[i] = i, rnk[i] = 0; }
    int fnd(int x) { return x == par[x] ? x : fnd(par[x]); }
    void uni(int x, int y)
    {
        x = fnd(x), y = fnd(y);
        if(x == y) return;
        if(rnk[x] == rnk[y])
        {
            rec.push_back({0, x});
            rec.push_back({1, y});
            ++rnk[x];
            par[y] = x;
        }
        else if(rnk[x] > rnk[y])
        {
            rec.push_back({1, y});
            par[y] = x;
        }
        else
        {
            rec.push_back({1, x});
            par[x] = y;
        }
        ++cnt;
    }
    void save(void) { pnt = (int)rec.size(); __cnt = cnt; }
    void rollback(void)
    {
        while((int)rec.size() > pnt)
        {
            auto [c, x] = rec.back();
            if(c == 0) --rnk[x];
            else par[x] = x;
            rec.pop_back();
        }
        cnt = __cnt;
    }
}uf[320];

vector<int> ind[320];

vector<int> simulateCollapse(int N, vector<int> C, vector<int> X, vector<int> Y, vector<int> W, vector<int>P)
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n = N, m = C.size(), T = W.size();
    for(int i = 0; i < m; ++i)
    {
        int c = C[i], x = X[i], y = Y[i];
        if(x > y) swap(x, y);

        if(c == 0)
        {
            M[{x, y}] = (int)eg.size();
            eg.push_back({x, y});
            lf.push_back({i, m});
        }
        else
        {
            lf[M[{x, y}]].ss = i;
            M.erase({x, y});
        }
    }
    for(int i = 0; i < (int)eg.size(); ++i)
    {
        if(eg[i].ff > 0) lsd[eg[i].ff - 1].push_back(i);
        lsu[eg[i].ss].push_back(i);
    }

    for(int i = 0; i < T; ++i)
    {
        int x = W[i], y = P[i];
        qr[y].push_back({x, i});
    }

    vector<int> ans(T); for(auto &i : ans) i = n;
    for(int i = 0; i < n; ++i)
    {
        for(int k = 0; k < (int)lsu[i].size(); ++k)
        {
            auto [x, y] = eg[lsu[i][k]];
            auto [s, e] = lf[lsu[i][k]];
            for(int j = s / Q; j <= (e - 1) / Q; ++j)
            {
                int l = j * Q, r = l + Q;
                if(s <= l && r <= e) uf[j].uni(x, y);
                else ind[j].push_back(lsu[i][k]);
            }
        }

        for(auto [t, q] : qr[i])
        {
            int j = t / Q;
            uf[j].save();
            for(int k = 0; k < (int)ind[j].size(); ++k)
            {
                auto [x, y] = eg[ind[j][k]];
                auto [s, e] = lf[ind[j][k]];
                if(s <= t && t < e) uf[j].uni(x, y);
            }
            ans[q] -= uf[j].cnt;
            uf[j].rollback();
        }
    }

    for(int i = 0; i < 320; ++i)
    {
        uf[i].pnt = uf[i].cnt = uf[i].__cnt = 0;
        uf[i].rollback();
        ind[i].clear();
        ind[i].shrink_to_fit();
    }

    for(int i = n - 1; i >= 0; --i)
    {
        for(int k = 0; k < (int)lsd[i].size(); ++k)
        {
            auto [x, y] = eg[lsd[i][k]];
            auto [s, e] = lf[lsd[i][k]];
            for(int j = s / Q; j <= (e - 1) / Q; ++j)
            {
                int l = j * Q, r = l + Q;
                if(s <= l && r <= e) uf[j].uni(x, y);
                else ind[j].push_back(lsd[i][k]);
            }
        }

        for(auto [t, q] : qr[i])
        {
            int j = t / Q;
            uf[j].save();
            for(int k = 0; k < (int)ind[j].size(); ++k)
            {
                auto [x, y] = eg[ind[j][k]];
                auto [s, e] = lf[ind[j][k]];
                if(s <= t && t < e) uf[j].uni(x, y);
            }
            ans[q] -= uf[j].cnt;
            uf[j].rollback();
        }
    }

	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 91 ms 197612 KB Output is correct
2 Correct 92 ms 197408 KB Output is correct
3 Correct 103 ms 197364 KB Output is correct
4 Correct 94 ms 197444 KB Output is correct
5 Correct 99 ms 197648 KB Output is correct
6 Correct 110 ms 198084 KB Output is correct
7 Correct 92 ms 197444 KB Output is correct
8 Correct 92 ms 197460 KB Output is correct
9 Correct 99 ms 197772 KB Output is correct
10 Correct 118 ms 197956 KB Output is correct
11 Correct 148 ms 198940 KB Output is correct
12 Correct 133 ms 198664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 132 ms 200256 KB Output is correct
2 Correct 124 ms 200296 KB Output is correct
3 Correct 259 ms 205172 KB Output is correct
4 Correct 153 ms 200328 KB Output is correct
5 Correct 463 ms 206192 KB Output is correct
6 Correct 353 ms 201324 KB Output is correct
7 Correct 831 ms 217732 KB Output is correct
8 Correct 576 ms 207508 KB Output is correct
9 Correct 130 ms 200740 KB Output is correct
10 Correct 143 ms 200640 KB Output is correct
11 Correct 432 ms 201164 KB Output is correct
12 Correct 770 ms 209008 KB Output is correct
13 Correct 2338 ms 278900 KB Output is correct
14 Correct 6233 ms 417716 KB Output is correct
15 Correct 4523 ms 392632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 122 ms 200240 KB Output is correct
2 Correct 128 ms 200584 KB Output is correct
3 Correct 143 ms 200700 KB Output is correct
4 Correct 149 ms 200792 KB Output is correct
5 Correct 406 ms 200816 KB Output is correct
6 Correct 360 ms 201812 KB Output is correct
7 Correct 722 ms 214068 KB Output is correct
8 Correct 2795 ms 250712 KB Output is correct
9 Correct 149 ms 202948 KB Output is correct
10 Correct 618 ms 202440 KB Output is correct
11 Correct 4872 ms 368920 KB Output is correct
12 Correct 7404 ms 420980 KB Output is correct
13 Correct 5300 ms 358484 KB Output is correct
14 Correct 7218 ms 419692 KB Output is correct
15 Correct 5350 ms 365636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 197612 KB Output is correct
2 Correct 92 ms 197408 KB Output is correct
3 Correct 103 ms 197364 KB Output is correct
4 Correct 94 ms 197444 KB Output is correct
5 Correct 99 ms 197648 KB Output is correct
6 Correct 110 ms 198084 KB Output is correct
7 Correct 92 ms 197444 KB Output is correct
8 Correct 92 ms 197460 KB Output is correct
9 Correct 99 ms 197772 KB Output is correct
10 Correct 118 ms 197956 KB Output is correct
11 Correct 148 ms 198940 KB Output is correct
12 Correct 133 ms 198664 KB Output is correct
13 Correct 132 ms 200256 KB Output is correct
14 Correct 124 ms 200296 KB Output is correct
15 Correct 259 ms 205172 KB Output is correct
16 Correct 153 ms 200328 KB Output is correct
17 Correct 463 ms 206192 KB Output is correct
18 Correct 353 ms 201324 KB Output is correct
19 Correct 831 ms 217732 KB Output is correct
20 Correct 576 ms 207508 KB Output is correct
21 Correct 130 ms 200740 KB Output is correct
22 Correct 143 ms 200640 KB Output is correct
23 Correct 432 ms 201164 KB Output is correct
24 Correct 770 ms 209008 KB Output is correct
25 Correct 2338 ms 278900 KB Output is correct
26 Correct 6233 ms 417716 KB Output is correct
27 Correct 4523 ms 392632 KB Output is correct
28 Correct 122 ms 200240 KB Output is correct
29 Correct 128 ms 200584 KB Output is correct
30 Correct 143 ms 200700 KB Output is correct
31 Correct 149 ms 200792 KB Output is correct
32 Correct 406 ms 200816 KB Output is correct
33 Correct 360 ms 201812 KB Output is correct
34 Correct 722 ms 214068 KB Output is correct
35 Correct 2795 ms 250712 KB Output is correct
36 Correct 149 ms 202948 KB Output is correct
37 Correct 618 ms 202440 KB Output is correct
38 Correct 4872 ms 368920 KB Output is correct
39 Correct 7404 ms 420980 KB Output is correct
40 Correct 5300 ms 358484 KB Output is correct
41 Correct 7218 ms 419692 KB Output is correct
42 Correct 5350 ms 365636 KB Output is correct
43 Correct 442 ms 206844 KB Output is correct
44 Correct 946 ms 218076 KB Output is correct
45 Correct 825 ms 208444 KB Output is correct
46 Correct 2826 ms 249976 KB Output is correct
47 Correct 163 ms 203144 KB Output is correct
48 Correct 160 ms 202044 KB Output is correct
49 Correct 544 ms 202600 KB Output is correct
50 Correct 1209 ms 207228 KB Output is correct
51 Correct 1006 ms 210108 KB Output is correct
52 Correct 2817 ms 266312 KB Output is correct
53 Correct 2181 ms 249640 KB Output is correct
54 Correct 4027 ms 311376 KB Output is correct
55 Correct 3131 ms 284240 KB Output is correct
56 Correct 3990 ms 308076 KB Output is correct
57 Correct 4771 ms 332708 KB Output is correct
58 Correct 6934 ms 398124 KB Output is correct
59 Correct 5387 ms 366512 KB Output is correct
60 Correct 7866 ms 420380 KB Output is correct