Submission #411476

# Submission time Handle Problem Language Result Execution time Memory
411476 2021-05-25T11:36:01 Z mhy908 Collapse (JOI18_collapse) C++14
5 / 100
15000 ms 79488 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=100;

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:90:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   90 |         while(uf.vc.size()>nsz)uf.rollback();
      |               ~~~~~~~~~~~~^~~~
collapse.cpp:99: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]
   99 |         for(; pv<nedge.size(); pv++){
      |               ~~^~~~~~~~~~~~~
collapse.cpp:114:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  114 |         while(uf.vc.size()>nsz)uf.rollback();
      |               ~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 19 ms 3112 KB Output is correct
2 Correct 10 ms 2892 KB Output is correct
3 Correct 14 ms 2936 KB Output is correct
4 Correct 21 ms 2980 KB Output is correct
5 Correct 60 ms 3064 KB Output is correct
6 Correct 96 ms 3456 KB Output is correct
7 Correct 6 ms 3024 KB Output is correct
8 Correct 8 ms 3016 KB Output is correct
9 Correct 59 ms 3088 KB Output is correct
10 Correct 100 ms 3384 KB Output is correct
11 Correct 107 ms 4352 KB Output is correct
12 Correct 102 ms 4320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 6964 KB Output is correct
2 Correct 71 ms 6936 KB Output is correct
3 Correct 6306 ms 12196 KB Output is correct
4 Correct 322 ms 7048 KB Output is correct
5 Correct 7754 ms 12588 KB Output is correct
6 Correct 786 ms 6748 KB Output is correct
7 Execution timed out 15067 ms 20140 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 45 ms 6848 KB Output is correct
2 Correct 86 ms 6772 KB Output is correct
3 Correct 160 ms 6944 KB Output is correct
4 Correct 337 ms 7036 KB Output is correct
5 Correct 893 ms 6604 KB Output is correct
6 Correct 1010 ms 6776 KB Output is correct
7 Correct 14555 ms 21148 KB Output is correct
8 Execution timed out 15097 ms 79488 KB Time limit exceeded
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 3112 KB Output is correct
2 Correct 10 ms 2892 KB Output is correct
3 Correct 14 ms 2936 KB Output is correct
4 Correct 21 ms 2980 KB Output is correct
5 Correct 60 ms 3064 KB Output is correct
6 Correct 96 ms 3456 KB Output is correct
7 Correct 6 ms 3024 KB Output is correct
8 Correct 8 ms 3016 KB Output is correct
9 Correct 59 ms 3088 KB Output is correct
10 Correct 100 ms 3384 KB Output is correct
11 Correct 107 ms 4352 KB Output is correct
12 Correct 102 ms 4320 KB Output is correct
13 Correct 45 ms 6964 KB Output is correct
14 Correct 71 ms 6936 KB Output is correct
15 Correct 6306 ms 12196 KB Output is correct
16 Correct 322 ms 7048 KB Output is correct
17 Correct 7754 ms 12588 KB Output is correct
18 Correct 786 ms 6748 KB Output is correct
19 Execution timed out 15067 ms 20140 KB Time limit exceeded
20 Halted 0 ms 0 KB -