답안 #679458

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
679458 2023-01-08T10:02:05 Z qwerasdfzxcl Collapse (JOI18_collapse) C++17
100 / 100
3218 ms 26612 KB
#include "collapse.h"
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
constexpr int B = 1001;
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[110000];
map<int, int> mp[110000];
int idx[110000], updated[110000], on[110000], ans[110000];

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 46 ms 8532 KB Output is correct
2 Correct 13 ms 8396 KB Output is correct
3 Correct 15 ms 8328 KB Output is correct
4 Correct 15 ms 8404 KB Output is correct
5 Correct 82 ms 8660 KB Output is correct
6 Correct 62 ms 8908 KB Output is correct
7 Correct 13 ms 8404 KB Output is correct
8 Correct 14 ms 8404 KB Output is correct
9 Correct 48 ms 8788 KB Output is correct
10 Correct 87 ms 8880 KB Output is correct
11 Correct 79 ms 9032 KB Output is correct
12 Correct 73 ms 9040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 175 ms 12436 KB Output is correct
2 Correct 200 ms 12536 KB Output is correct
3 Correct 1750 ms 19088 KB Output is correct
4 Correct 214 ms 12108 KB Output is correct
5 Correct 1340 ms 18016 KB Output is correct
6 Correct 975 ms 12704 KB Output is correct
7 Correct 2037 ms 23896 KB Output is correct
8 Correct 1250 ms 20204 KB Output is correct
9 Correct 180 ms 12796 KB Output is correct
10 Correct 192 ms 12876 KB Output is correct
11 Correct 1109 ms 13284 KB Output is correct
12 Correct 1245 ms 20884 KB Output is correct
13 Correct 1913 ms 22584 KB Output is correct
14 Correct 2636 ms 24944 KB Output is correct
15 Correct 2187 ms 25444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 180 ms 12512 KB Output is correct
2 Correct 192 ms 12520 KB Output is correct
3 Correct 201 ms 12568 KB Output is correct
4 Correct 223 ms 12600 KB Output is correct
5 Correct 645 ms 12840 KB Output is correct
6 Correct 1008 ms 13448 KB Output is correct
7 Correct 1696 ms 23564 KB Output is correct
8 Correct 2502 ms 26612 KB Output is correct
9 Correct 203 ms 12872 KB Output is correct
10 Correct 1476 ms 13008 KB Output is correct
11 Correct 2574 ms 25648 KB Output is correct
12 Correct 3208 ms 25596 KB Output is correct
13 Correct 2712 ms 25560 KB Output is correct
14 Correct 3195 ms 25608 KB Output is correct
15 Correct 2638 ms 25808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 8532 KB Output is correct
2 Correct 13 ms 8396 KB Output is correct
3 Correct 15 ms 8328 KB Output is correct
4 Correct 15 ms 8404 KB Output is correct
5 Correct 82 ms 8660 KB Output is correct
6 Correct 62 ms 8908 KB Output is correct
7 Correct 13 ms 8404 KB Output is correct
8 Correct 14 ms 8404 KB Output is correct
9 Correct 48 ms 8788 KB Output is correct
10 Correct 87 ms 8880 KB Output is correct
11 Correct 79 ms 9032 KB Output is correct
12 Correct 73 ms 9040 KB Output is correct
13 Correct 175 ms 12436 KB Output is correct
14 Correct 200 ms 12536 KB Output is correct
15 Correct 1750 ms 19088 KB Output is correct
16 Correct 214 ms 12108 KB Output is correct
17 Correct 1340 ms 18016 KB Output is correct
18 Correct 975 ms 12704 KB Output is correct
19 Correct 2037 ms 23896 KB Output is correct
20 Correct 1250 ms 20204 KB Output is correct
21 Correct 180 ms 12796 KB Output is correct
22 Correct 192 ms 12876 KB Output is correct
23 Correct 1109 ms 13284 KB Output is correct
24 Correct 1245 ms 20884 KB Output is correct
25 Correct 1913 ms 22584 KB Output is correct
26 Correct 2636 ms 24944 KB Output is correct
27 Correct 2187 ms 25444 KB Output is correct
28 Correct 180 ms 12512 KB Output is correct
29 Correct 192 ms 12520 KB Output is correct
30 Correct 201 ms 12568 KB Output is correct
31 Correct 223 ms 12600 KB Output is correct
32 Correct 645 ms 12840 KB Output is correct
33 Correct 1008 ms 13448 KB Output is correct
34 Correct 1696 ms 23564 KB Output is correct
35 Correct 2502 ms 26612 KB Output is correct
36 Correct 203 ms 12872 KB Output is correct
37 Correct 1476 ms 13008 KB Output is correct
38 Correct 2574 ms 25648 KB Output is correct
39 Correct 3208 ms 25596 KB Output is correct
40 Correct 2712 ms 25560 KB Output is correct
41 Correct 3195 ms 25608 KB Output is correct
42 Correct 2638 ms 25808 KB Output is correct
43 Correct 1078 ms 20252 KB Output is correct
44 Correct 2050 ms 24456 KB Output is correct
45 Correct 1272 ms 20372 KB Output is correct
46 Correct 2262 ms 24620 KB Output is correct
47 Correct 189 ms 12872 KB Output is correct
48 Correct 199 ms 12936 KB Output is correct
49 Correct 1181 ms 13284 KB Output is correct
50 Correct 1705 ms 14492 KB Output is correct
51 Correct 1450 ms 21140 KB Output is correct
52 Correct 2161 ms 22288 KB Output is correct
53 Correct 2039 ms 21744 KB Output is correct
54 Correct 2462 ms 23120 KB Output is correct
55 Correct 2214 ms 22996 KB Output is correct
56 Correct 2450 ms 23892 KB Output is correct
57 Correct 2609 ms 24996 KB Output is correct
58 Correct 3065 ms 25116 KB Output is correct
59 Correct 2598 ms 25628 KB Output is correct
60 Correct 3218 ms 25624 KB Output is correct