/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
*/
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define eb emplace_back
#define PI acos(-1.0)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define BIT(x, i) (1 & ((x) >> (i)))
#define MASK(x) (1LL << (x))
#define task "tnc"
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
typedef long long ll;
typedef long double ld;
const ll INF = 1e18;
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
const int mo = 998244353;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using pii = pair<pair<ll, ll>, ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct seg
{
int l, r, id;
seg() {}
seg(int _l, int _r, int _id)
{
l = _l;
r = _r;
id = _id;
}
} A[maxn];
int pref[maxn];
vector<int> G[maxn];
int n, m;
int pref1[maxn];
vector<int> sus[maxn];
int col[maxn];
int ch = 0;
void dfs(int u)
{
for (auto v : G[u])
{
if (col[v] == -1)
{
col[v] = col[u] ^ 1;
dfs(v);
}
else if (col[v] != col[u] ^ 1)
{
ch = 1;
}
}
}
int pref2[maxn];
vector<pair<int, int>> st[maxn];
vector<int> era[maxn];
signed main()
{
cin.tie(0), cout.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int l, r;
cin >> l >> r;
st[l].pb({r, i});
pref[l]++;
pref[r + 1]--;
if (l > r)
pref[1]++;
A[i] = seg(l, r, i);
}
vector<int> check;
for (int i = 1; i <= n; i++)
{
pref[i] += pref[i - 1];
pref1[i] = pref1[i - 1];
if (pref[i] == 2)
{
check.pb(i);
pref1[i]++;
}
if (pref[i] == 1)
{
cout << "impossible\n";
return 0;
}
}
for (int i = 1; i <= n; i++)
{
if (A[i].l <= A[i].r)
{
if (pref1[A[i].r] - pref1[A[i].l - 1] > 0)
{
int v = lower_bound(check.begin(), check.end(), A[i].l) - check.begin();
while (v < check.size() && check[v] <= A[i].r)
{
sus[check[v]].pb(i);
v++;
}
}
}
else
{
int v = lower_bound(check.begin(), check.end(), A[i].l) - check.begin();
while (v < check.size())
{
sus[check[v]].pb(i);
v++;
}
v = lower_bound(check.begin(), check.end(), 1) - check.begin();
while (v < check.size() && check[v] <= A[i].r)
{
sus[check[v]].pb(i);
v++;
}
}
}
for (auto v : check)
{
G[sus[v][0]].pb(sus[v][1]);
G[sus[v][1]].pb(sus[v][0]);
}
memset(col, -1, sizeof(col));
for (int i = 1; i <= m; i++)
{
if (G[i].size() && col[i] == -1)
{
col[i] = 0;
dfs(i);
}
}
if (ch == 1)
{
cout << "impossible\n";
return 0;
}
set<pair<int, int>> pq;
for (int i = 1; i <= m; i++)
{
if (col[i] == 1)
{
pref2[A[i].l]++;
pref2[A[i].r + 1]--;
if (A[i].l > A[i].r)
pref2[1]++;
}
if (col[i] == -1 && A[i].l > A[i].r)
{
pq.insert({A[i].r, A[i].id});
era[A[i].r + 1].pb(i);
}
}
for (int i = 1; i <= n; i++)
{
pref2[i] += pref2[i - 1];
int id = 0;
int r = 0;
for (auto v : era[i])
{
pq.erase({i, v});
}
for (auto v : st[i])
{
if (col[v.se] == -1)
{
if (i > v.fi)
{
pq.insert({v.fi + n, v.se});
}
else
{
pq.insert({v.fi, v.se});
era[v.fi + 1].pb(v.se);
}
}
}
if (pref2[i] == 0)
{
int id = (*pq.begin()).se;
int r = (*pq.begin()).fi;
col[id] = 1;
pref2[i + 1]++;
pref2[r + 1]--;
if (A[id].l > A[id].r)
{
pref2[A[id].l]++;
}
}
}
for (int i = 1; i <= m; i++)
{
if (col[i] == 1)
{
cout << 1;
}
else
{
cout << 0;
}
}
}
Compilation message
alternating.cpp: In function 'void dfs(int)':
alternating.cpp:60:25: warning: suggest parentheses around comparison in operand of '^' [-Wparentheses]
60 | else if (col[v] != col[u] ^ 1)
| ~~~~~~~^~~~~~~~~
alternating.cpp: In function 'int main()':
alternating.cpp:107:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | while (v < check.size() && check[v] <= A[i].r)
| ~~^~~~~~~~~~~~~~
alternating.cpp:117:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | while (v < check.size())
| ~~^~~~~~~~~~~~~~
alternating.cpp:123:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | while (v < check.size() && check[v] <= A[i].r)
| ~~^~~~~~~~~~~~~~
alternating.cpp:170:13: warning: unused variable 'id' [-Wunused-variable]
170 | int id = 0;
| ^~
alternating.cpp:171:13: warning: unused variable 'r' [-Wunused-variable]
171 | int r = 0;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
98212 KB |
Output is correct |
2 |
Correct |
44 ms |
98084 KB |
Output is correct |
3 |
Correct |
46 ms |
98144 KB |
Output is correct |
4 |
Correct |
44 ms |
98164 KB |
Output is correct |
5 |
Correct |
44 ms |
98072 KB |
Output is correct |
6 |
Correct |
43 ms |
98180 KB |
Output is correct |
7 |
Correct |
49 ms |
98192 KB |
Output is correct |
8 |
Correct |
42 ms |
94216 KB |
Output is correct |
9 |
Correct |
43 ms |
98124 KB |
Output is correct |
10 |
Correct |
43 ms |
94156 KB |
Output is correct |
11 |
Correct |
45 ms |
94240 KB |
Output is correct |
12 |
Correct |
49 ms |
98124 KB |
Output is correct |
13 |
Correct |
42 ms |
94204 KB |
Output is correct |
14 |
Correct |
43 ms |
98152 KB |
Output is correct |
15 |
Correct |
45 ms |
98132 KB |
Output is correct |
16 |
Correct |
45 ms |
98172 KB |
Output is correct |
17 |
Correct |
52 ms |
98176 KB |
Output is correct |
18 |
Correct |
48 ms |
98180 KB |
Output is correct |
19 |
Correct |
45 ms |
98112 KB |
Output is correct |
20 |
Correct |
46 ms |
98192 KB |
Output is correct |
21 |
Correct |
45 ms |
98072 KB |
Output is correct |
22 |
Correct |
44 ms |
98112 KB |
Output is correct |
23 |
Correct |
45 ms |
98104 KB |
Output is correct |
24 |
Correct |
44 ms |
98076 KB |
Output is correct |
25 |
Correct |
47 ms |
98168 KB |
Output is correct |
26 |
Correct |
46 ms |
98124 KB |
Output is correct |
27 |
Incorrect |
46 ms |
98192 KB |
no wires in direction 0 between segments 15 and 15 |
28 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
98212 KB |
Output is correct |
2 |
Correct |
44 ms |
98084 KB |
Output is correct |
3 |
Correct |
46 ms |
98144 KB |
Output is correct |
4 |
Correct |
44 ms |
98164 KB |
Output is correct |
5 |
Correct |
44 ms |
98072 KB |
Output is correct |
6 |
Correct |
43 ms |
98180 KB |
Output is correct |
7 |
Correct |
49 ms |
98192 KB |
Output is correct |
8 |
Correct |
42 ms |
94216 KB |
Output is correct |
9 |
Correct |
43 ms |
98124 KB |
Output is correct |
10 |
Correct |
43 ms |
94156 KB |
Output is correct |
11 |
Correct |
45 ms |
94240 KB |
Output is correct |
12 |
Correct |
49 ms |
98124 KB |
Output is correct |
13 |
Correct |
42 ms |
94204 KB |
Output is correct |
14 |
Correct |
43 ms |
98152 KB |
Output is correct |
15 |
Correct |
45 ms |
98132 KB |
Output is correct |
16 |
Correct |
45 ms |
98172 KB |
Output is correct |
17 |
Correct |
52 ms |
98176 KB |
Output is correct |
18 |
Correct |
48 ms |
98180 KB |
Output is correct |
19 |
Correct |
45 ms |
98112 KB |
Output is correct |
20 |
Correct |
46 ms |
98192 KB |
Output is correct |
21 |
Correct |
45 ms |
98072 KB |
Output is correct |
22 |
Correct |
44 ms |
98112 KB |
Output is correct |
23 |
Correct |
45 ms |
98104 KB |
Output is correct |
24 |
Correct |
44 ms |
98076 KB |
Output is correct |
25 |
Correct |
47 ms |
98168 KB |
Output is correct |
26 |
Correct |
46 ms |
98124 KB |
Output is correct |
27 |
Incorrect |
46 ms |
98192 KB |
no wires in direction 0 between segments 15 and 15 |
28 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
98212 KB |
Output is correct |
2 |
Correct |
44 ms |
98084 KB |
Output is correct |
3 |
Correct |
46 ms |
98144 KB |
Output is correct |
4 |
Correct |
44 ms |
98164 KB |
Output is correct |
5 |
Correct |
44 ms |
98072 KB |
Output is correct |
6 |
Correct |
43 ms |
98180 KB |
Output is correct |
7 |
Correct |
49 ms |
98192 KB |
Output is correct |
8 |
Correct |
42 ms |
94216 KB |
Output is correct |
9 |
Correct |
43 ms |
98124 KB |
Output is correct |
10 |
Correct |
43 ms |
94156 KB |
Output is correct |
11 |
Correct |
45 ms |
94240 KB |
Output is correct |
12 |
Correct |
49 ms |
98124 KB |
Output is correct |
13 |
Correct |
42 ms |
94204 KB |
Output is correct |
14 |
Correct |
43 ms |
98152 KB |
Output is correct |
15 |
Correct |
45 ms |
98132 KB |
Output is correct |
16 |
Correct |
45 ms |
98172 KB |
Output is correct |
17 |
Correct |
52 ms |
98176 KB |
Output is correct |
18 |
Correct |
48 ms |
98180 KB |
Output is correct |
19 |
Correct |
45 ms |
98112 KB |
Output is correct |
20 |
Correct |
46 ms |
98192 KB |
Output is correct |
21 |
Correct |
45 ms |
98072 KB |
Output is correct |
22 |
Correct |
44 ms |
98112 KB |
Output is correct |
23 |
Correct |
45 ms |
98104 KB |
Output is correct |
24 |
Correct |
44 ms |
98076 KB |
Output is correct |
25 |
Correct |
47 ms |
98168 KB |
Output is correct |
26 |
Correct |
46 ms |
98124 KB |
Output is correct |
27 |
Incorrect |
46 ms |
98192 KB |
no wires in direction 0 between segments 15 and 15 |
28 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
106420 KB |
Output is correct |
2 |
Correct |
67 ms |
104232 KB |
Output is correct |
3 |
Correct |
56 ms |
96744 KB |
Output is correct |
4 |
Correct |
74 ms |
105476 KB |
Output is correct |
5 |
Correct |
120 ms |
109388 KB |
Output is correct |
6 |
Correct |
67 ms |
98192 KB |
Output is correct |
7 |
Incorrect |
114 ms |
109768 KB |
no wires in direction 1 between segments 762 and 762 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
98212 KB |
Output is correct |
2 |
Correct |
44 ms |
98084 KB |
Output is correct |
3 |
Correct |
46 ms |
98144 KB |
Output is correct |
4 |
Correct |
44 ms |
98164 KB |
Output is correct |
5 |
Correct |
44 ms |
98072 KB |
Output is correct |
6 |
Correct |
43 ms |
98180 KB |
Output is correct |
7 |
Correct |
49 ms |
98192 KB |
Output is correct |
8 |
Correct |
42 ms |
94216 KB |
Output is correct |
9 |
Correct |
43 ms |
98124 KB |
Output is correct |
10 |
Correct |
43 ms |
94156 KB |
Output is correct |
11 |
Correct |
45 ms |
94240 KB |
Output is correct |
12 |
Correct |
49 ms |
98124 KB |
Output is correct |
13 |
Correct |
42 ms |
94204 KB |
Output is correct |
14 |
Correct |
43 ms |
98152 KB |
Output is correct |
15 |
Correct |
45 ms |
98132 KB |
Output is correct |
16 |
Correct |
45 ms |
98172 KB |
Output is correct |
17 |
Correct |
52 ms |
98176 KB |
Output is correct |
18 |
Correct |
48 ms |
98180 KB |
Output is correct |
19 |
Correct |
45 ms |
98112 KB |
Output is correct |
20 |
Correct |
46 ms |
98192 KB |
Output is correct |
21 |
Correct |
45 ms |
98072 KB |
Output is correct |
22 |
Correct |
44 ms |
98112 KB |
Output is correct |
23 |
Correct |
45 ms |
98104 KB |
Output is correct |
24 |
Correct |
44 ms |
98076 KB |
Output is correct |
25 |
Correct |
47 ms |
98168 KB |
Output is correct |
26 |
Correct |
46 ms |
98124 KB |
Output is correct |
27 |
Incorrect |
46 ms |
98192 KB |
no wires in direction 0 between segments 15 and 15 |
28 |
Halted |
0 ms |
0 KB |
- |