Submission #375396

# Submission time Handle Problem Language Result Execution time Memory
375396 2021-03-09T10:34:30 Z rrrr10000 Collapse (JOI18_collapse) C++14
100 / 100
6578 ms 19272 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
using pl=pair<ll,ll>;
typedef vector<pl> vp;
typedef vector<vp> vvp;
typedef vector<bool> vb;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,k,n) for(ll i=(ll)(k);i<(ll)(n);i++)
#define fi first
#define se second
#define pb emplace_back
#define all(a) a.begin(),a.end()
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define dame(a) {out(a);return 0;}
#define rsort(a) {sort(all(a));reverse(all(a));}
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> void outp(T p){printf("(%d,%d)",p.fi,p.se);cout<<'\n';}
template<class T> void outvp(T v){for(auto p:v)printf("(%d,%d)",p.fi,p.se);cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
#include "collapse.h"
class UF{
    vi par,sz;
public:
    ll cnt;
    vp memo;
    UF(ll n){
        par=vi(n,-1);sz=vi(n,1);cnt=n;
    }
    ll root(ll i){if(par[i]==-1)return i;return root(par[i]);}
    void merge(ll a,ll b,bool flag){
        a=root(a);b=root(b);
        if(a==b)return;
        if(sz[a]>sz[b])swap(a,b);
        if(flag){memo.pb(a,par[a]);memo.pb(b,sz[b]);}
        par[a]=b;sz[b]+=sz[a];cnt--;
    }
    void rollback(){
        while(!memo.empty()){
            sz[memo.back().fi]=memo.back().se;memo.pop_back();
            par[memo.back().fi]=memo.back().se;memo.pop_back();
            cnt++;
        }
    }
};
vector<int> simulateCollapse(int n,vector<int> T,vector<int> X,vector<int> Y,vector<int> W,vector<int> P){
    ll C=T.size(),Q=W.size(),B=300;
    rep(i,C)if(X[i]>Y[i])swap(X[i],Y[i]);
    vp srtedge(C);
    vi edgeid(C);
    rep(i,C)srtedge[i]=pl(X[i],Y[i]);
    sort(all(srtedge));
    rep(i,C)edgeid[i]=lb(srtedge,pl(X[i],Y[i]));
	vvp query(C/B+1);
    rep(i,Q){
        query[W[i]/B].pb(P[i],i);
    }
    vector<int> ans(Q);
    rep(i,query.size())sort(all(query[i]));
    rep(i,query.size()){
        vb special(C,false);REP(j,i*B,min(C,(i+1)*B))special[edgeid[j]]=true;
        vi con(C);rep(j,i*B)con[edgeid[j]]=1-con[edgeid[j]];
        UF uf(n);
        vp s;rep(j,i*B)if(con[edgeid[j]]&&!special[edgeid[j]])s.pb(Y[j],X[j]);
        sort(all(s));
        ll w=0;
        vb used(C,false);
        for(auto x:query[i]){
            while(w!=s.size()&&s[w].fi<=x.fi){
                uf.merge(s[w].fi,s[w].se,0);
                w++;
            }
            for(int j=W[x.se];j>=i*B;j--)if(Y[j]<=x.fi&&!used[edgeid[j]]){
                if(!T[j])uf.merge(X[j],Y[j],1);
                used[edgeid[j]]=true;
            }
            REP(j,W[x.se]+1,min(C,(i+1)*B))if(Y[j]<=x.fi&&!used[edgeid[j]]&&con[edgeid[j]]){
                used[edgeid[j]]=true;
                uf.merge(X[j],Y[j],1);
            }
            ans[x.se]+=uf.cnt-(n-P[x.se]-1);
            REP(j,i*B,min(C,(i+1)*B))used[edgeid[j]]=false;
            uf.rollback();
        }
    }
    // outv(ans);
    rep(i,query.size())reverse(all(query[i]));
    rep(i,query.size()){
        vb special(C,false);REP(j,i*B,min(C,(i+1)*B))special[edgeid[j]]=true;
        vi con(C);rep(j,i*B)con[edgeid[j]]=1-con[edgeid[j]];
        UF uf(n);
        vp s;rep(j,i*B)if(con[edgeid[j]]&&!special[edgeid[j]])s.pb(X[j],Y[j]);
        rsort(s);
        vb used(C,false);
        ll w=0;
        for(auto x:query[i]){
            while(w!=s.size()&&s[w].fi>x.fi){
                uf.merge(s[w].fi,s[w].se,0);
                w++;
            }
            for(int j=W[x.se];j>=i*B;j--)if(X[j]>x.fi&&!used[edgeid[j]]){
                if(!T[j])uf.merge(X[j],Y[j],1);
                used[edgeid[j]]=true;
            }
            REP(j,W[x.se]+1,min(C,(i+1)*B))if(X[j]>x.fi&&!used[edgeid[j]]&&con[edgeid[j]]){
                used[edgeid[j]]=true;
                uf.merge(X[j],Y[j],1);
            }
            ans[x.se]+=uf.cnt-P[x.se]-1;
            REP(j,i*B,min(C,(i+1)*B))used[edgeid[j]]=false;
            uf.rollback();
        }
    }
    return ans;
}

Compilation message

collapse.cpp: In function 'std::vector<int> simulateCollapse(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
collapse.cpp:77:20: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |             while(w!=s.size()&&s[w].fi<=x.fi){
      |                   ~^~~~~~~~~~
collapse.cpp:105:20: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |             while(w!=s.size()&&s[w].fi>x.fi){
      |                   ~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 17 ms 876 KB Output is correct
2 Correct 3 ms 620 KB Output is correct
3 Correct 6 ms 620 KB Output is correct
4 Correct 9 ms 748 KB Output is correct
5 Correct 33 ms 1004 KB Output is correct
6 Correct 44 ms 1176 KB Output is correct
7 Correct 4 ms 620 KB Output is correct
8 Correct 5 ms 620 KB Output is correct
9 Correct 29 ms 1132 KB Output is correct
10 Correct 43 ms 1116 KB Output is correct
11 Correct 60 ms 1272 KB Output is correct
12 Correct 51 ms 1240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 4064 KB Output is correct
2 Correct 57 ms 4064 KB Output is correct
3 Correct 827 ms 12508 KB Output is correct
4 Correct 116 ms 4576 KB Output is correct
5 Correct 885 ms 12776 KB Output is correct
6 Correct 429 ms 5544 KB Output is correct
7 Correct 5313 ms 16748 KB Output is correct
8 Correct 899 ms 13280 KB Output is correct
9 Correct 46 ms 6236 KB Output is correct
10 Correct 71 ms 6236 KB Output is correct
11 Correct 598 ms 6904 KB Output is correct
12 Correct 1407 ms 15228 KB Output is correct
13 Correct 3492 ms 16840 KB Output is correct
14 Correct 5686 ms 18664 KB Output is correct
15 Correct 5536 ms 18884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 4064 KB Output is correct
2 Correct 58 ms 4084 KB Output is correct
3 Correct 87 ms 4448 KB Output is correct
4 Correct 124 ms 4576 KB Output is correct
5 Correct 571 ms 4492 KB Output is correct
6 Correct 473 ms 5612 KB Output is correct
7 Correct 3153 ms 13928 KB Output is correct
8 Correct 5639 ms 17084 KB Output is correct
9 Correct 55 ms 6236 KB Output is correct
10 Correct 756 ms 6968 KB Output is correct
11 Correct 6189 ms 19272 KB Output is correct
12 Correct 6578 ms 18928 KB Output is correct
13 Correct 6414 ms 18896 KB Output is correct
14 Correct 6453 ms 18856 KB Output is correct
15 Correct 6234 ms 18744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 876 KB Output is correct
2 Correct 3 ms 620 KB Output is correct
3 Correct 6 ms 620 KB Output is correct
4 Correct 9 ms 748 KB Output is correct
5 Correct 33 ms 1004 KB Output is correct
6 Correct 44 ms 1176 KB Output is correct
7 Correct 4 ms 620 KB Output is correct
8 Correct 5 ms 620 KB Output is correct
9 Correct 29 ms 1132 KB Output is correct
10 Correct 43 ms 1116 KB Output is correct
11 Correct 60 ms 1272 KB Output is correct
12 Correct 51 ms 1240 KB Output is correct
13 Correct 39 ms 4064 KB Output is correct
14 Correct 57 ms 4064 KB Output is correct
15 Correct 827 ms 12508 KB Output is correct
16 Correct 116 ms 4576 KB Output is correct
17 Correct 885 ms 12776 KB Output is correct
18 Correct 429 ms 5544 KB Output is correct
19 Correct 5313 ms 16748 KB Output is correct
20 Correct 899 ms 13280 KB Output is correct
21 Correct 46 ms 6236 KB Output is correct
22 Correct 71 ms 6236 KB Output is correct
23 Correct 598 ms 6904 KB Output is correct
24 Correct 1407 ms 15228 KB Output is correct
25 Correct 3492 ms 16840 KB Output is correct
26 Correct 5686 ms 18664 KB Output is correct
27 Correct 5536 ms 18884 KB Output is correct
28 Correct 38 ms 4064 KB Output is correct
29 Correct 58 ms 4084 KB Output is correct
30 Correct 87 ms 4448 KB Output is correct
31 Correct 124 ms 4576 KB Output is correct
32 Correct 571 ms 4492 KB Output is correct
33 Correct 473 ms 5612 KB Output is correct
34 Correct 3153 ms 13928 KB Output is correct
35 Correct 5639 ms 17084 KB Output is correct
36 Correct 55 ms 6236 KB Output is correct
37 Correct 756 ms 6968 KB Output is correct
38 Correct 6189 ms 19272 KB Output is correct
39 Correct 6578 ms 18928 KB Output is correct
40 Correct 6414 ms 18896 KB Output is correct
41 Correct 6453 ms 18856 KB Output is correct
42 Correct 6234 ms 18744 KB Output is correct
43 Correct 921 ms 12924 KB Output is correct
44 Correct 5359 ms 16892 KB Output is correct
45 Correct 1012 ms 13236 KB Output is correct
46 Correct 5569 ms 17332 KB Output is correct
47 Correct 53 ms 6236 KB Output is correct
48 Correct 77 ms 6236 KB Output is correct
49 Correct 686 ms 6756 KB Output is correct
50 Correct 908 ms 8924 KB Output is correct
51 Correct 1568 ms 15132 KB Output is correct
52 Correct 2610 ms 15908 KB Output is correct
53 Correct 2948 ms 16240 KB Output is correct
54 Correct 3722 ms 16964 KB Output is correct
55 Correct 3961 ms 17232 KB Output is correct
56 Correct 4896 ms 17428 KB Output is correct
57 Correct 5627 ms 18664 KB Output is correct
58 Correct 5591 ms 18612 KB Output is correct
59 Correct 6267 ms 18952 KB Output is correct
60 Correct 6539 ms 19188 KB Output is correct