Submission #377099

#TimeUsernameProblemLanguageResultExecution timeMemory
377099couplefireBridges (APIO19_bridges)C++17
100 / 100
2111 ms17192 KiB
#include <bits/stdc++.h> using namespace std; const int sz=1<<10; int pt=sz; char buff[sz]; inline char readchar(){ if(pt==sz){ fread(buff,1,sz,stdin); pt=0; } return buff[pt++]; } #define getchar readchar #define in ({int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';n?-x:x;}) inline void out(int x){ if(x>9) out(x/10); putchar(x%10+'0'); } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int rnd(int l,int r){return l+rng()%(r-l+1);} #define fasty ios_base::sync_with_stdio(false),cin.tie(nullptr); #define task "B" #define forinc(a,b,c) for(int a=b,_c=c;a<=_c;++a) #define fordec(a,b,c) for(int a=b,_c=c;a>=_c;--a) #define forv(a,b) for(auto&a:b) #define fi first #define se second #define pb push_back #define ii pair<int,int> #define mt make_tuple #define all(a) a.begin(),a.end() #define reset(f, x) memset(f, x, sizeof(f)) #define bit(x,i) ((x>>(i-1))&1) #define on(x,i) (x|(1ll<<(i-1))) #define off(x,i) (x&~(1<<(i-1))) const int N=100010; struct ed{ int u,v,c,i; }e[N]; struct qr{ int o,u,x,i; }q[N]; struct dt{ int x,y,X,Y; }; stack<dt> st; int n,m,t; int T=1000,mode; int id[N],b[N],ans[N]; vector<int> mm[N]; inline int root(int x){ return id[x]<0?x:root(id[x]); } inline void join(int x,int y){ if((x=root(x))==(y=root(y))) return; if(mode){ st.push({x,y,id[x],id[y]}); } if(id[x]>id[y]) swap(x,y); id[x]+=id[y]; id[y]=x; } int nxt[N*2],cur[N]; main(){ //freopen(task".inp","r",stdin); //freopen(task".out","w",stdout); n=in; forinc(i,1,m=in) e[i]={in,in,in,i}; forinc(i,1,t=in) q[i]={in,in,in,i}; forinc(i,1,t){ if(q[i].o==1) mm[q[i].u].pb(i); b[i]=(i-1)/T+1; } forinc(i,1,m) cur[i]=t+1; fordec(i,t,1) if(q[i].o==1){ nxt[i]=cur[q[i].u]; cur[q[i].u]=i; } forinc(i,1,m){ nxt[i+t+1]=cur[i]; cur[i]=i+t+1; } forinc(i,1,t){ if(b[i]==b[i-1]) continue; vector<ed> E; vector<qr> Q; reset(id,-1); int j=i,l=i,r=b[i]*T > t ? t : b[i]*T; while(j<=t && b[j]==b[i]){ if(q[j].o==2) Q.pb(q[j]); j++; } sort(all(Q),[](qr i,qr j){ return i.x>j.x; }); forinc(j,1,m){ if(nxt[cur[j]]>r) E.pb(e[j]); } sort(all(E),[](ed i,ed j){ return i.c>j.c; }); int k=0; forinc(j,0,Q.size()-1){ while(k<E.size() && E[k].c>=Q[j].x){ join(E[k].u,E[k].v); k++; } mode=1; forinc(k,l,r) if(q[k].o==1){ if(nxt[cur[q[k].u]]>r) continue; if(k<Q[j].i && nxt[k]>Q[j].i && q[k].x>=Q[j].x){ join(e[q[k].u].u,e[q[k].u].v); continue; } if(nxt[cur[q[k].u]]>Q[j].i && e[q[k].u].c>=Q[j].x){ join(e[q[k].u].u,e[q[k].u].v); } } mode=0; ans[Q[j].i]=-id[root(Q[j].u)]; while(st.size()){ auto x=st.top(); st.pop(); id[x.x]=x.X; id[x.y]=x.Y; } } forinc(j,l,r) if(q[j].o==1){ e[q[j].u].c=q[j].x; cur[q[j].u]=j; } } forinc(i,1,t) if(q[i].o==2) out(ans[i]),putchar('\n'); }

Compilation message (stderr)

bridges.cpp:72:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   72 | main(){
      |      ^
bridges.cpp: In function 'int main()':
bridges.cpp:114:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<ed>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |             while(k<E.size() && E[k].c>=Q[j].x){
      |                   ~^~~~~~~~~
bridges.cpp: In function 'char readchar()':
bridges.cpp:8:14: warning: ignoring return value of 'size_t fread(void*, size_t, size_t, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
    8 |         fread(buff,1,sz,stdin);
      |         ~~~~~^~~~~~~~~~~~~~~~~
#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...