Submission #255734

# Submission time Handle Problem Language Result Execution time Memory
255734 2020-08-01T18:53:59 Z mehrdad_sohrabi Street Lamps (APIO19_street_lamps) C++14
0 / 100
23 ms 7532 KB
#include <bits/stdc++.h>
typedef int ll;
typedef long double ld;
#define pb push_back
#define pii pair < int , int >
#define F first
#define S second
//#define endl '\n'
//#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout<<x<<'\n', 0;
using namespace std;
const int N=1e5+100,sq=1;
vector <pair <int,pii > > yall,yy;
ll vis[N];
ll par[N],sz[N];
map <pii,int> mp;
ll getpar(ll v){
    if (par[v]==v) return v;
    return par[v]=getpar(par[v]);
}
inline void mrg(ll v,ll u){
    ll u1=getpar(u),v1=getpar(v);
    if (u1==v1) return ;
    if (sz[u1]>sz[v1])swap(u1,v1);
    par[u1]=v1;
    sz[v1]+=sz[u1];
}
ll pp[N],zz[N];
ll gg(ll v){
    if (pp[v]==v) return v;
    return pp[v]=gg(pp[v]);
}
inline void merg(ll v,ll u){
    ll v1=gg(v),u1=gg(u);
    if (v1==u1) return ;
    if (zz[u1]>zz[v1]) swap(v1,u1);
    pp[u1]=v1;
    zz[v1]+=zz[u1];
}
ll ans[N];
vector <pair <int,pii> > q;
int32_t main(){
    sync;
    ll n,m;
    cin >> n >> m;
    for (int i=1;i<=m;i++){
        ll u,v,w;
        cin >> u >> v >> w;
        yall.pb({w,{u,v}});
    }
    yy=yall;
    sort(yall.begin(),yall.end());
    reverse(yall.begin(),yall.end());
    ll q1;
    cin >> q1;
    while(q1--){
        ll u,v,w;
        cin >> u >> v >> w;
        q.pb({u,{v,w}});
    }
    for (int i=0;i<q.size();i+=sq){
        memset(vis,0,sizeof vis);
        vector <pii> g;
        vector <pair <int,pii> > yal;
        for (int j=i;j<min(i+sq,(int)q.size());j++){
            if (q[j].F==1) vis[q[j].S.F-1]=1;
            else{
                g.pb({q[j].S.S,j});
            }
        }
        memset(par,0,sizeof par);
        memset(sz,0,sizeof sz);
        memset(zz,0,sizeof zz);
        for (int j=1;j<=n;j++){
            sz[j]=1;
            par[j]=j;
        }
        memset(pp,0,sizeof pp);
        for (int j=0;j<yy.size();j++){
            if (vis[j]) continue;
            yal.pb(yy[j]);
        }
        sort(yal.begin(),yal.end());
        reverse(yal.begin(),yal.end());
        sort(g.begin(),g.end());
        reverse(g.begin(),g.end());
        ll cnt=0;
        /// bug : momkene ye yal baadan hazf she ///
        for (int j=0;j<yal.size();j++){
            while(cnt<g.size() && g[cnt].F>yal[j].F){
                for (int k=i;k<min(i+sq,(ll)q.size());k++){
                    if (q[k].F==1){
                        ll v=yy[q[k].S.F-1].S.F,u=yy[q[k].S.F-1].S.S;
                        pp[v]=getpar(v);
                        pp[u]=getpar(u);
                        zz[pp[v]]=sz[pp[v]];
                        zz[pp[u]]=sz[pp[u]];
                        pp[pp[v]]=pp[v];
                        pp[pp[u]]=pp[u];
                    }
                    else{
                        ll v=q[k].S.F;
                        pp[v]=getpar(v);
                        zz[pp[v]]=sz[pp[v]];
                        pp[pp[v]]=pp[v];
                    }
                }
                mp.clear();
                ll id=g[cnt].S;
                for (int k=id;k>=i;k--){
                    if (q[k].F==2) continue;
                    ll v=yy[q[k].S.F-1].S.F,u=yy[q[k].S.F-1].S.S;
                    if (mp[{u,v}]) continue;
                    mp[{u,v}]=1;
                    if (q[k].S.S>=g[cnt].F){
                        merg(u,v);
                    }
                }
                for (int k=id+1;k<min(i+sq,(ll)q.size());k++){
                    if (q[k].F==2) continue;
                    ll v=yy[q[k].S.F-1].S.F,u=yy[q[k].S.F-1].S.S;
                    if (mp[{u,v}]) continue;
                    mp[{u,v}]=1;
                    if (yy[q[k].S.F-1].F>=g[cnt].F){
                        merg(u,v);
                    }
                }
                ans[id]=zz[gg(q[id].S.F)];
                cnt++;
            }

            mrg(yal[j].S.F,yal[j].S.S);
        }
        while(cnt<g.size()){
                for (int k=i;k<min(i+sq,(ll)q.size());k++){
                    if (q[k].F==1){
                        ll v=yy[q[k].S.F-1].S.F,u=yy[q[k].S.F-1].S.S;
                        pp[v]=getpar(v);
                        pp[u]=getpar(u);
                        zz[pp[v]]=sz[pp[v]];
                        zz[pp[u]]=sz[pp[u]];
                        pp[pp[u]]=pp[u];
                        pp[pp[v]]=pp[v];
                    }
                    else{
                        ll v=q[k].S.F;
                        pp[v]=getpar(v);
                        zz[pp[v]]=sz[pp[v]];
                        pp[pp[v]]=pp[v];
                    }
                }
                mp.clear();
                ll id=g[cnt].S;
                for (int k=id;k>=i;k--){
                    if (q[k].F==2) continue;
                    ll v=yy[q[k].S.F-1].S.F,u=yy[q[k].S.F-1].S.S;
                    if (mp[{u,v}]) continue;
                    mp[{u,v}]=1;
                    if (q[k].S.S>=g[cnt].F){
                        merg(u,v);
                    }
                }
                for (int k=id+1;k<min(i+sq,(ll)q.size());k++){
                    if (q[k].F==2) continue;
                    ll v=yy[q[k].S.F-1].S.F,u=yy[q[k].S.F-1].S.S;
                    if (mp[{u,v}]) continue;
                    mp[{u,v}]=1;
                    if (yy[q[k].S.F-1].F>=g[cnt].F){
                        merg(u,v);
                    }
                }
                ans[id]=zz[gg(q[id].S.F)];
                cnt++;
        }
        for (int j=i;j<(min(i+sq,(ll)q.size()));j++){
            if (q[j].F==1){
                yy[q[j].S.F-1].F=q[j].S.S;
            }
        }
    }
    for (int i=0;i<q.size();i++){
        if (q[i].F==2)
        cout << ans[i] << endl;
    }
}



Compilation message

street_lamps.cpp: In function 'int32_t main()':
street_lamps.cpp:63:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i=0;i<q.size();i+=sq){
                  ~^~~~~~~~~
street_lamps.cpp:81:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j=0;j<yy.size();j++){
                      ~^~~~~~~~~~
street_lamps.cpp:91:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j=0;j<yal.size();j++){
                      ~^~~~~~~~~~~
street_lamps.cpp:92:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while(cnt<g.size() && g[cnt].F>yal[j].F){
                   ~~~^~~~~~~~~
street_lamps.cpp:136:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while(cnt<g.size()){
               ~~~^~~~~~~~~
street_lamps.cpp:183:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i=0;i<q.size();i++){
                  ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 23 ms 7532 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -