#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define compress(v) sort(all(v)), v.erase(unique(all(v)), v.end())
using namespace std;
typedef long long ll;
struct Info{
int s, e, x;
Info() : Info(0, 0, 0) {}
Info(int s, int e, int x) : s(s), e(e), x(x) {}
bool operator < (const Info &t) const {
if(s != t.s) return s < t.s;
if(e != t.e) return e > t.e;
return x < t.x;
}
};
int n, m;
Info a[101010];
int ans[101010];
bool chk[101010][2];
void naive(){
for(int bit=0; bit<(1 << m); bit++){
memset(chk, 0, sizeof chk);
for(int i=1; i<=m; i++){
int j = i-1;
for(int k=a[i].s; ; k++){
if(k > n) k = 1;
chk[k][(bit >> j) & 1] = 1;
if(k == a[i].e) break;
}
}
int flag = 1;
for(int i=1; i<=n; i++) flag &= (chk[i][0] & chk[i][1]);
if(flag){
for(int i=0; i<m; i++) cout << ((bit >> i) & 1);
return;
}
}
cout << "impossible";
}
vector<int> big;
int par[1010];
int find(int v){ return v == par[v] ? v : par[v] = find(par[v]); }
void partition(){
vector<Info> v; int ed = 0, idx = 0;
for(int i=1; i<=m; i++){
v.push_back(a[i]);
if(a[i].s > a[i].e){
ed = max(ed, a[i].e);
if(ed == a[i].e) idx = i;
v.back().e += n;
}
}
sort(all(v));
deque<Info> dq;
iota(par, par+1010, 0);
for(auto i : v){
if(dq.empty() || dq.back().e < i.e) dq.push_back(i);
else par[i.x] = dq.back().x;
}
while(dq.size() && dq.front().e <= ed) par[dq.front().x] = idx, dq.pop_front();
for(auto i : dq) big.push_back(i.x);
compress(big);
for(int i=1; i<=m; i++) par[i] = find(par[i]);
}
int even(){
int pv = 0;
for(auto i : big){
ans[i] = (pv & 1); pv++;
}
for(int i=1; i<=m; i++){
if(i == par[i]) continue;
ans[i] = !ans[par[i]];
}
for(int i=1; i<=m; i++){
int s = a[i].s, e = a[i].e;
for(int j=s; ; j++){
if(j > n) j = 1;
chk[j][ans[i]] = 1;
if(j == e) break;
}
}
for(int i=1; i<=n; i++){
if(!chk[i][0] || !chk[i][1]) return 0;
}
for(int i=1; i<=m; i++) cout << ans[i];
return 1;
}
void subtask55(){
partition();
if(even()) return;
big.push_back(big[0]);
big.erase(big.begin());
memset(chk, 0, sizeof chk);
if(!even()) cout << "impossible";
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> m;
for(int i=1; i<=m; i++){ cin >> a[i].s >> a[i].e; a[i].x = i; }
if(n <= 15 && m <= 15){ naive(); return 0; }
if(n <= 1000 && m <= 1000){ subtask55(); return 0; }
sort(a+1, a+m+1);
int cnt0 = 0, cnt1 = 0;
for(int i=1; i<=m; i++){
int s = a[i].s, e = a[i].e, x = a[i].x;
if(cnt0 < cnt1){
if(cnt0 >= s-1) cnt0 = max(cnt0, e), ans[x] = 0;
}else{
if(cnt1 >= s-1) cnt1 = max(cnt1, e), ans[x] = 1;
}
}
if(cnt0 < n || cnt1 < n){
cout << "impossible"; return 0;
}
for(int i=1; i<=m; i++) cout << ans[i];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1664 KB |
Output is correct |
2 |
Correct |
1 ms |
1664 KB |
Output is correct |
3 |
Correct |
1 ms |
1664 KB |
Output is correct |
4 |
Correct |
2 ms |
1664 KB |
Output is correct |
5 |
Correct |
1 ms |
1664 KB |
Output is correct |
6 |
Correct |
1 ms |
1664 KB |
Output is correct |
7 |
Correct |
1 ms |
1664 KB |
Output is correct |
8 |
Correct |
2 ms |
1664 KB |
Output is correct |
9 |
Correct |
1 ms |
1664 KB |
Output is correct |
10 |
Correct |
4 ms |
1664 KB |
Output is correct |
11 |
Correct |
2 ms |
1664 KB |
Output is correct |
12 |
Correct |
2 ms |
1664 KB |
Output is correct |
13 |
Correct |
13 ms |
1664 KB |
Output is correct |
14 |
Correct |
1 ms |
1664 KB |
Output is correct |
15 |
Correct |
1 ms |
1664 KB |
Output is correct |
16 |
Correct |
25 ms |
1664 KB |
Output is correct |
17 |
Correct |
4 ms |
1664 KB |
Output is correct |
18 |
Correct |
4 ms |
1792 KB |
Output is correct |
19 |
Correct |
1 ms |
1664 KB |
Output is correct |
20 |
Correct |
1 ms |
1664 KB |
Output is correct |
21 |
Correct |
1 ms |
1664 KB |
Output is correct |
22 |
Correct |
21 ms |
1664 KB |
Output is correct |
23 |
Correct |
1 ms |
1664 KB |
Output is correct |
24 |
Correct |
3 ms |
1664 KB |
Output is correct |
25 |
Correct |
1 ms |
1664 KB |
Output is correct |
26 |
Correct |
1 ms |
1664 KB |
Output is correct |
27 |
Correct |
24 ms |
1664 KB |
Output is correct |
28 |
Correct |
1 ms |
1664 KB |
Output is correct |
29 |
Correct |
2 ms |
1664 KB |
Output is correct |
30 |
Correct |
2 ms |
1792 KB |
Output is correct |
31 |
Correct |
1 ms |
1664 KB |
Output is correct |
32 |
Correct |
1 ms |
1664 KB |
Output is correct |
33 |
Correct |
1 ms |
1664 KB |
Output is correct |
34 |
Correct |
1 ms |
1664 KB |
Output is correct |
35 |
Correct |
201 ms |
1664 KB |
Output is correct |
36 |
Correct |
8 ms |
1664 KB |
Output is correct |
37 |
Correct |
2 ms |
1664 KB |
Output is correct |
38 |
Correct |
215 ms |
1780 KB |
Output is correct |
39 |
Correct |
2 ms |
1664 KB |
Output is correct |
40 |
Correct |
2 ms |
1664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1664 KB |
Output is correct |
2 |
Correct |
1 ms |
1664 KB |
Output is correct |
3 |
Correct |
1 ms |
1664 KB |
Output is correct |
4 |
Correct |
2 ms |
1664 KB |
Output is correct |
5 |
Correct |
1 ms |
1664 KB |
Output is correct |
6 |
Correct |
1 ms |
1664 KB |
Output is correct |
7 |
Correct |
1 ms |
1664 KB |
Output is correct |
8 |
Correct |
2 ms |
1664 KB |
Output is correct |
9 |
Correct |
1 ms |
1664 KB |
Output is correct |
10 |
Correct |
4 ms |
1664 KB |
Output is correct |
11 |
Correct |
2 ms |
1664 KB |
Output is correct |
12 |
Correct |
2 ms |
1664 KB |
Output is correct |
13 |
Correct |
13 ms |
1664 KB |
Output is correct |
14 |
Correct |
1 ms |
1664 KB |
Output is correct |
15 |
Correct |
1 ms |
1664 KB |
Output is correct |
16 |
Correct |
25 ms |
1664 KB |
Output is correct |
17 |
Correct |
4 ms |
1664 KB |
Output is correct |
18 |
Correct |
4 ms |
1792 KB |
Output is correct |
19 |
Correct |
1 ms |
1664 KB |
Output is correct |
20 |
Correct |
1 ms |
1664 KB |
Output is correct |
21 |
Correct |
1 ms |
1664 KB |
Output is correct |
22 |
Correct |
21 ms |
1664 KB |
Output is correct |
23 |
Correct |
1 ms |
1664 KB |
Output is correct |
24 |
Correct |
3 ms |
1664 KB |
Output is correct |
25 |
Correct |
1 ms |
1664 KB |
Output is correct |
26 |
Correct |
1 ms |
1664 KB |
Output is correct |
27 |
Correct |
24 ms |
1664 KB |
Output is correct |
28 |
Correct |
1 ms |
1664 KB |
Output is correct |
29 |
Correct |
2 ms |
1664 KB |
Output is correct |
30 |
Correct |
2 ms |
1792 KB |
Output is correct |
31 |
Correct |
1 ms |
1664 KB |
Output is correct |
32 |
Correct |
1 ms |
1664 KB |
Output is correct |
33 |
Correct |
1 ms |
1664 KB |
Output is correct |
34 |
Correct |
1 ms |
1664 KB |
Output is correct |
35 |
Correct |
201 ms |
1664 KB |
Output is correct |
36 |
Correct |
8 ms |
1664 KB |
Output is correct |
37 |
Correct |
2 ms |
1664 KB |
Output is correct |
38 |
Correct |
215 ms |
1780 KB |
Output is correct |
39 |
Correct |
2 ms |
1664 KB |
Output is correct |
40 |
Correct |
2 ms |
1664 KB |
Output is correct |
41 |
Correct |
1 ms |
1536 KB |
Output is correct |
42 |
Incorrect |
1 ms |
1664 KB |
'impossible' claimed, but there is a solution |
43 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1664 KB |
Output is correct |
2 |
Correct |
1 ms |
1664 KB |
Output is correct |
3 |
Correct |
1 ms |
1664 KB |
Output is correct |
4 |
Correct |
2 ms |
1664 KB |
Output is correct |
5 |
Correct |
1 ms |
1664 KB |
Output is correct |
6 |
Correct |
1 ms |
1664 KB |
Output is correct |
7 |
Correct |
1 ms |
1664 KB |
Output is correct |
8 |
Correct |
2 ms |
1664 KB |
Output is correct |
9 |
Correct |
1 ms |
1664 KB |
Output is correct |
10 |
Correct |
4 ms |
1664 KB |
Output is correct |
11 |
Correct |
2 ms |
1664 KB |
Output is correct |
12 |
Correct |
2 ms |
1664 KB |
Output is correct |
13 |
Correct |
13 ms |
1664 KB |
Output is correct |
14 |
Correct |
1 ms |
1664 KB |
Output is correct |
15 |
Correct |
1 ms |
1664 KB |
Output is correct |
16 |
Correct |
25 ms |
1664 KB |
Output is correct |
17 |
Correct |
4 ms |
1664 KB |
Output is correct |
18 |
Correct |
4 ms |
1792 KB |
Output is correct |
19 |
Correct |
1 ms |
1664 KB |
Output is correct |
20 |
Correct |
1 ms |
1664 KB |
Output is correct |
21 |
Correct |
1 ms |
1664 KB |
Output is correct |
22 |
Correct |
21 ms |
1664 KB |
Output is correct |
23 |
Correct |
1 ms |
1664 KB |
Output is correct |
24 |
Correct |
3 ms |
1664 KB |
Output is correct |
25 |
Correct |
1 ms |
1664 KB |
Output is correct |
26 |
Correct |
1 ms |
1664 KB |
Output is correct |
27 |
Correct |
24 ms |
1664 KB |
Output is correct |
28 |
Correct |
1 ms |
1664 KB |
Output is correct |
29 |
Correct |
2 ms |
1664 KB |
Output is correct |
30 |
Correct |
2 ms |
1792 KB |
Output is correct |
31 |
Correct |
1 ms |
1664 KB |
Output is correct |
32 |
Correct |
1 ms |
1664 KB |
Output is correct |
33 |
Correct |
1 ms |
1664 KB |
Output is correct |
34 |
Correct |
1 ms |
1664 KB |
Output is correct |
35 |
Correct |
201 ms |
1664 KB |
Output is correct |
36 |
Correct |
8 ms |
1664 KB |
Output is correct |
37 |
Correct |
2 ms |
1664 KB |
Output is correct |
38 |
Correct |
215 ms |
1780 KB |
Output is correct |
39 |
Correct |
2 ms |
1664 KB |
Output is correct |
40 |
Correct |
2 ms |
1664 KB |
Output is correct |
41 |
Correct |
1 ms |
1536 KB |
Output is correct |
42 |
Incorrect |
1 ms |
1664 KB |
'impossible' claimed, but there is a solution |
43 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
2048 KB |
Output is correct |
2 |
Correct |
2 ms |
1536 KB |
Output is correct |
3 |
Correct |
13 ms |
1536 KB |
Output is correct |
4 |
Correct |
18 ms |
1792 KB |
Output is correct |
5 |
Correct |
36 ms |
2048 KB |
Output is correct |
6 |
Correct |
31 ms |
1912 KB |
Output is correct |
7 |
Correct |
33 ms |
1920 KB |
Output is correct |
8 |
Correct |
2 ms |
1536 KB |
Output is correct |
9 |
Correct |
1 ms |
1536 KB |
Output is correct |
10 |
Correct |
37 ms |
2040 KB |
Output is correct |
11 |
Correct |
27 ms |
1912 KB |
Output is correct |
12 |
Correct |
32 ms |
1912 KB |
Output is correct |
13 |
Correct |
1 ms |
1536 KB |
Output is correct |
14 |
Correct |
1 ms |
1536 KB |
Output is correct |
15 |
Correct |
36 ms |
2040 KB |
Output is correct |
16 |
Correct |
14 ms |
1664 KB |
Output is correct |
17 |
Correct |
36 ms |
2040 KB |
Output is correct |
18 |
Correct |
35 ms |
2040 KB |
Output is correct |
19 |
Correct |
3 ms |
1536 KB |
Output is correct |
20 |
Correct |
39 ms |
2040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1664 KB |
Output is correct |
2 |
Correct |
1 ms |
1664 KB |
Output is correct |
3 |
Correct |
1 ms |
1664 KB |
Output is correct |
4 |
Correct |
2 ms |
1664 KB |
Output is correct |
5 |
Correct |
1 ms |
1664 KB |
Output is correct |
6 |
Correct |
1 ms |
1664 KB |
Output is correct |
7 |
Correct |
1 ms |
1664 KB |
Output is correct |
8 |
Correct |
2 ms |
1664 KB |
Output is correct |
9 |
Correct |
1 ms |
1664 KB |
Output is correct |
10 |
Correct |
4 ms |
1664 KB |
Output is correct |
11 |
Correct |
2 ms |
1664 KB |
Output is correct |
12 |
Correct |
2 ms |
1664 KB |
Output is correct |
13 |
Correct |
13 ms |
1664 KB |
Output is correct |
14 |
Correct |
1 ms |
1664 KB |
Output is correct |
15 |
Correct |
1 ms |
1664 KB |
Output is correct |
16 |
Correct |
25 ms |
1664 KB |
Output is correct |
17 |
Correct |
4 ms |
1664 KB |
Output is correct |
18 |
Correct |
4 ms |
1792 KB |
Output is correct |
19 |
Correct |
1 ms |
1664 KB |
Output is correct |
20 |
Correct |
1 ms |
1664 KB |
Output is correct |
21 |
Correct |
1 ms |
1664 KB |
Output is correct |
22 |
Correct |
21 ms |
1664 KB |
Output is correct |
23 |
Correct |
1 ms |
1664 KB |
Output is correct |
24 |
Correct |
3 ms |
1664 KB |
Output is correct |
25 |
Correct |
1 ms |
1664 KB |
Output is correct |
26 |
Correct |
1 ms |
1664 KB |
Output is correct |
27 |
Correct |
24 ms |
1664 KB |
Output is correct |
28 |
Correct |
1 ms |
1664 KB |
Output is correct |
29 |
Correct |
2 ms |
1664 KB |
Output is correct |
30 |
Correct |
2 ms |
1792 KB |
Output is correct |
31 |
Correct |
1 ms |
1664 KB |
Output is correct |
32 |
Correct |
1 ms |
1664 KB |
Output is correct |
33 |
Correct |
1 ms |
1664 KB |
Output is correct |
34 |
Correct |
1 ms |
1664 KB |
Output is correct |
35 |
Correct |
201 ms |
1664 KB |
Output is correct |
36 |
Correct |
8 ms |
1664 KB |
Output is correct |
37 |
Correct |
2 ms |
1664 KB |
Output is correct |
38 |
Correct |
215 ms |
1780 KB |
Output is correct |
39 |
Correct |
2 ms |
1664 KB |
Output is correct |
40 |
Correct |
2 ms |
1664 KB |
Output is correct |
41 |
Correct |
1 ms |
1536 KB |
Output is correct |
42 |
Incorrect |
1 ms |
1664 KB |
'impossible' claimed, but there is a solution |
43 |
Halted |
0 ms |
0 KB |
- |