Submission #411470

# Submission time Handle Problem Language Result Execution time Memory
411470 2021-05-25T11:29:09 Z mhy908 Collapse (JOI18_collapse) C++14
100 / 100
13610 ms 151428 KB
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define mp make_pair
#define eb emplace_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
typedef pair<int, int> pii;

const int BUC=500;

struct UNION_FIND{
    int par[100010], sz[100010], cnt;
    vector<int> vc;
    void init(int num){for(int i=1; i<=num; i++)par[i]=i, sz[i]=1; cnt=0;}
    int findpar(int num){return num==par[num]?num:findpar(par[num]);}
    void mergepar(int a, int b){
        a=findpar(a);
        b=findpar(b);
        if(a==b)return;
        if(sz[a]<sz[b])swap(a, b);
        vc.eb(b);
        par[b]=a;
        sz[a]+=sz[b];
        cnt++;
    }
    void rollback(){
        int tmp=vc.back();
        vc.pop_back();
        sz[par[tmp]]-=sz[tmp];
        par[tmp]=tmp;
        cnt--;
    }
}uf;

int n, m, q, query[100010], ans[100010];
pair<int, pii> edge[100010];
vector<int> qlst[100010];

bool cmpl(pii a, pii b){return a.S<b.S;}
bool cmpr(pii a, pii b){return a.F>b.F;}

void solve(int s, int e){

    set<pii> edgs;
    for(int i=1; i<s; i++){
        if(edge[i].F==0)edgs.insert(edge[i].S);
        else edgs.erase(edge[i].S);
    }
    vector<pii> org;
    for(int i=s; i<=e; i++){
        if(edge[i].F&&edgs.find(edge[i].S)!=edgs.end()){
            edgs.erase(edge[i].S);
            org.eb(edge[i].S);
        }
    }

    vector<pair<int, pii> > nq;
    for(int i=s; i<=e; i++){
        for(auto j:qlst[i])nq.eb(query[j], mp(i, j));
    }

    vector<pii> nedge;
    for(auto i:edgs)nedge.eb(i);

    sort(all(nedge), cmpl);
    sort(all(nq));
    uf.init(n);

    int pv=0;
    for(auto i:nq){
        for(; pv<nedge.size(); pv++){
            if(nedge[pv].S>i.F)break;
            uf.mergepar(nedge[pv].F, nedge[pv].S);
        }
        set<pii> nadd;
        for(auto j:org)nadd.insert(j);
        for(int j=s; j<=i.S.F; j++){
            if(edge[j].F==0)nadd.insert(edge[j].S);
            else nadd.erase(edge[j].S);
        }

        int nsz=uf.vc.size();

        for(auto j:nadd){
            if(j.S<=i.F)uf.mergepar(j.F, j.S);
        }

        ans[i.S.S]+=i.F-uf.cnt;

        while(uf.vc.size()>nsz)uf.rollback();

    }

    sort(all(nedge), cmpr);
    reverse(all(nq));
    uf.init(n);
    pv=0;
    
    for(auto i:nq){
        for(; pv<nedge.size(); pv++){
            if(nedge[pv].F<=i.F)break;
            uf.mergepar(nedge[pv].F, nedge[pv].S);
        }
        set<pii> nadd;
        for(auto j:org)nadd.insert(j);
        for(int j=s; j<=i.S.F; j++){
            if(edge[j].F==0)nadd.insert(edge[j].S);
            else nadd.erase(edge[j].S);
        }

        int nsz=uf.vc.size();

        for(auto j:nadd){
            if(j.F>i.F)uf.mergepar(j.F, j.S);
        }

        ans[i.S.S]+=n-i.F-uf.cnt;

        while(uf.vc.size()>nsz)uf.rollback();

    }

}

/*
int main(){
    scanf("%d %d %d", &n, &m, &q);
    for(int i=1; i<=m; i++){
        scanf("%d %d %d", &edge[i].F, &edge[i].S.F, &edge[i].S.S);
        if(edge[i].S.F>edge[i].S.S)swap(edge[i].S.F, edge[i].S.S);
        edge[i].S.F++, edge[i].S.S++;
    }
    for(int i=1; i<=q; i++){
        int a, b;
        scanf("%d %d", &a, &b);
        qlst[a+1].eb(i);
        query[i]=b+1;
    }

    for(int i=1; i<=m; i+=BUC)solve(i, min(m, i+BUC-1));
    for(int i=1; i<=q; i++)printf("%d\n", ans[i]);
}
*/

vector<int> simulateCollapse(
	int N,
	vector<int> T,
	vector<int> X,
	vector<int> Y,
	vector<int> W,
	vector<int> P
) {
    n=N; m=T.size(); q=W.size();
    for(int i=1; i<=m; i++){
        edge[i].F=T[i-1];
        edge[i].S.F=X[i-1];
        edge[i].S.S=Y[i-1];
        if(edge[i].S.F>edge[i].S.S)swap(edge[i].S.F, edge[i].S.S);
        edge[i].S.F++, edge[i].S.S++;
    }
    
    for(int i=1; i<=q; i++){
        int a=W[i-1], b=P[i-1];
        qlst[a+1].eb(i);
        query[i]=b+1;
    }
    
    vector<int> ret;
    
    for(int i=1; i<=m; i+=BUC)solve(i, min(m, i+BUC-1));
    for(int i=1; i<=q; i++)ret.eb(ans[i]);
    
    return ret;
}

Compilation message

collapse.cpp: In function 'void solve(int, int)':
collapse.cpp:75:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |         for(; pv<nedge.size(); pv++){
      |               ~~^~~~~~~~~~~~~
collapse.cpp:94:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   94 |         while(uf.vc.size()>nsz)uf.rollback();
      |               ~~~~~~~~~~~~^~~~
collapse.cpp:104:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  104 |         for(; pv<nedge.size(); pv++){
      |               ~~^~~~~~~~~~~~~
collapse.cpp:123:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  123 |         while(uf.vc.size()>nsz)uf.rollback();
      |               ~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 57 ms 3168 KB Output is correct
2 Correct 7 ms 2892 KB Output is correct
3 Correct 14 ms 2916 KB Output is correct
4 Correct 20 ms 2892 KB Output is correct
5 Correct 220 ms 3132 KB Output is correct
6 Correct 374 ms 3380 KB Output is correct
7 Correct 5 ms 3020 KB Output is correct
8 Correct 8 ms 3020 KB Output is correct
9 Correct 213 ms 3212 KB Output is correct
10 Correct 440 ms 3288 KB Output is correct
11 Correct 354 ms 3732 KB Output is correct
12 Correct 382 ms 3792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 7348 KB Output is correct
2 Correct 70 ms 7356 KB Output is correct
3 Correct 5459 ms 13552 KB Output is correct
4 Correct 314 ms 7516 KB Output is correct
5 Correct 8175 ms 13856 KB Output is correct
6 Correct 5570 ms 7608 KB Output is correct
7 Correct 11269 ms 20796 KB Output is correct
8 Correct 7793 ms 14600 KB Output is correct
9 Correct 49 ms 8500 KB Output is correct
10 Correct 105 ms 8332 KB Output is correct
11 Correct 4412 ms 8824 KB Output is correct
12 Correct 7520 ms 15516 KB Output is correct
13 Correct 13122 ms 25192 KB Output is correct
14 Correct 12238 ms 53836 KB Output is correct
15 Correct 11409 ms 53988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 7356 KB Output is correct
2 Correct 89 ms 7156 KB Output is correct
3 Correct 172 ms 7300 KB Output is correct
4 Correct 329 ms 7504 KB Output is correct
5 Correct 6538 ms 7560 KB Output is correct
6 Correct 6754 ms 7668 KB Output is correct
7 Correct 9330 ms 17880 KB Output is correct
8 Correct 12320 ms 34092 KB Output is correct
9 Correct 54 ms 8500 KB Output is correct
10 Correct 6809 ms 8736 KB Output is correct
11 Correct 12692 ms 84340 KB Output is correct
12 Correct 13317 ms 151428 KB Output is correct
13 Correct 12827 ms 84556 KB Output is correct
14 Correct 13610 ms 151316 KB Output is correct
15 Correct 13172 ms 84488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 3168 KB Output is correct
2 Correct 7 ms 2892 KB Output is correct
3 Correct 14 ms 2916 KB Output is correct
4 Correct 20 ms 2892 KB Output is correct
5 Correct 220 ms 3132 KB Output is correct
6 Correct 374 ms 3380 KB Output is correct
7 Correct 5 ms 3020 KB Output is correct
8 Correct 8 ms 3020 KB Output is correct
9 Correct 213 ms 3212 KB Output is correct
10 Correct 440 ms 3288 KB Output is correct
11 Correct 354 ms 3732 KB Output is correct
12 Correct 382 ms 3792 KB Output is correct
13 Correct 46 ms 7348 KB Output is correct
14 Correct 70 ms 7356 KB Output is correct
15 Correct 5459 ms 13552 KB Output is correct
16 Correct 314 ms 7516 KB Output is correct
17 Correct 8175 ms 13856 KB Output is correct
18 Correct 5570 ms 7608 KB Output is correct
19 Correct 11269 ms 20796 KB Output is correct
20 Correct 7793 ms 14600 KB Output is correct
21 Correct 49 ms 8500 KB Output is correct
22 Correct 105 ms 8332 KB Output is correct
23 Correct 4412 ms 8824 KB Output is correct
24 Correct 7520 ms 15516 KB Output is correct
25 Correct 13122 ms 25192 KB Output is correct
26 Correct 12238 ms 53836 KB Output is correct
27 Correct 11409 ms 53988 KB Output is correct
28 Correct 44 ms 7356 KB Output is correct
29 Correct 89 ms 7156 KB Output is correct
30 Correct 172 ms 7300 KB Output is correct
31 Correct 329 ms 7504 KB Output is correct
32 Correct 6538 ms 7560 KB Output is correct
33 Correct 6754 ms 7668 KB Output is correct
34 Correct 9330 ms 17880 KB Output is correct
35 Correct 12320 ms 34092 KB Output is correct
36 Correct 54 ms 8500 KB Output is correct
37 Correct 6809 ms 8736 KB Output is correct
38 Correct 12692 ms 84340 KB Output is correct
39 Correct 13317 ms 151428 KB Output is correct
40 Correct 12827 ms 84556 KB Output is correct
41 Correct 13610 ms 151316 KB Output is correct
42 Correct 13172 ms 84488 KB Output is correct
43 Correct 6512 ms 12992 KB Output is correct
44 Correct 11961 ms 20684 KB Output is correct
45 Correct 7632 ms 13744 KB Output is correct
46 Correct 12474 ms 33908 KB Output is correct
47 Correct 59 ms 8404 KB Output is correct
48 Correct 106 ms 8368 KB Output is correct
49 Correct 5871 ms 8792 KB Output is correct
50 Correct 8292 ms 9928 KB Output is correct
51 Correct 8767 ms 14548 KB Output is correct
52 Correct 13065 ms 31176 KB Output is correct
53 Correct 13598 ms 31144 KB Output is correct
54 Correct 13185 ms 48512 KB Output is correct
55 Correct 13561 ms 48532 KB Output is correct
56 Correct 13293 ms 82892 KB Output is correct
57 Correct 13363 ms 83308 KB Output is correct
58 Correct 13425 ms 83188 KB Output is correct
59 Correct 12410 ms 84304 KB Output is correct
60 Correct 13561 ms 151272 KB Output is correct