#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];
pair<int,int> seg[2*pot+7];
void ins(int v,int x,int i)
{
v+=pot-1;
seg[v]=max(seg[v],{x,i});
while(v>1)
{
v/=2;
seg[v]=max(seg[2*v],seg[2*v+1]);
}
}
pair<int,int>que(int v,int a,int b,int l,int r)
{
if(a<=l&&b>=r) return seg[v];
if(r<a||l>b) return {-inf,0};
return max(que(2*v,a,b,l,(l+r)/2),que(2*v+1,a,b,(l+r)/2+1,r));
}
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;
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;
}
set<pair<int,int>>is;
is.insert({inf,0});
for(int i=1;i<=m;i++)
{
if(l[i]<=r[i]) ins(l[i]+1,r[i],i);
else is.insert({l[i],i});
}
int a=0,b=r[k];
while(true)
{
if(b==n-1) break;
int x=L[b];
a=max(a,x+1);
if((*is.lower_bound({a,0})).st<=b+1)
{
ans[(*is.lower_bound({a,0})).nd]=1;
break;
}
pair<int,int>p=que(1,a+1,b+2,1,pot);
if(p.st<=b) gg();
a=b+1;
b=p.st;
ans[p.nd]=1;
}
for(int i=1;i<=m;i++) cout<<ans[i];
return 0;
}
Compilation message
alternating.cpp: In function 'int main()':
alternating.cpp:68:11: warning: 'k' may be used uninitialized in this function [-Wmaybe-uninitialized]
68 | ans[k]=1;
| ~~~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Incorrect |
0 ms |
340 KB |
no wires in direction 0 between segments 14 and 15 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Incorrect |
0 ms |
340 KB |
no wires in direction 0 between segments 14 and 15 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Incorrect |
0 ms |
340 KB |
no wires in direction 0 between segments 14 and 15 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
1548 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
9 ms |
1108 KB |
Output is correct |
4 |
Correct |
16 ms |
2368 KB |
Output is correct |
5 |
Incorrect |
30 ms |
3568 KB |
no wires in direction 0 between segments 31129 and 31130 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Incorrect |
0 ms |
340 KB |
no wires in direction 0 between segments 14 and 15 |
4 |
Halted |
0 ms |
0 KB |
- |