#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
int n;int m;
pair<pair<int,int>,int>ar[500005];
int freq[500005];
int freq2[500005];
int ans[500005];
int fans[500005];
void chk(){
for(int i=1;i<=m;i++){
if(ans[i]){
fans[ar[i].second]=1;
freq[ar[i].first.first]++;
freq[ar[i].first.second+1]--;
}else{
freq2[ar[i].first.first]++;
freq2[ar[i].first.second+1]--;
}
}
for(int i=1;i<=n;i++){
freq[i]+=freq[i-1];
freq2[i]+=freq2[i-1];
if(freq[i]==0||freq2[i]==0){
cout<<"impossible\n";exit(0);
}
}
}
signed main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>ar[i].first.first;
cin>>ar[i].first.second;
ar[i].second=i;
}
sort(ar+1,ar+m+1);
int stx=0;int sty=0;int xrr=1;
priority_queue<pair<int,int>>pq;
vector<int>vca;vector<int>vcb;
ar[m+1]={{n+10,n+10},0};
for(int i=1;i<=m+1;i++){
pair<int,int>pr=ar[i].first;
if(pr.first<=stx+1&&pr.second<=sty){
vca.push_back(i);
stx=max(stx,pr.second);
continue;
}
if(pr.second<=sty){
vcb.push_back(i);
if(pq.size()){
stx=max(stx,pq.top().first);
vca.push_back(pq.top().second);
pq.pop();
if(stx>sty){
swap(stx,sty);
swap(vca,vcb);
}
}else{
cout<<"impossible\n";return 0;
}
continue;
}
if(pr.first>stx+1){
sty=max(sty,pr.second);
vcb.push_back(i);
continue;
}
pq.push({pr.second,i});
}
for(int i=0;i<vca.size();i++){
ans[vca[i]]=1;
}
chk();
for(int i=1;i<=m;i++){
cout<<fans[i];
}
}
Compilation message
alternating.cpp: In function 'int main()':
alternating.cpp:80:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for(int i=0;i<vca.size();i++){
| ~^~~~~~~~~~~
alternating.cpp:46:25: warning: unused variable 'xrr' [-Wunused-variable]
46 | int stx=0;int sty=0;int xrr=1;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
'impossible' claimed, but there is a solution |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
'impossible' claimed, but there is a solution |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
'impossible' claimed, but there is a solution |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
38 ms |
9512 KB |
'impossible' claimed, but there is a solution |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
'impossible' claimed, but there is a solution |
2 |
Halted |
0 ms |
0 KB |
- |