#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define sz(a) (int)a.size()
const int N = 1e5 + 10;
int t[4 * N], lazy[4 * N], n, m, ans[N], a[N], b[N];
vector<int> adj[N];
void push(int i, int l, int r) {
if(!lazy[i]) return;
t[i] += lazy[i];
if(l != r) {
lazy[2 * i] += lazy[i];
lazy[2 * i + 1] += lazy[i];
}
lazy[i] = 0;
}
void modif(int i, int l, int r, int tl, int tr, int val) {
push(i, l, r);
if(l > tr || r < tl) return;
if(l >= tl && r <= tr) {
lazy[i] += val;
push(i, l, r);
return;
}
int mid = l + r >> 1;
modif(2 * i, l, mid, tl, tr, val);
modif(2 * i + 1, mid + 1, r, tl, tr, val);
t[i] = min(t[2 * i], t[2 * i + 1]);
}
int query(int i, int l, int r, int tl, int tr) {
push(i, l, r);
if(l > tr || r < tl) return 1e9;
if(l >= tl && r <= tr) return t[i];
int mid = l + r >> 1;
return min(query(2 * i, l, mid, tl, tr), query(2 * i + 1, mid + 1, r, tl, tr));
}
void modif(int l, int r, int val) {
if(l <= r) modif(1, 1, n, l, r, val);
else {
modif(1, 1, n, l, n, val);
modif(1, 1, n, 1, r, val);
}
}
int query(int l, int r) {
if(l <= r) return query(1, 1, n, l, r);
else {
return min(query(1, 1, n, l, n), query(1, 1, n, 1, r));
}
}
bool dfs(int u) {
for(int v: adj[u]) {
if(ans[v] == 0) {
ans[v] = -ans[u];
if(!dfs(v)) return false;
} else if(ans[v] == ans[u]) return false;
}
return true;
}
void solve() {
cin >> n >> m;
set<int> ongoing;
vector<pair<int, int>> events;
for(int i = 1; i <= m; ++i) {
cin >> a[i] >> b[i];
if(a[i] > b[i]) ongoing.insert(i);
events.pb({a[i], i});
events.pb({b[i] + 1, -i});
}
for(int i = 1, j = 0; i <= n; ++i) {
while(j < sz(events) && events[j].first == i) {
if(events[j].second < 0) ongoing.erase(-events[j].second);
else ongoing.insert(events[j].second);
++j;
}
if(sz(ongoing) < 2) {
cout << "impossible\n";
return;
}
if(sz(ongoing) == 2) {
int A = *ongoing.begin(), B = *++ongoing.begin();
adj[A].pb(B);
adj[B].pb(A);
}
}
for(int i = 1; i <= m; ++i) {
if(ans[i] == 0 && sz(adj[i])) {
ans[i] = -1;
if(!dfs(i)) {
cout << "impossible\n";
return;
}
}
}
for(int i = 1; i <= m; ++i) {
if(ans[i] >= 0) modif(a[i], b[i], 1);
}
for(int i = 1; i <= m; ++i) {
if(ans[i] == 0) {
if(query(a[i], b[i]) >= 2) {
ans[i] = -1;
modif(a[i], b[i], -1);
} else {
ans[i] = 1;
}
}
}
for(int i = 1; i <= m; ++i) {
cout << (ans[i] == -1 ? 0 : 1);
}
}
int32_t main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t = 1;
//cin >> t;
while(t--) {
solve();
}
}
Compilation message
alternating.cpp: In function 'void modif(int, int, int, int, int, int)':
alternating.cpp:32:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
32 | int mid = l + r >> 1;
| ~~^~~
alternating.cpp: In function 'int query(int, int, int, int, int)':
alternating.cpp:41:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
41 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
'impossible' claimed, but there is a solution |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
'impossible' claimed, but there is a solution |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
'impossible' claimed, but there is a solution |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
17 ms |
6108 KB |
'impossible' claimed, but there is a solution |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
'impossible' claimed, but there is a solution |
2 |
Halted |
0 ms |
0 KB |
- |