#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <functional>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
typedef long long llong;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<llong, llong> pll;
void fail(int i) {
printf("impossible\n");
exit(0);
}
int n, m;
int cnt[100001];
vector<int> two;
int findPoint(int s, int e) {
if (s > e) return 0;
return *lower_bound(two.begin(), two.end(), s) <= e;
}
struct cur {
int a, b, l, i;
cur(int a, int b) : a(a), b(b) {}
cur() {}
void scan(int idx) {
i = idx;
scanf("%d%d", &a, &b);
if (a == b + 1) a = 1, b = n;
if (a <= b) {
if (b < n) --cnt[b + 1];
++cnt[a];
}
else {
++cnt[a];
++cnt[1];
--cnt[b + 1];
b += n;
}
l = b - a + 1;
}
bool operator<(const cur &p) const {
if (a != p.a) return a < p.a;
return l > p.l;
}
bool in(cur p) const {
if (n < p.b) return in(cur(p.a, n)) && in(cur(1, p.b - n));
if (n < b) return cur(a, n).in(p) || cur(1, b - n).in(p);
return a <= p.a && p.b <= b;
}
bool check(cur p) const {
if (n < p.b) return check(cur(p.a, n)) || check(cur(1, p.b - n));
if (n < b) return cur(a, n).check(p) || cur(1, b - n).check(p);
return findPoint(max(a, p.a), min(b, p.b));
}
} cs[100000];
int par[100000];
int find(int x) {
if (par[x] != -1) return par[x] = find(par[x]);
return x;
}
int ans[100000];
void printAnswer() {
for (int i = 0; i < m; ++i) {
printf("%d", ans[i]);
}
printf("\n");
exit(0);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; ++i) {
cs[i].scan(i);
par[i] = -1;
}
for (int i = 1; i <= n; ++i) {
cnt[i] += cnt[i - 1];
if (cnt[i] < 2) fail(0);
if (cnt[i] == 2) two.push_back(i);
}
two.push_back(3 * n);
sort(cs, cs + m);
for (int it = 1, pr = 0; it < (m << 1); ++it) {
int i = it % n;
if (i == pr) continue;
if (par[i] != -1) continue;
if (cs[pr].in(cs[i])) {
par[i] = pr;
}
else {
pr = i;
}
}
int c = 0;
for (int i = 0; i < m; ++i) {
if (par[i] == -1) ++c;
}
if (c > 2 && (c & 1)) {
for (int i = 0; i < m; ++i) {
if (par[i] != -1) continue;
int j = (i + 1) % m;
while (par[j] != -1) j = (j + 1) % m;
if (cs[i].check(cs[j])) continue;
c = 0;
for (int k = j; k != i; k = (k + 1) % m) {
if (par[k] != -1) continue;
ans[cs[k].i] = cs[k].l = (c ^= 1);
}
ans[cs[i].i] = cs[i].l = 1;
for (int k = 0; k < m; ++k) {
if (par[k] == -1) continue;
ans[cs[k].i] = cs[k].l = (cs[find(k)].l ^ 1);
}
printAnswer();
}
fail(1);
}
else {
c = 0;
for (int i = 0; i < m; ++i) {
if (par[i] != -1) continue;
ans[cs[i].i] = cs[i].l = (c ^= 1);
}
for (int i = 0; i < m; ++i) {
if (par[i] == -1) continue;
ans[cs[i].i] = cs[i].l = (cs[find(i)].l ^ 1);
}
printAnswer();
}
return 0;
}
Compilation message
alternating.cpp: In function 'int main()':
alternating.cpp:85:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~
alternating.cpp: In member function 'void cur::scan(int)':
alternating.cpp:40:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
432 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
516 KB |
Output is correct |
7 |
Correct |
2 ms |
516 KB |
Output is correct |
8 |
Correct |
3 ms |
516 KB |
Output is correct |
9 |
Correct |
3 ms |
516 KB |
Output is correct |
10 |
Correct |
2 ms |
516 KB |
Output is correct |
11 |
Correct |
2 ms |
516 KB |
Output is correct |
12 |
Correct |
3 ms |
516 KB |
Output is correct |
13 |
Correct |
3 ms |
644 KB |
Output is correct |
14 |
Correct |
2 ms |
644 KB |
Output is correct |
15 |
Correct |
3 ms |
644 KB |
Output is correct |
16 |
Correct |
3 ms |
644 KB |
Output is correct |
17 |
Correct |
3 ms |
644 KB |
Output is correct |
18 |
Correct |
2 ms |
644 KB |
Output is correct |
19 |
Correct |
3 ms |
644 KB |
Output is correct |
20 |
Correct |
3 ms |
644 KB |
Output is correct |
21 |
Correct |
3 ms |
644 KB |
Output is correct |
22 |
Correct |
3 ms |
644 KB |
Output is correct |
23 |
Correct |
2 ms |
644 KB |
Output is correct |
24 |
Correct |
3 ms |
644 KB |
Output is correct |
25 |
Correct |
3 ms |
644 KB |
Output is correct |
26 |
Correct |
2 ms |
644 KB |
Output is correct |
27 |
Correct |
2 ms |
644 KB |
Output is correct |
28 |
Correct |
3 ms |
644 KB |
Output is correct |
29 |
Correct |
3 ms |
644 KB |
Output is correct |
30 |
Correct |
2 ms |
644 KB |
Output is correct |
31 |
Correct |
2 ms |
644 KB |
Output is correct |
32 |
Correct |
2 ms |
644 KB |
Output is correct |
33 |
Correct |
3 ms |
644 KB |
Output is correct |
34 |
Correct |
2 ms |
740 KB |
Output is correct |
35 |
Correct |
2 ms |
740 KB |
Output is correct |
36 |
Correct |
2 ms |
740 KB |
Output is correct |
37 |
Correct |
2 ms |
740 KB |
Output is correct |
38 |
Correct |
2 ms |
740 KB |
Output is correct |
39 |
Correct |
3 ms |
740 KB |
Output is correct |
40 |
Correct |
5 ms |
740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
432 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
516 KB |
Output is correct |
7 |
Correct |
2 ms |
516 KB |
Output is correct |
8 |
Correct |
3 ms |
516 KB |
Output is correct |
9 |
Correct |
3 ms |
516 KB |
Output is correct |
10 |
Correct |
2 ms |
516 KB |
Output is correct |
11 |
Correct |
2 ms |
516 KB |
Output is correct |
12 |
Correct |
3 ms |
516 KB |
Output is correct |
13 |
Correct |
3 ms |
644 KB |
Output is correct |
14 |
Correct |
2 ms |
644 KB |
Output is correct |
15 |
Correct |
3 ms |
644 KB |
Output is correct |
16 |
Correct |
3 ms |
644 KB |
Output is correct |
17 |
Correct |
3 ms |
644 KB |
Output is correct |
18 |
Correct |
2 ms |
644 KB |
Output is correct |
19 |
Correct |
3 ms |
644 KB |
Output is correct |
20 |
Correct |
3 ms |
644 KB |
Output is correct |
21 |
Correct |
3 ms |
644 KB |
Output is correct |
22 |
Correct |
3 ms |
644 KB |
Output is correct |
23 |
Correct |
2 ms |
644 KB |
Output is correct |
24 |
Correct |
3 ms |
644 KB |
Output is correct |
25 |
Correct |
3 ms |
644 KB |
Output is correct |
26 |
Correct |
2 ms |
644 KB |
Output is correct |
27 |
Correct |
2 ms |
644 KB |
Output is correct |
28 |
Correct |
3 ms |
644 KB |
Output is correct |
29 |
Correct |
3 ms |
644 KB |
Output is correct |
30 |
Correct |
2 ms |
644 KB |
Output is correct |
31 |
Correct |
2 ms |
644 KB |
Output is correct |
32 |
Correct |
2 ms |
644 KB |
Output is correct |
33 |
Correct |
3 ms |
644 KB |
Output is correct |
34 |
Correct |
2 ms |
740 KB |
Output is correct |
35 |
Correct |
2 ms |
740 KB |
Output is correct |
36 |
Correct |
2 ms |
740 KB |
Output is correct |
37 |
Correct |
2 ms |
740 KB |
Output is correct |
38 |
Correct |
2 ms |
740 KB |
Output is correct |
39 |
Correct |
3 ms |
740 KB |
Output is correct |
40 |
Correct |
5 ms |
740 KB |
Output is correct |
41 |
Correct |
3 ms |
740 KB |
Output is correct |
42 |
Correct |
3 ms |
740 KB |
Output is correct |
43 |
Correct |
3 ms |
740 KB |
Output is correct |
44 |
Correct |
2 ms |
764 KB |
Output is correct |
45 |
Correct |
2 ms |
764 KB |
Output is correct |
46 |
Correct |
3 ms |
764 KB |
Output is correct |
47 |
Correct |
4 ms |
840 KB |
Output is correct |
48 |
Correct |
2 ms |
876 KB |
Output is correct |
49 |
Correct |
3 ms |
876 KB |
Output is correct |
50 |
Correct |
3 ms |
876 KB |
Output is correct |
51 |
Correct |
2 ms |
876 KB |
Output is correct |
52 |
Correct |
2 ms |
876 KB |
Output is correct |
53 |
Correct |
2 ms |
876 KB |
Output is correct |
54 |
Correct |
2 ms |
876 KB |
Output is correct |
55 |
Incorrect |
3 ms |
876 KB |
no wires in direction 1 between segments 19 and 31 |
56 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
432 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
516 KB |
Output is correct |
7 |
Correct |
2 ms |
516 KB |
Output is correct |
8 |
Correct |
3 ms |
516 KB |
Output is correct |
9 |
Correct |
3 ms |
516 KB |
Output is correct |
10 |
Correct |
2 ms |
516 KB |
Output is correct |
11 |
Correct |
2 ms |
516 KB |
Output is correct |
12 |
Correct |
3 ms |
516 KB |
Output is correct |
13 |
Correct |
3 ms |
644 KB |
Output is correct |
14 |
Correct |
2 ms |
644 KB |
Output is correct |
15 |
Correct |
3 ms |
644 KB |
Output is correct |
16 |
Correct |
3 ms |
644 KB |
Output is correct |
17 |
Correct |
3 ms |
644 KB |
Output is correct |
18 |
Correct |
2 ms |
644 KB |
Output is correct |
19 |
Correct |
3 ms |
644 KB |
Output is correct |
20 |
Correct |
3 ms |
644 KB |
Output is correct |
21 |
Correct |
3 ms |
644 KB |
Output is correct |
22 |
Correct |
3 ms |
644 KB |
Output is correct |
23 |
Correct |
2 ms |
644 KB |
Output is correct |
24 |
Correct |
3 ms |
644 KB |
Output is correct |
25 |
Correct |
3 ms |
644 KB |
Output is correct |
26 |
Correct |
2 ms |
644 KB |
Output is correct |
27 |
Correct |
2 ms |
644 KB |
Output is correct |
28 |
Correct |
3 ms |
644 KB |
Output is correct |
29 |
Correct |
3 ms |
644 KB |
Output is correct |
30 |
Correct |
2 ms |
644 KB |
Output is correct |
31 |
Correct |
2 ms |
644 KB |
Output is correct |
32 |
Correct |
2 ms |
644 KB |
Output is correct |
33 |
Correct |
3 ms |
644 KB |
Output is correct |
34 |
Correct |
2 ms |
740 KB |
Output is correct |
35 |
Correct |
2 ms |
740 KB |
Output is correct |
36 |
Correct |
2 ms |
740 KB |
Output is correct |
37 |
Correct |
2 ms |
740 KB |
Output is correct |
38 |
Correct |
2 ms |
740 KB |
Output is correct |
39 |
Correct |
3 ms |
740 KB |
Output is correct |
40 |
Correct |
5 ms |
740 KB |
Output is correct |
41 |
Correct |
3 ms |
740 KB |
Output is correct |
42 |
Correct |
3 ms |
740 KB |
Output is correct |
43 |
Correct |
3 ms |
740 KB |
Output is correct |
44 |
Correct |
2 ms |
764 KB |
Output is correct |
45 |
Correct |
2 ms |
764 KB |
Output is correct |
46 |
Correct |
3 ms |
764 KB |
Output is correct |
47 |
Correct |
4 ms |
840 KB |
Output is correct |
48 |
Correct |
2 ms |
876 KB |
Output is correct |
49 |
Correct |
3 ms |
876 KB |
Output is correct |
50 |
Correct |
3 ms |
876 KB |
Output is correct |
51 |
Correct |
2 ms |
876 KB |
Output is correct |
52 |
Correct |
2 ms |
876 KB |
Output is correct |
53 |
Correct |
2 ms |
876 KB |
Output is correct |
54 |
Correct |
2 ms |
876 KB |
Output is correct |
55 |
Incorrect |
3 ms |
876 KB |
no wires in direction 1 between segments 19 and 31 |
56 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
3540 KB |
Output is correct |
2 |
Correct |
5 ms |
3540 KB |
Output is correct |
3 |
Correct |
15 ms |
3540 KB |
Output is correct |
4 |
Correct |
32 ms |
3540 KB |
Output is correct |
5 |
Correct |
85 ms |
4024 KB |
Output is correct |
6 |
Correct |
43 ms |
4024 KB |
Output is correct |
7 |
Correct |
65 ms |
4024 KB |
Output is correct |
8 |
Correct |
6 ms |
4024 KB |
Output is correct |
9 |
Correct |
5 ms |
4024 KB |
Output is correct |
10 |
Correct |
69 ms |
4132 KB |
Output is correct |
11 |
Correct |
69 ms |
4132 KB |
Output is correct |
12 |
Correct |
65 ms |
4132 KB |
Output is correct |
13 |
Correct |
4 ms |
4132 KB |
Output is correct |
14 |
Correct |
4 ms |
4132 KB |
Output is correct |
15 |
Correct |
59 ms |
4132 KB |
Output is correct |
16 |
Correct |
17 ms |
4132 KB |
Output is correct |
17 |
Correct |
81 ms |
4132 KB |
Output is correct |
18 |
Correct |
59 ms |
4132 KB |
Output is correct |
19 |
Correct |
5 ms |
4132 KB |
Output is correct |
20 |
Correct |
58 ms |
4132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
432 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
516 KB |
Output is correct |
7 |
Correct |
2 ms |
516 KB |
Output is correct |
8 |
Correct |
3 ms |
516 KB |
Output is correct |
9 |
Correct |
3 ms |
516 KB |
Output is correct |
10 |
Correct |
2 ms |
516 KB |
Output is correct |
11 |
Correct |
2 ms |
516 KB |
Output is correct |
12 |
Correct |
3 ms |
516 KB |
Output is correct |
13 |
Correct |
3 ms |
644 KB |
Output is correct |
14 |
Correct |
2 ms |
644 KB |
Output is correct |
15 |
Correct |
3 ms |
644 KB |
Output is correct |
16 |
Correct |
3 ms |
644 KB |
Output is correct |
17 |
Correct |
3 ms |
644 KB |
Output is correct |
18 |
Correct |
2 ms |
644 KB |
Output is correct |
19 |
Correct |
3 ms |
644 KB |
Output is correct |
20 |
Correct |
3 ms |
644 KB |
Output is correct |
21 |
Correct |
3 ms |
644 KB |
Output is correct |
22 |
Correct |
3 ms |
644 KB |
Output is correct |
23 |
Correct |
2 ms |
644 KB |
Output is correct |
24 |
Correct |
3 ms |
644 KB |
Output is correct |
25 |
Correct |
3 ms |
644 KB |
Output is correct |
26 |
Correct |
2 ms |
644 KB |
Output is correct |
27 |
Correct |
2 ms |
644 KB |
Output is correct |
28 |
Correct |
3 ms |
644 KB |
Output is correct |
29 |
Correct |
3 ms |
644 KB |
Output is correct |
30 |
Correct |
2 ms |
644 KB |
Output is correct |
31 |
Correct |
2 ms |
644 KB |
Output is correct |
32 |
Correct |
2 ms |
644 KB |
Output is correct |
33 |
Correct |
3 ms |
644 KB |
Output is correct |
34 |
Correct |
2 ms |
740 KB |
Output is correct |
35 |
Correct |
2 ms |
740 KB |
Output is correct |
36 |
Correct |
2 ms |
740 KB |
Output is correct |
37 |
Correct |
2 ms |
740 KB |
Output is correct |
38 |
Correct |
2 ms |
740 KB |
Output is correct |
39 |
Correct |
3 ms |
740 KB |
Output is correct |
40 |
Correct |
5 ms |
740 KB |
Output is correct |
41 |
Correct |
3 ms |
740 KB |
Output is correct |
42 |
Correct |
3 ms |
740 KB |
Output is correct |
43 |
Correct |
3 ms |
740 KB |
Output is correct |
44 |
Correct |
2 ms |
764 KB |
Output is correct |
45 |
Correct |
2 ms |
764 KB |
Output is correct |
46 |
Correct |
3 ms |
764 KB |
Output is correct |
47 |
Correct |
4 ms |
840 KB |
Output is correct |
48 |
Correct |
2 ms |
876 KB |
Output is correct |
49 |
Correct |
3 ms |
876 KB |
Output is correct |
50 |
Correct |
3 ms |
876 KB |
Output is correct |
51 |
Correct |
2 ms |
876 KB |
Output is correct |
52 |
Correct |
2 ms |
876 KB |
Output is correct |
53 |
Correct |
2 ms |
876 KB |
Output is correct |
54 |
Correct |
2 ms |
876 KB |
Output is correct |
55 |
Incorrect |
3 ms |
876 KB |
no wires in direction 1 between segments 19 and 31 |
56 |
Halted |
0 ms |
0 KB |
- |