Submission #724943

#TimeUsernameProblemLanguageResultExecution timeMemory
724943Rafi22Alternating Current (BOI18_alternating)C++14
100 / 100
55 ms10476 KiB
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define ll long long ll mod=1000000007; int inf=1000000007; ll infl=1000000000000000007; void gg() { cout<<"impossible"; exit(0); } const int N=100007,pot=1<<17; bool ans[N]; int l[N]; int r[N]; int ile[N]; int L[N]; vector<int>V[N]; int o[N]; pair<int,int> seg[2*pot+7]; pair<int,int> que(int v) { v+=pot-1; pair<int,int>p={inf,0}; while(v>0) { p=min(p,seg[v]); v/=2; } return p; } void ins(int v,int a,int b,int l,int r,int x,int i) { if(a<=l&&b>=r) { seg[v]=min(seg[v], {x,i}); return ; } if(r<a||l>b) return ; ins(2*v,a,b,l,(l+r)/2,x,i); ins(2*v+1,a,b,(l+r)/2+1,r,x,i); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m; cin>>n>>m; int mx=0,k; for(int i=1;i<=m;i++) { cin>>l[i]>>r[i]; l[i]--; r[i]--; if((r[i]-l[i]+n)%n+1>mx) { mx=(r[i]-l[i]+n)%n+1; k=i; } } ans[k]=1; int t=l[k]; for(int i=1;i<=m;i++) { l[i]=(l[i]-t+n)%n; r[i]=(r[i]-t+n)%n; // cout<<l[i]<<" "<<r[i]<<endl; if(l[i]<=r[i]) { ile[l[i]]++; ile[r[i]+1]--; } else { ile[l[i]]++; ile[0]++; ile[r[i]+1]--; } } int act=0; L[0]=-1; for(int i=0;i<n;i++) { act+=ile[i]; if(act<2) gg(); if(i>0) L[i]=L[i-1]; if(act==2) L[i]=i; } for(int i=1;i<=m;i++) if(i!=k) V[l[i]].pb(i); for(int i=1;i<2*pot;i++) seg[i].st=inf; ins(1,L[r[k]]+2,r[k]+2,1,pot,r[k]+1,k); int X=-1; if(r[k]==n-1) X=k; for(int j=0;j<n;j++) { for(auto i:V[j]) { pair<int,int>p=que(l[i]+1); o[i]=p.nd; if(o[i]!=0&&r[i]==n-1) X=i; if(l[i]<=r[i]) ins(1,max(p.st,L[r[i]]+1)+1,r[i]+2,1,pot,r[i]+1,i); else { if(o[i]!=0&&L[r[i]]==-1) X=i; } } } if(X==-1) gg(); while(X>0) { ans[X]=1; X=o[X]; } for(int i=1;i<=m;i++) cout<<ans[i]; return 0; }

Compilation message (stderr)

alternating.cpp: In function 'int main()':
alternating.cpp:78:11: warning: 'k' may be used uninitialized in this function [-Wmaybe-uninitialized]
   78 |     ans[k]=1;
      |     ~~~~~~^~
#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...