Submission #411478

# Submission time Handle Problem Language Result Execution time Memory
411478 2021-05-25T11:41:04 Z mhy908 Collapse (JOI18_collapse) C++14
100 / 100
10788 ms 150716 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];
map<pii, int> tmp;

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

int del[100010];
bool ch[100010];

void solve(int s, int e){

    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> org, nedge;

    for(int i=s; i<=e; i++)ch[del[i]]=true;
    for(int i=1; i<s; i++){
        if(edge[i].F==0&&ch[i]==false)nedge.eb(edge[i].S);
    }
    for(int i=s; i<=e; i++){
        if(edge[i].F==1&&del[i]<s)org.eb(edge[i].S);
    }

    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++;
        if(edge[i].F==0)tmp[edge[i].S]=i;
        else del[i]=tmp[edge[i].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:73: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]
   73 |         for(; pv<nedge.size(); pv++){
      |               ~~^~~~~~~~~~~~~
collapse.cpp:88:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   88 |         while(uf.vc.size()>nsz)uf.rollback();
      |               ~~~~~~~~~~~~^~~~
collapse.cpp:97: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]
   97 |         for(; pv<nedge.size(); pv++){
      |               ~~^~~~~~~~~~~~~
collapse.cpp:112:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  112 |         while(uf.vc.size()>nsz)uf.rollback();
      |               ~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 56 ms 3152 KB Output is correct
2 Correct 6 ms 2992 KB Output is correct
3 Correct 15 ms 2976 KB Output is correct
4 Correct 19 ms 2980 KB Output is correct
5 Correct 199 ms 3100 KB Output is correct
6 Correct 363 ms 3452 KB Output is correct
7 Correct 6 ms 3016 KB Output is correct
8 Correct 8 ms 3020 KB Output is correct
9 Correct 195 ms 3296 KB Output is correct
10 Correct 430 ms 3464 KB Output is correct
11 Correct 349 ms 3784 KB Output is correct
12 Correct 359 ms 3840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 6884 KB Output is correct
2 Correct 73 ms 6832 KB Output is correct
3 Correct 4781 ms 12680 KB Output is correct
4 Correct 309 ms 7120 KB Output is correct
5 Correct 6780 ms 13040 KB Output is correct
6 Correct 5909 ms 7024 KB Output is correct
7 Correct 7500 ms 21324 KB Output is correct
8 Correct 6729 ms 16216 KB Output is correct
9 Correct 50 ms 7732 KB Output is correct
10 Correct 106 ms 7584 KB Output is correct
11 Correct 4411 ms 7812 KB Output is correct
12 Correct 6288 ms 16656 KB Output is correct
13 Correct 9904 ms 25980 KB Output is correct
14 Correct 8447 ms 53304 KB Output is correct
15 Correct 7699 ms 53312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 6872 KB Output is correct
2 Correct 85 ms 6816 KB Output is correct
3 Correct 161 ms 6948 KB Output is correct
4 Correct 319 ms 7044 KB Output is correct
5 Correct 6496 ms 6900 KB Output is correct
6 Correct 7245 ms 7020 KB Output is correct
7 Correct 7461 ms 17736 KB Output is correct
8 Correct 8357 ms 34276 KB Output is correct
9 Correct 56 ms 7652 KB Output is correct
10 Correct 6957 ms 7740 KB Output is correct
11 Correct 8978 ms 84732 KB Output is correct
12 Correct 9561 ms 150716 KB Output is correct
13 Correct 9142 ms 84820 KB Output is correct
14 Correct 9501 ms 150676 KB Output is correct
15 Correct 9084 ms 84840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 3152 KB Output is correct
2 Correct 6 ms 2992 KB Output is correct
3 Correct 15 ms 2976 KB Output is correct
4 Correct 19 ms 2980 KB Output is correct
5 Correct 199 ms 3100 KB Output is correct
6 Correct 363 ms 3452 KB Output is correct
7 Correct 6 ms 3016 KB Output is correct
8 Correct 8 ms 3020 KB Output is correct
9 Correct 195 ms 3296 KB Output is correct
10 Correct 430 ms 3464 KB Output is correct
11 Correct 349 ms 3784 KB Output is correct
12 Correct 359 ms 3840 KB Output is correct
13 Correct 56 ms 6884 KB Output is correct
14 Correct 73 ms 6832 KB Output is correct
15 Correct 4781 ms 12680 KB Output is correct
16 Correct 309 ms 7120 KB Output is correct
17 Correct 6780 ms 13040 KB Output is correct
18 Correct 5909 ms 7024 KB Output is correct
19 Correct 7500 ms 21324 KB Output is correct
20 Correct 6729 ms 16216 KB Output is correct
21 Correct 50 ms 7732 KB Output is correct
22 Correct 106 ms 7584 KB Output is correct
23 Correct 4411 ms 7812 KB Output is correct
24 Correct 6288 ms 16656 KB Output is correct
25 Correct 9904 ms 25980 KB Output is correct
26 Correct 8447 ms 53304 KB Output is correct
27 Correct 7699 ms 53312 KB Output is correct
28 Correct 48 ms 6872 KB Output is correct
29 Correct 85 ms 6816 KB Output is correct
30 Correct 161 ms 6948 KB Output is correct
31 Correct 319 ms 7044 KB Output is correct
32 Correct 6496 ms 6900 KB Output is correct
33 Correct 7245 ms 7020 KB Output is correct
34 Correct 7461 ms 17736 KB Output is correct
35 Correct 8357 ms 34276 KB Output is correct
36 Correct 56 ms 7652 KB Output is correct
37 Correct 6957 ms 7740 KB Output is correct
38 Correct 8978 ms 84732 KB Output is correct
39 Correct 9561 ms 150716 KB Output is correct
40 Correct 9142 ms 84820 KB Output is correct
41 Correct 9501 ms 150676 KB Output is correct
42 Correct 9084 ms 84840 KB Output is correct
43 Correct 5479 ms 14664 KB Output is correct
44 Correct 7945 ms 20388 KB Output is correct
45 Correct 6536 ms 15068 KB Output is correct
46 Correct 8448 ms 34320 KB Output is correct
47 Correct 56 ms 7656 KB Output is correct
48 Correct 105 ms 7572 KB Output is correct
49 Correct 5791 ms 7864 KB Output is correct
50 Correct 8785 ms 9520 KB Output is correct
51 Correct 7526 ms 15720 KB Output is correct
52 Correct 10201 ms 32404 KB Output is correct
53 Correct 10788 ms 32004 KB Output is correct
54 Correct 10047 ms 49548 KB Output is correct
55 Correct 10367 ms 49368 KB Output is correct
56 Correct 9941 ms 83496 KB Output is correct
57 Correct 9709 ms 84348 KB Output is correct
58 Correct 9636 ms 84132 KB Output is correct
59 Correct 8840 ms 84716 KB Output is correct
60 Correct 9417 ms 150616 KB Output is correct