Submission #383045

#TimeUsernameProblemLanguageResultExecution timeMemory
383045KerimBridges (APIO19_bridges)C++17
100 / 100
2898 ms112508 KiB
#include "bits/stdc++.h" #define MAXN 100009 #define INF 1000000007 #define mp(x,y) make_pair(x,y) #define all(v) v.begin(),v.end() #define pb(x) push_back(x) #define wr cout<<"----------------"<<endl; #define ppb() pop_back() #define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++) #define ff first #define ss second #define my_little_dodge 46 #define debug(x) cerr<< #x <<" = "<< x<<endl; using namespace std; typedef long long ll; typedef pair<int,int> PII; template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;} template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;} const int BLOK=750; pair<PII,int>edge[MAXN]; pair<int,PII>query[MAXN]; int ans[MAXN],ata[MAXN],sz[MAXN]; bool ban[MAXN],ban1[MAXN]; stack<int>st; vector<pair<PII,int> >history; int tap(int x){ if(ata[x]==x)return x; return tap(ata[x]); } bool merge(PII v){ int x=tap(v.ff),y=tap(v.ss); if(x==y)return 0; if(x<y)swap(x,y); history.pb(mp(mp(x,y),sz[y])); ata[y]=x;sz[x]+=sz[y]; return 1; } int main(){ //freopen("file.in", "r", stdin); int n,m; scanf("%d%d",&n,&m); set<PII>edges; for(int i=1;i<=m;i++){ int u,v,w; scanf("%d%d%d",&u,&v,&w); edge[i]=mp(mp(u,v),w); edges.insert(mp(w,i)); } int q; scanf("%d",&q); for(int i=1;i<=q;i++){ int t,a,b; scanf("%d%d%d",&t,&a,&b); query[i]=mp(t,mp(a,b)); } for(int i=1;i<=q;i+=BLOK){ memset(ban,0,sizeof ban); for(int i=1;i<=n;i++)ata[i]=i,sz[i]=1; vector<PII>v,g;int r=min(i+BLOK-1,q); for(int j=i;j<=r;j++){ if(query[j].ff==1) ban[query[j].ss.ff]=j; else g.pb(mp(query[j].ss.ss,j)); } tr(it,edges) if(!ban[it->ss]) v.pb(*it); sort(all(g)); int p=int(v.size())-1,rec; for(int j=int(g.size())-1,ind,k;j>=0;j--){ rec=0; while(p>=0 and v[p].ff>=g[j].ff) merge(edge[v[p].ss].ff),p--; for(k=g[j].ss-1;k>=i;k--){ ind=query[k].ss.ff; if(query[k].ff==1 and !ban1[ind]){ if(query[k].ss.ss>=g[j].ff)rec+=merge(edge[ind].ff); ban1[ind]=1;st.push(ind); } } for(k=g[j].ss+1;k<=r;k++){ ind=query[k].ss.ff; if(query[k].ff==1 and !ban1[ind]){ if(edge[ind].ss>=g[j].ff)rec+=merge(edge[ind].ff); ban1[ind]=1;st.push(ind); } } ans[g[j].ss]=sz[tap(query[g[j].ss].ss.ff)]; while(!st.empty())ban1[st.top()]=0,st.pop(); while(rec--){ //x,y,sz[y] int y=history.back().ff.ss; sz[history.back().ff.ff]-=history.back().ss;ata[y]=y; history.ppb(); } } for(int j=i,a;j<=r;j++) if(query[j].ff==1){ a=query[j].ss.ff; edges.erase(mp(edge[a].ss,a)); edge[a].ss=query[j].ss.ss; edges.insert(mp(edge[a].ss,a)); } } for(int i=1;i<=q;i++) if(query[i].ff==2) printf("%d\n",ans[i]); return 0; }

Compilation message (stderr)

bridges.cpp: In function 'int main()':
bridges.cpp:105:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  105 |     for(int i=1;i<=q;i++)
      |     ^~~
bridges.cpp:108:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  108 |  return 0;
      |  ^~~~~~
bridges.cpp:41:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   41 |     scanf("%d%d",&n,&m);
      |     ~~~~~^~~~~~~~~~~~~~
bridges.cpp:45:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   45 |   scanf("%d%d%d",&u,&v,&w);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~
bridges.cpp:50:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   50 |     scanf("%d",&q);
      |     ~~~~~^~~~~~~~~
bridges.cpp:53:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   53 |   scanf("%d%d%d",&t,&a,&b);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...