답안 #679455

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
679455 2023-01-08T09:59:46 Z qwerasdfzxcl Collapse (JOI18_collapse) C++17
100 / 100
4273 ms 35004 KB
#include "collapse.h"
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
constexpr int B = 502;
struct DSU{
    int path[100100], sz[100100], cnt;
    vector<pair<int, int>> mylog;
    void init(int n){for (int i=1;i<=n;i++){path[i] = i; sz[i] = 1;} mylog.clear(); cnt = 0;}
    int find(int s){
        if (s==path[s]) return s;
        return find(path[s]);
    }
    bool merge(int x, int y){
        //printf("merge: %d %d\n", x, y);
        x = find(x), y = find(y);
        if (x==y) return 0;
        cnt++;
        if (sz[x] > sz[y]) swap(x, y);

        path[x] = y;
        sz[y] += sz[x];
        mylog.emplace_back(x, sz[x]);
        return 1;
    }
    void rollback(int c){
        while(c--){
            auto [x, _sz] = mylog.back(); mylog.pop_back();
            sz[path[x]] -= _sz;
            path[x] = x;
            --cnt;
        }
    }
    int count(int x){return x - cnt;}
}dsu;

struct Edge{
    int x, y;
    Edge(){}
    Edge(int _x, int _y): x(_x), y(_y) {}
}E[200200];

struct Query{
    int w, p, i;
    Query(){}
    Query(int _w, int _p, int _i): w(_w), p(_p), i(_i) {}
};

vector<Query> bucket[200200];
map<int, int> mp[200200];
int idx[200200], updated[200200], on[200200], ans[200200];

bool cmpB(const Query &x, const Query &y){return x.p < y.p;}
bool cmpE(int x, int y){return E[x].y < E[y].y;}

void calc(int n, vector<int> T, vector<int> X, vector<int> Y, vector<int> W, vector<int> P){
    int c = T.size(), q = W.size();

    ///init
    for (int i=1;i<=c;i++){
        mp[i].clear();
        on[i] = 0;
        updated[i] = 0;
    }
    for (int i=0;i<=c/B;i++) bucket[i].clear();
    fill(E, E+c+1, Edge(0, 0));
    ///

    int Ecnt = 0;
    for (int i=1;i<=c;i++){
        int op, x, y;
        op = T[i-1], x = X[i-1], y = Y[i-1];

        if (x==y) {idx[i] = 0; continue;}
        if (x > y) swap(x, y);
        if (mp[x].find(y)==mp[x].end()){
            mp[x][y] = ++Ecnt;
            E[Ecnt] = Edge(x, y);
        }

        idx[i] = mp[x][y];
    }

    for (int i=1;i<=q;i++){
        int w, p;
        w = W[i-1], p = P[i-1];
        bucket[w/B].emplace_back(w, p, i);
    }

    vector<int> onE;

    for (int z=0;z<=c/B;z++){
        int l = z*B, r = (z+1)*B;
        for (int i=l;i<r;i++) if (idx[i]) updated[idx[i]] = 1;

        sort(bucket[z].begin(), bucket[z].end(), cmpB);
        dsu.init(n);
        int pt = 0;

        for (auto &[w, p, qi]:bucket[z]){
            while(pt < (int)onE.size() && E[onE[pt]].y <= p){
                if (!updated[onE[pt]]) dsu.merge(E[onE[pt]].x, E[onE[pt]].y);
                pt++;
            }

            for (int i=l;i<=w;i++) if (idx[i]) on[idx[i]] ^= 1;

            int cnt = 0;
            for (int i=l;i<r;i++) if (idx[i] && on[idx[i]] && E[idx[i]].y <= p){
                if (dsu.merge(E[idx[i]].x, E[idx[i]].y)) cnt++;
            }

            //printf("ok %d -> %d\n", qi, dsu.count(p));
            ans[qi] += dsu.count(p);
            dsu.rollback(cnt);

            for (int i=l;i<=w;i++) if (idx[i]) on[idx[i]] ^= 1;
        }

        for (int i=l;i<r;i++) if (idx[i]) on[idx[i]] ^= 1, updated[idx[i]] = 0;

        vector<int> nE;
        for (auto &x:onE) if (on[x]) nE.push_back(x);
        for (int i=l;i<r;i++) if (on[idx[i]]) nE.push_back(idx[i]);

        sort(nE.begin(), nE.end(), cmpE);
        nE.erase(unique(nE.begin(), nE.end()), nE.end());
        swap(nE, onE);
    }
}

std::vector<int> simulateCollapse(
	int N,
	std::vector<int> T,
	std::vector<int> X,
	std::vector<int> Y,
	std::vector<int> W,
	std::vector<int> P
) {
    for (auto &x:X) ++x;
    for (auto &x:Y) ++x;
    for (auto &x:W) ++x;
    for (auto &x:P) ++x;

    calc(N, T, X, Y, W, P);
    for (auto &x:X) x = N+1 - x;
    for (auto &y:Y) y = N+1 - y;
    for (auto &p:P) p = N - p;
    calc(N, T, X, Y, W, P);

    vector<int> ret(W.size());
    for (int i=0;i<(int)W.size();i++) ret[i] = ans[i+1];

	return ret;
}

Compilation message

collapse.cpp: In function 'void calc(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
collapse.cpp:72:13: warning: variable 'op' set but not used [-Wunused-but-set-variable]
   72 |         int op, x, y;
      |             ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 14912 KB Output is correct
2 Correct 14 ms 14676 KB Output is correct
3 Correct 17 ms 14740 KB Output is correct
4 Correct 17 ms 14740 KB Output is correct
5 Correct 49 ms 14936 KB Output is correct
6 Correct 42 ms 15356 KB Output is correct
7 Correct 14 ms 14804 KB Output is correct
8 Correct 16 ms 14776 KB Output is correct
9 Correct 34 ms 15060 KB Output is correct
10 Correct 53 ms 15124 KB Output is correct
11 Correct 50 ms 15312 KB Output is correct
12 Correct 47 ms 15356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 137 ms 18468 KB Output is correct
2 Correct 157 ms 18456 KB Output is correct
3 Correct 887 ms 24068 KB Output is correct
4 Correct 184 ms 18956 KB Output is correct
5 Correct 743 ms 26036 KB Output is correct
6 Correct 485 ms 20292 KB Output is correct
7 Correct 2706 ms 32696 KB Output is correct
8 Correct 725 ms 28756 KB Output is correct
9 Correct 142 ms 19944 KB Output is correct
10 Correct 157 ms 20120 KB Output is correct
11 Correct 613 ms 20512 KB Output is correct
12 Correct 781 ms 29732 KB Output is correct
13 Correct 1529 ms 31724 KB Output is correct
14 Correct 3246 ms 33860 KB Output is correct
15 Correct 2716 ms 34360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 139 ms 18464 KB Output is correct
2 Correct 154 ms 18492 KB Output is correct
3 Correct 169 ms 18492 KB Output is correct
4 Correct 183 ms 18492 KB Output is correct
5 Correct 577 ms 18540 KB Output is correct
6 Correct 505 ms 19412 KB Output is correct
7 Correct 1692 ms 28488 KB Output is correct
8 Correct 3002 ms 31236 KB Output is correct
9 Correct 155 ms 20032 KB Output is correct
10 Correct 758 ms 20312 KB Output is correct
11 Correct 3368 ms 34612 KB Output is correct
12 Correct 4130 ms 34664 KB Output is correct
13 Correct 3530 ms 35000 KB Output is correct
14 Correct 4100 ms 34616 KB Output is correct
15 Correct 3400 ms 34780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 14912 KB Output is correct
2 Correct 14 ms 14676 KB Output is correct
3 Correct 17 ms 14740 KB Output is correct
4 Correct 17 ms 14740 KB Output is correct
5 Correct 49 ms 14936 KB Output is correct
6 Correct 42 ms 15356 KB Output is correct
7 Correct 14 ms 14804 KB Output is correct
8 Correct 16 ms 14776 KB Output is correct
9 Correct 34 ms 15060 KB Output is correct
10 Correct 53 ms 15124 KB Output is correct
11 Correct 50 ms 15312 KB Output is correct
12 Correct 47 ms 15356 KB Output is correct
13 Correct 137 ms 18468 KB Output is correct
14 Correct 157 ms 18456 KB Output is correct
15 Correct 887 ms 24068 KB Output is correct
16 Correct 184 ms 18956 KB Output is correct
17 Correct 743 ms 26036 KB Output is correct
18 Correct 485 ms 20292 KB Output is correct
19 Correct 2706 ms 32696 KB Output is correct
20 Correct 725 ms 28756 KB Output is correct
21 Correct 142 ms 19944 KB Output is correct
22 Correct 157 ms 20120 KB Output is correct
23 Correct 613 ms 20512 KB Output is correct
24 Correct 781 ms 29732 KB Output is correct
25 Correct 1529 ms 31724 KB Output is correct
26 Correct 3246 ms 33860 KB Output is correct
27 Correct 2716 ms 34360 KB Output is correct
28 Correct 139 ms 18464 KB Output is correct
29 Correct 154 ms 18492 KB Output is correct
30 Correct 169 ms 18492 KB Output is correct
31 Correct 183 ms 18492 KB Output is correct
32 Correct 577 ms 18540 KB Output is correct
33 Correct 505 ms 19412 KB Output is correct
34 Correct 1692 ms 28488 KB Output is correct
35 Correct 3002 ms 31236 KB Output is correct
36 Correct 155 ms 20032 KB Output is correct
37 Correct 758 ms 20312 KB Output is correct
38 Correct 3368 ms 34612 KB Output is correct
39 Correct 4130 ms 34664 KB Output is correct
40 Correct 3530 ms 35000 KB Output is correct
41 Correct 4100 ms 34616 KB Output is correct
42 Correct 3400 ms 34780 KB Output is correct
43 Correct 719 ms 28652 KB Output is correct
44 Correct 2731 ms 32760 KB Output is correct
45 Correct 807 ms 29268 KB Output is correct
46 Correct 3004 ms 33264 KB Output is correct
47 Correct 152 ms 20032 KB Output is correct
48 Correct 169 ms 20048 KB Output is correct
49 Correct 701 ms 20524 KB Output is correct
50 Correct 863 ms 22256 KB Output is correct
51 Correct 919 ms 30060 KB Output is correct
52 Correct 1482 ms 31176 KB Output is correct
53 Correct 1351 ms 30884 KB Output is correct
54 Correct 2080 ms 32304 KB Output is correct
55 Correct 1849 ms 31996 KB Output is correct
56 Correct 2373 ms 32672 KB Output is correct
57 Correct 2950 ms 34044 KB Output is correct
58 Correct 3456 ms 34196 KB Output is correct
59 Correct 3444 ms 35004 KB Output is correct
60 Correct 4273 ms 34720 KB Output is correct