Submission #679461

# Submission time Handle Problem Language Result Execution time Memory
679461 2023-01-08T10:04:40 Z qwerasdfzxcl Collapse (JOI18_collapse) C++17
100 / 100
4485 ms 25904 KB
#include "collapse.h"
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
constexpr int B = 2020;
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;
      |             ^~
# Verdict Execution time Memory Grader output
1 Correct 81 ms 8568 KB Output is correct
2 Correct 28 ms 8372 KB Output is correct
3 Correct 30 ms 8384 KB Output is correct
4 Correct 29 ms 8264 KB Output is correct
5 Correct 148 ms 8532 KB Output is correct
6 Correct 106 ms 8868 KB Output is correct
7 Correct 26 ms 8408 KB Output is correct
8 Correct 27 ms 8412 KB Output is correct
9 Correct 83 ms 8732 KB Output is correct
10 Correct 142 ms 8660 KB Output is correct
11 Correct 137 ms 8980 KB Output is correct
12 Correct 126 ms 8848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 437 ms 12108 KB Output is correct
2 Correct 454 ms 12048 KB Output is correct
3 Correct 3452 ms 17816 KB Output is correct
4 Correct 475 ms 12056 KB Output is correct
5 Correct 2374 ms 17932 KB Output is correct
6 Correct 1816 ms 12944 KB Output is correct
7 Correct 2483 ms 23956 KB Output is correct
8 Correct 2261 ms 20104 KB Output is correct
9 Correct 440 ms 12952 KB Output is correct
10 Correct 462 ms 12868 KB Output is correct
11 Correct 1917 ms 13076 KB Output is correct
12 Correct 2210 ms 20756 KB Output is correct
13 Correct 3433 ms 22680 KB Output is correct
14 Correct 3337 ms 24936 KB Output is correct
15 Correct 2921 ms 25380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 447 ms 12196 KB Output is correct
2 Correct 451 ms 12132 KB Output is correct
3 Correct 461 ms 12228 KB Output is correct
4 Correct 476 ms 12280 KB Output is correct
5 Correct 895 ms 12184 KB Output is correct
6 Correct 1881 ms 13032 KB Output is correct
7 Correct 2269 ms 21868 KB Output is correct
8 Correct 2827 ms 24244 KB Output is correct
9 Correct 473 ms 12864 KB Output is correct
10 Correct 1731 ms 13004 KB Output is correct
11 Correct 3433 ms 25472 KB Output is correct
12 Correct 4175 ms 25440 KB Output is correct
13 Correct 3447 ms 25712 KB Output is correct
14 Correct 4167 ms 25564 KB Output is correct
15 Correct 3399 ms 25488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 8568 KB Output is correct
2 Correct 28 ms 8372 KB Output is correct
3 Correct 30 ms 8384 KB Output is correct
4 Correct 29 ms 8264 KB Output is correct
5 Correct 148 ms 8532 KB Output is correct
6 Correct 106 ms 8868 KB Output is correct
7 Correct 26 ms 8408 KB Output is correct
8 Correct 27 ms 8412 KB Output is correct
9 Correct 83 ms 8732 KB Output is correct
10 Correct 142 ms 8660 KB Output is correct
11 Correct 137 ms 8980 KB Output is correct
12 Correct 126 ms 8848 KB Output is correct
13 Correct 437 ms 12108 KB Output is correct
14 Correct 454 ms 12048 KB Output is correct
15 Correct 3452 ms 17816 KB Output is correct
16 Correct 475 ms 12056 KB Output is correct
17 Correct 2374 ms 17932 KB Output is correct
18 Correct 1816 ms 12944 KB Output is correct
19 Correct 2483 ms 23956 KB Output is correct
20 Correct 2261 ms 20104 KB Output is correct
21 Correct 440 ms 12952 KB Output is correct
22 Correct 462 ms 12868 KB Output is correct
23 Correct 1917 ms 13076 KB Output is correct
24 Correct 2210 ms 20756 KB Output is correct
25 Correct 3433 ms 22680 KB Output is correct
26 Correct 3337 ms 24936 KB Output is correct
27 Correct 2921 ms 25380 KB Output is correct
28 Correct 447 ms 12196 KB Output is correct
29 Correct 451 ms 12132 KB Output is correct
30 Correct 461 ms 12228 KB Output is correct
31 Correct 476 ms 12280 KB Output is correct
32 Correct 895 ms 12184 KB Output is correct
33 Correct 1881 ms 13032 KB Output is correct
34 Correct 2269 ms 21868 KB Output is correct
35 Correct 2827 ms 24244 KB Output is correct
36 Correct 473 ms 12864 KB Output is correct
37 Correct 1731 ms 13004 KB Output is correct
38 Correct 3433 ms 25472 KB Output is correct
39 Correct 4175 ms 25440 KB Output is correct
40 Correct 3447 ms 25712 KB Output is correct
41 Correct 4167 ms 25564 KB Output is correct
42 Correct 3399 ms 25488 KB Output is correct
43 Correct 1934 ms 20240 KB Output is correct
44 Correct 2502 ms 24216 KB Output is correct
45 Correct 2202 ms 20336 KB Output is correct
46 Correct 2786 ms 24380 KB Output is correct
47 Correct 449 ms 12888 KB Output is correct
48 Correct 484 ms 12936 KB Output is correct
49 Correct 2054 ms 13064 KB Output is correct
50 Correct 3429 ms 14520 KB Output is correct
51 Correct 2433 ms 20920 KB Output is correct
52 Correct 4212 ms 21860 KB Output is correct
53 Correct 4057 ms 21584 KB Output is correct
54 Correct 4324 ms 22976 KB Output is correct
55 Correct 4003 ms 22984 KB Output is correct
56 Correct 4022 ms 23608 KB Output is correct
57 Correct 3804 ms 25004 KB Output is correct
58 Correct 4485 ms 25148 KB Output is correct
59 Correct 3438 ms 25904 KB Output is correct
60 Correct 4165 ms 25376 KB Output is correct