Submission #906987

# Submission time Handle Problem Language Result Execution time Memory
906987 2024-01-15T05:33:11 Z bachhoangxuan Collapse (JOI18_collapse) C++17
100 / 100
4498 ms 463636 KB
#include "collapse.h"
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
const int B=320;
#define pii pair<int,int>
#define fi first
#define se second

vector<pii> e[maxn];
vector<int> cc[maxn],pp[maxn];

struct DSU{
    int par[maxn],sz[maxn],cnt=0;
    vector<pii> event;
    DSU(int n){
        cnt=0;
        for(int i=0;i<n;i++) par[i]=i,sz[i]=1;
    }
    void init(int n){
        cnt=0;
        for(int i=0;i<n;i++) par[i]=i,sz[i]=1;
    }
    int findpar(int u){
        if(u!=par[u]) return findpar(par[u]);
        return u;
    }
    void unions(int u,int v){
        u=findpar(u);v=findpar(v);
        if(u==v){
            event.push_back({-1,-1});
            return;
        }
        if(sz[u]<sz[v]) swap(u,v);
        par[v]=u;sz[u]+=sz[v];cnt++;
        event.push_back({u,v});
    }
    void roll_back(){
        pii x=event.back();
        event.pop_back();
        if(x.fi==-1) return;
        int u=x.fi,v=x.se;
        par[v]=v;sz[u]-=sz[v];cnt--;
    }
};

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
) {
    int C=(int)T.size(),Q=(int)W.size();
    vector<int> res(Q,0);
    vector<vector<int>> qq(C);
    for(int i=0;i<Q;i++) qq[W[i]].push_back(i);
    DSU dsu(N);
    set<pii> ss;
    for(int i=0;i<C;i+=B){
        set<pii> st;
        for(int j=i;j<min(i+B,C);j++){
            if(X[j]>Y[j]) swap(X[j],Y[j]);
            if(ss.find({X[j],Y[j]})!=ss.end()){
                ss.erase({X[j],Y[j]});
                st.insert({X[j],Y[j]});
            }
        }

        for(int j=0;j<N;j++) pp[j].clear();
        for(int j=i;j<min(i+B,C);j++){
            if(T[j]) st.erase({X[j],Y[j]});
            else st.insert({X[j],Y[j]});
            for(pii x:st) e[j].push_back(x);
            for(int id:qq[j]) pp[P[id]].push_back(id);
        }

        dsu.init(N);
        for(int j=0;j<N;j++) cc[j].clear();
        for(pii x:ss) cc[x.se].push_back(x.fi);

        for(int j=0;j<N;j++){
            for(int u:cc[j]) dsu.unions(u,j);
            for(int id:pp[j]){
                for(pii x:e[W[id]]) if(max(x.fi,x.se)<=j) dsu.unions(x.fi,x.se);
                res[id]+=dsu.cnt;
                for(pii x:e[W[id]]) if(max(x.fi,x.se)<=j) dsu.roll_back();
            }
        }

        dsu.init(N);
        for(int j=0;j<N;j++) cc[j].clear();
        for(pii x:ss) cc[x.fi].push_back(x.se);

        for(int j=N-1;j>=0;j--){
            for(int id:pp[j]){
                for(pii x:e[W[id]]) if(min(x.fi,x.se)>j) dsu.unions(x.fi,x.se);
                res[id]+=dsu.cnt;
                for(pii x:e[W[id]]) if(min(x.fi,x.se)>j) dsu.roll_back();
            }
            for(int u:cc[j]) dsu.unions(u,j);
        }

        for(pii x:st) ss.insert(x);
    }
    for(int i=0;i<Q;i++) res[i]=N-res[i];
    return res;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8792 KB Output is correct
2 Correct 4 ms 8284 KB Output is correct
3 Correct 4 ms 8284 KB Output is correct
4 Correct 5 ms 8284 KB Output is correct
5 Correct 8 ms 9308 KB Output is correct
6 Correct 29 ms 19112 KB Output is correct
7 Correct 5 ms 8540 KB Output is correct
8 Correct 5 ms 8540 KB Output is correct
9 Correct 10 ms 9820 KB Output is correct
10 Correct 24 ms 15956 KB Output is correct
11 Correct 37 ms 19192 KB Output is correct
12 Correct 34 ms 19156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 11728 KB Output is correct
2 Correct 23 ms 11732 KB Output is correct
3 Correct 164 ms 46936 KB Output is correct
4 Correct 39 ms 11992 KB Output is correct
5 Correct 290 ms 102352 KB Output is correct
6 Correct 138 ms 22220 KB Output is correct
7 Correct 1231 ms 458848 KB Output is correct
8 Correct 331 ms 112920 KB Output is correct
9 Correct 27 ms 11988 KB Output is correct
10 Correct 31 ms 11992 KB Output is correct
11 Correct 142 ms 13904 KB Output is correct
12 Correct 523 ms 103372 KB Output is correct
13 Correct 1535 ms 300696 KB Output is correct
14 Correct 3237 ms 461868 KB Output is correct
15 Correct 2643 ms 462712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 11728 KB Output is correct
2 Correct 29 ms 11612 KB Output is correct
3 Correct 32 ms 11868 KB Output is correct
4 Correct 46 ms 11824 KB Output is correct
5 Correct 141 ms 12532 KB Output is correct
6 Correct 164 ms 22120 KB Output is correct
7 Correct 893 ms 423704 KB Output is correct
8 Correct 1882 ms 459684 KB Output is correct
9 Correct 53 ms 14292 KB Output is correct
10 Correct 254 ms 15636 KB Output is correct
11 Correct 3587 ms 463636 KB Output is correct
12 Correct 4329 ms 463344 KB Output is correct
13 Correct 3752 ms 462312 KB Output is correct
14 Correct 4498 ms 463460 KB Output is correct
15 Correct 3797 ms 462868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8792 KB Output is correct
2 Correct 4 ms 8284 KB Output is correct
3 Correct 4 ms 8284 KB Output is correct
4 Correct 5 ms 8284 KB Output is correct
5 Correct 8 ms 9308 KB Output is correct
6 Correct 29 ms 19112 KB Output is correct
7 Correct 5 ms 8540 KB Output is correct
8 Correct 5 ms 8540 KB Output is correct
9 Correct 10 ms 9820 KB Output is correct
10 Correct 24 ms 15956 KB Output is correct
11 Correct 37 ms 19192 KB Output is correct
12 Correct 34 ms 19156 KB Output is correct
13 Correct 23 ms 11728 KB Output is correct
14 Correct 23 ms 11732 KB Output is correct
15 Correct 164 ms 46936 KB Output is correct
16 Correct 39 ms 11992 KB Output is correct
17 Correct 290 ms 102352 KB Output is correct
18 Correct 138 ms 22220 KB Output is correct
19 Correct 1231 ms 458848 KB Output is correct
20 Correct 331 ms 112920 KB Output is correct
21 Correct 27 ms 11988 KB Output is correct
22 Correct 31 ms 11992 KB Output is correct
23 Correct 142 ms 13904 KB Output is correct
24 Correct 523 ms 103372 KB Output is correct
25 Correct 1535 ms 300696 KB Output is correct
26 Correct 3237 ms 461868 KB Output is correct
27 Correct 2643 ms 462712 KB Output is correct
28 Correct 24 ms 11728 KB Output is correct
29 Correct 29 ms 11612 KB Output is correct
30 Correct 32 ms 11868 KB Output is correct
31 Correct 46 ms 11824 KB Output is correct
32 Correct 141 ms 12532 KB Output is correct
33 Correct 164 ms 22120 KB Output is correct
34 Correct 893 ms 423704 KB Output is correct
35 Correct 1882 ms 459684 KB Output is correct
36 Correct 53 ms 14292 KB Output is correct
37 Correct 254 ms 15636 KB Output is correct
38 Correct 3587 ms 463636 KB Output is correct
39 Correct 4329 ms 463344 KB Output is correct
40 Correct 3752 ms 462312 KB Output is correct
41 Correct 4498 ms 463460 KB Output is correct
42 Correct 3797 ms 462868 KB Output is correct
43 Correct 265 ms 79332 KB Output is correct
44 Correct 1499 ms 459480 KB Output is correct
45 Correct 348 ms 105936 KB Output is correct
46 Correct 2266 ms 460064 KB Output is correct
47 Correct 47 ms 14292 KB Output is correct
48 Correct 41 ms 13476 KB Output is correct
49 Correct 233 ms 15740 KB Output is correct
50 Correct 430 ms 41668 KB Output is correct
51 Correct 713 ms 117240 KB Output is correct
52 Correct 1476 ms 264808 KB Output is correct
53 Correct 1497 ms 265756 KB Output is correct
54 Correct 2019 ms 301000 KB Output is correct
55 Correct 2299 ms 301852 KB Output is correct
56 Correct 3048 ms 450540 KB Output is correct
57 Correct 2957 ms 440816 KB Output is correct
58 Correct 3022 ms 439528 KB Output is correct
59 Correct 2805 ms 463380 KB Output is correct
60 Correct 3285 ms 462972 KB Output is correct