Submission #566366

# Submission time Handle Problem Language Result Execution time Memory
566366 2022-05-22T09:22:05 Z hoanghq2004 Alternating Current (BOI18_alternating) C++14
100 / 100
180 ms 16568 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;


const int N = 1e5 + 10;

int n, m, k;
vector <int> s[N], g[N];
int color[N];

int main() {
    ios :: sync_with_stdio(0); cin.tie(0);
    cin >> n >> m;
    vector <tuple <int, int, int>> events;
    set <int> s;
    for (int i = 1; i <= m; ++i) {
        int L, R;
        cin >> L >> R;
        if (L <= R) {
            events.push_back({L, -1, i});
            events.push_back({R, 1, i});
        } else {
            events.push_back({L, -1, i});
            events.push_back({n, 1, i});
            events.push_back({1, -1, i});
            events.push_back({R, 1, i});
        }
    }

    for (int i = 1; i <= n; ++i) events.push_back({i, 0, i});
    sort(events.begin(), events.end());
    set <int> c0, c1;
    for (auto [x, sign, i]: events) {
        if (sign == -1) {
            s.insert(i);
        } else if (sign == 1) {
            s.erase(i);
        } else {
            if (s.size() < 2) {
                cout << "impossible\n";
                exit(0);
            }
            if (s.size() == 2) {
                int u = *s.begin();
                int v = *(--s.end());
                g[u].push_back(v);
                g[v].push_back(u);
            }
        }
    }

    function<int(int, int)> dfs = [&](int u, int c) {
        if (color[u] != -1) return (int)(color[u] == c);
        color[u] = c;
        if (s.find(u) != s.end()) {
            s.erase(u);
            if (c == 0) c0.insert(u);
            else c1.insert(u);
        }
        for (auto v: g[u])
            if (!dfs(v, c ^ 1)) return 0;
        return 1;
    };

    memset(color, -1, sizeof(color));
    for (auto [x, sign, i]: events) {
        if (sign == -1) {
            if (color[i] == -1) s.insert(i);
            else if (color[i] == 0) c0.insert(i);
            else c1.insert(i);
        } else if (sign == 1) {
            s.erase(i);
            c0.erase(i);
            c1.erase(i);
        } else {
            if (c0.size() && c1.size()) continue;
            if (c0.empty()) {
                int u = *s.begin();
                if (!dfs(u, 0)) {
                    cout << "impossible";
                    exit(0);
                }
            }
            if (c1.empty()) {
                int u = *s.begin();
                if (!dfs(u, 1)) {
                    cout << "impossible";
                    exit(0);
                }
            }
        }
    }

    for (int i = 1; i <= m; ++i) if (color[i] == -1) color[i] = 0;
    for (int i = 1; i <= m; ++i) cout << color[i];
}

Compilation message

alternating.cpp: In function 'int main()':
alternating.cpp:42:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   42 |     for (auto [x, sign, i]: events) {
      |               ^
alternating.cpp:75:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   75 |     for (auto [x, sign, i]: events) {
      |               ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5332 KB Output is correct
2 Correct 4 ms 5332 KB Output is correct
3 Correct 4 ms 5332 KB Output is correct
4 Correct 3 ms 5404 KB Output is correct
5 Correct 5 ms 5332 KB Output is correct
6 Correct 4 ms 5412 KB Output is correct
7 Correct 3 ms 5412 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 5332 KB Output is correct
10 Correct 3 ms 5032 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 5332 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5332 KB Output is correct
15 Correct 3 ms 5412 KB Output is correct
16 Correct 3 ms 5332 KB Output is correct
17 Correct 4 ms 5332 KB Output is correct
18 Correct 3 ms 5416 KB Output is correct
19 Correct 3 ms 5332 KB Output is correct
20 Correct 3 ms 5332 KB Output is correct
21 Correct 3 ms 5332 KB Output is correct
22 Correct 3 ms 5332 KB Output is correct
23 Correct 3 ms 5416 KB Output is correct
24 Correct 3 ms 5332 KB Output is correct
25 Correct 3 ms 5336 KB Output is correct
26 Correct 3 ms 5332 KB Output is correct
27 Correct 3 ms 5028 KB Output is correct
28 Correct 3 ms 5332 KB Output is correct
29 Correct 3 ms 4948 KB Output is correct
30 Correct 4 ms 4948 KB Output is correct
31 Correct 3 ms 5332 KB Output is correct
32 Correct 3 ms 4948 KB Output is correct
33 Correct 3 ms 5408 KB Output is correct
34 Correct 3 ms 4948 KB Output is correct
35 Correct 3 ms 4948 KB Output is correct
36 Correct 3 ms 5332 KB Output is correct
37 Correct 3 ms 5412 KB Output is correct
38 Correct 3 ms 4948 KB Output is correct
39 Correct 3 ms 5332 KB Output is correct
40 Correct 3 ms 5408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5332 KB Output is correct
2 Correct 4 ms 5332 KB Output is correct
3 Correct 4 ms 5332 KB Output is correct
4 Correct 3 ms 5404 KB Output is correct
5 Correct 5 ms 5332 KB Output is correct
6 Correct 4 ms 5412 KB Output is correct
7 Correct 3 ms 5412 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 5332 KB Output is correct
10 Correct 3 ms 5032 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 5332 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5332 KB Output is correct
15 Correct 3 ms 5412 KB Output is correct
16 Correct 3 ms 5332 KB Output is correct
17 Correct 4 ms 5332 KB Output is correct
18 Correct 3 ms 5416 KB Output is correct
19 Correct 3 ms 5332 KB Output is correct
20 Correct 3 ms 5332 KB Output is correct
21 Correct 3 ms 5332 KB Output is correct
22 Correct 3 ms 5332 KB Output is correct
23 Correct 3 ms 5416 KB Output is correct
24 Correct 3 ms 5332 KB Output is correct
25 Correct 3 ms 5336 KB Output is correct
26 Correct 3 ms 5332 KB Output is correct
27 Correct 3 ms 5028 KB Output is correct
28 Correct 3 ms 5332 KB Output is correct
29 Correct 3 ms 4948 KB Output is correct
30 Correct 4 ms 4948 KB Output is correct
31 Correct 3 ms 5332 KB Output is correct
32 Correct 3 ms 4948 KB Output is correct
33 Correct 3 ms 5408 KB Output is correct
34 Correct 3 ms 4948 KB Output is correct
35 Correct 3 ms 4948 KB Output is correct
36 Correct 3 ms 5332 KB Output is correct
37 Correct 3 ms 5412 KB Output is correct
38 Correct 3 ms 4948 KB Output is correct
39 Correct 3 ms 5332 KB Output is correct
40 Correct 3 ms 5408 KB Output is correct
41 Correct 3 ms 5332 KB Output is correct
42 Correct 3 ms 5332 KB Output is correct
43 Correct 3 ms 5412 KB Output is correct
44 Correct 4 ms 5332 KB Output is correct
45 Correct 3 ms 5332 KB Output is correct
46 Correct 3 ms 5416 KB Output is correct
47 Correct 3 ms 5332 KB Output is correct
48 Correct 2 ms 4948 KB Output is correct
49 Correct 3 ms 5332 KB Output is correct
50 Correct 2 ms 4948 KB Output is correct
51 Correct 3 ms 4948 KB Output is correct
52 Correct 3 ms 4948 KB Output is correct
53 Correct 3 ms 4948 KB Output is correct
54 Correct 3 ms 5332 KB Output is correct
55 Correct 3 ms 5332 KB Output is correct
56 Correct 5 ms 5332 KB Output is correct
57 Correct 3 ms 5332 KB Output is correct
58 Correct 4 ms 5412 KB Output is correct
59 Correct 3 ms 5332 KB Output is correct
60 Correct 3 ms 5412 KB Output is correct
61 Correct 3 ms 5412 KB Output is correct
62 Correct 3 ms 5024 KB Output is correct
63 Correct 3 ms 5024 KB Output is correct
64 Correct 3 ms 5332 KB Output is correct
65 Correct 3 ms 4948 KB Output is correct
66 Correct 3 ms 5332 KB Output is correct
67 Correct 4 ms 5332 KB Output is correct
68 Correct 3 ms 5464 KB Output is correct
69 Correct 3 ms 5412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5332 KB Output is correct
2 Correct 4 ms 5332 KB Output is correct
3 Correct 4 ms 5332 KB Output is correct
4 Correct 3 ms 5404 KB Output is correct
5 Correct 5 ms 5332 KB Output is correct
6 Correct 4 ms 5412 KB Output is correct
7 Correct 3 ms 5412 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 5332 KB Output is correct
10 Correct 3 ms 5032 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 5332 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5332 KB Output is correct
15 Correct 3 ms 5412 KB Output is correct
16 Correct 3 ms 5332 KB Output is correct
17 Correct 4 ms 5332 KB Output is correct
18 Correct 3 ms 5416 KB Output is correct
19 Correct 3 ms 5332 KB Output is correct
20 Correct 3 ms 5332 KB Output is correct
21 Correct 3 ms 5332 KB Output is correct
22 Correct 3 ms 5332 KB Output is correct
23 Correct 3 ms 5416 KB Output is correct
24 Correct 3 ms 5332 KB Output is correct
25 Correct 3 ms 5336 KB Output is correct
26 Correct 3 ms 5332 KB Output is correct
27 Correct 3 ms 5028 KB Output is correct
28 Correct 3 ms 5332 KB Output is correct
29 Correct 3 ms 4948 KB Output is correct
30 Correct 4 ms 4948 KB Output is correct
31 Correct 3 ms 5332 KB Output is correct
32 Correct 3 ms 4948 KB Output is correct
33 Correct 3 ms 5408 KB Output is correct
34 Correct 3 ms 4948 KB Output is correct
35 Correct 3 ms 4948 KB Output is correct
36 Correct 3 ms 5332 KB Output is correct
37 Correct 3 ms 5412 KB Output is correct
38 Correct 3 ms 4948 KB Output is correct
39 Correct 3 ms 5332 KB Output is correct
40 Correct 3 ms 5408 KB Output is correct
41 Correct 3 ms 5332 KB Output is correct
42 Correct 3 ms 5332 KB Output is correct
43 Correct 3 ms 5412 KB Output is correct
44 Correct 4 ms 5332 KB Output is correct
45 Correct 3 ms 5332 KB Output is correct
46 Correct 3 ms 5416 KB Output is correct
47 Correct 3 ms 5332 KB Output is correct
48 Correct 2 ms 4948 KB Output is correct
49 Correct 3 ms 5332 KB Output is correct
50 Correct 2 ms 4948 KB Output is correct
51 Correct 3 ms 4948 KB Output is correct
52 Correct 3 ms 4948 KB Output is correct
53 Correct 3 ms 4948 KB Output is correct
54 Correct 3 ms 5332 KB Output is correct
55 Correct 3 ms 5332 KB Output is correct
56 Correct 5 ms 5332 KB Output is correct
57 Correct 3 ms 5332 KB Output is correct
58 Correct 4 ms 5412 KB Output is correct
59 Correct 3 ms 5332 KB Output is correct
60 Correct 3 ms 5412 KB Output is correct
61 Correct 3 ms 5412 KB Output is correct
62 Correct 3 ms 5024 KB Output is correct
63 Correct 3 ms 5024 KB Output is correct
64 Correct 3 ms 5332 KB Output is correct
65 Correct 3 ms 4948 KB Output is correct
66 Correct 3 ms 5332 KB Output is correct
67 Correct 4 ms 5332 KB Output is correct
68 Correct 3 ms 5464 KB Output is correct
69 Correct 3 ms 5412 KB Output is correct
70 Correct 4 ms 5420 KB Output is correct
71 Correct 4 ms 5544 KB Output is correct
72 Correct 4 ms 5460 KB Output is correct
73 Correct 4 ms 5416 KB Output is correct
74 Correct 3 ms 5460 KB Output is correct
75 Correct 3 ms 5332 KB Output is correct
76 Correct 3 ms 5076 KB Output is correct
77 Correct 3 ms 5460 KB Output is correct
78 Correct 3 ms 4948 KB Output is correct
79 Correct 3 ms 5028 KB Output is correct
80 Correct 3 ms 5076 KB Output is correct
81 Correct 4 ms 5460 KB Output is correct
82 Correct 4 ms 5408 KB Output is correct
83 Correct 4 ms 5460 KB Output is correct
84 Correct 5 ms 5416 KB Output is correct
85 Correct 3 ms 5332 KB Output is correct
86 Correct 3 ms 5332 KB Output is correct
87 Correct 4 ms 5464 KB Output is correct
88 Correct 4 ms 5416 KB Output is correct
89 Correct 3 ms 5076 KB Output is correct
90 Correct 4 ms 5460 KB Output is correct
91 Correct 3 ms 5460 KB Output is correct
92 Correct 4 ms 5460 KB Output is correct
93 Correct 3 ms 5332 KB Output is correct
94 Correct 3 ms 5024 KB Output is correct
95 Correct 3 ms 5460 KB Output is correct
96 Correct 4 ms 5468 KB Output is correct
97 Correct 6 ms 5460 KB Output is correct
98 Correct 4 ms 5460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 96 ms 14660 KB Output is correct
2 Correct 18 ms 7756 KB Output is correct
3 Correct 33 ms 8832 KB Output is correct
4 Correct 49 ms 10644 KB Output is correct
5 Correct 81 ms 13084 KB Output is correct
6 Correct 60 ms 12384 KB Output is correct
7 Correct 75 ms 12896 KB Output is correct
8 Correct 12 ms 7392 KB Output is correct
9 Correct 14 ms 7112 KB Output is correct
10 Correct 87 ms 13356 KB Output is correct
11 Correct 87 ms 11808 KB Output is correct
12 Correct 69 ms 12924 KB Output is correct
13 Correct 14 ms 7372 KB Output is correct
14 Correct 16 ms 7116 KB Output is correct
15 Correct 70 ms 12416 KB Output is correct
16 Correct 50 ms 10248 KB Output is correct
17 Correct 162 ms 12572 KB Output is correct
18 Correct 131 ms 11204 KB Output is correct
19 Correct 10 ms 6692 KB Output is correct
20 Correct 58 ms 12684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5332 KB Output is correct
2 Correct 4 ms 5332 KB Output is correct
3 Correct 4 ms 5332 KB Output is correct
4 Correct 3 ms 5404 KB Output is correct
5 Correct 5 ms 5332 KB Output is correct
6 Correct 4 ms 5412 KB Output is correct
7 Correct 3 ms 5412 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 5332 KB Output is correct
10 Correct 3 ms 5032 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 5332 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5332 KB Output is correct
15 Correct 3 ms 5412 KB Output is correct
16 Correct 3 ms 5332 KB Output is correct
17 Correct 4 ms 5332 KB Output is correct
18 Correct 3 ms 5416 KB Output is correct
19 Correct 3 ms 5332 KB Output is correct
20 Correct 3 ms 5332 KB Output is correct
21 Correct 3 ms 5332 KB Output is correct
22 Correct 3 ms 5332 KB Output is correct
23 Correct 3 ms 5416 KB Output is correct
24 Correct 3 ms 5332 KB Output is correct
25 Correct 3 ms 5336 KB Output is correct
26 Correct 3 ms 5332 KB Output is correct
27 Correct 3 ms 5028 KB Output is correct
28 Correct 3 ms 5332 KB Output is correct
29 Correct 3 ms 4948 KB Output is correct
30 Correct 4 ms 4948 KB Output is correct
31 Correct 3 ms 5332 KB Output is correct
32 Correct 3 ms 4948 KB Output is correct
33 Correct 3 ms 5408 KB Output is correct
34 Correct 3 ms 4948 KB Output is correct
35 Correct 3 ms 4948 KB Output is correct
36 Correct 3 ms 5332 KB Output is correct
37 Correct 3 ms 5412 KB Output is correct
38 Correct 3 ms 4948 KB Output is correct
39 Correct 3 ms 5332 KB Output is correct
40 Correct 3 ms 5408 KB Output is correct
41 Correct 3 ms 5332 KB Output is correct
42 Correct 3 ms 5332 KB Output is correct
43 Correct 3 ms 5412 KB Output is correct
44 Correct 4 ms 5332 KB Output is correct
45 Correct 3 ms 5332 KB Output is correct
46 Correct 3 ms 5416 KB Output is correct
47 Correct 3 ms 5332 KB Output is correct
48 Correct 2 ms 4948 KB Output is correct
49 Correct 3 ms 5332 KB Output is correct
50 Correct 2 ms 4948 KB Output is correct
51 Correct 3 ms 4948 KB Output is correct
52 Correct 3 ms 4948 KB Output is correct
53 Correct 3 ms 4948 KB Output is correct
54 Correct 3 ms 5332 KB Output is correct
55 Correct 3 ms 5332 KB Output is correct
56 Correct 5 ms 5332 KB Output is correct
57 Correct 3 ms 5332 KB Output is correct
58 Correct 4 ms 5412 KB Output is correct
59 Correct 3 ms 5332 KB Output is correct
60 Correct 3 ms 5412 KB Output is correct
61 Correct 3 ms 5412 KB Output is correct
62 Correct 3 ms 5024 KB Output is correct
63 Correct 3 ms 5024 KB Output is correct
64 Correct 3 ms 5332 KB Output is correct
65 Correct 3 ms 4948 KB Output is correct
66 Correct 3 ms 5332 KB Output is correct
67 Correct 4 ms 5332 KB Output is correct
68 Correct 3 ms 5464 KB Output is correct
69 Correct 3 ms 5412 KB Output is correct
70 Correct 4 ms 5420 KB Output is correct
71 Correct 4 ms 5544 KB Output is correct
72 Correct 4 ms 5460 KB Output is correct
73 Correct 4 ms 5416 KB Output is correct
74 Correct 3 ms 5460 KB Output is correct
75 Correct 3 ms 5332 KB Output is correct
76 Correct 3 ms 5076 KB Output is correct
77 Correct 3 ms 5460 KB Output is correct
78 Correct 3 ms 4948 KB Output is correct
79 Correct 3 ms 5028 KB Output is correct
80 Correct 3 ms 5076 KB Output is correct
81 Correct 4 ms 5460 KB Output is correct
82 Correct 4 ms 5408 KB Output is correct
83 Correct 4 ms 5460 KB Output is correct
84 Correct 5 ms 5416 KB Output is correct
85 Correct 3 ms 5332 KB Output is correct
86 Correct 3 ms 5332 KB Output is correct
87 Correct 4 ms 5464 KB Output is correct
88 Correct 4 ms 5416 KB Output is correct
89 Correct 3 ms 5076 KB Output is correct
90 Correct 4 ms 5460 KB Output is correct
91 Correct 3 ms 5460 KB Output is correct
92 Correct 4 ms 5460 KB Output is correct
93 Correct 3 ms 5332 KB Output is correct
94 Correct 3 ms 5024 KB Output is correct
95 Correct 3 ms 5460 KB Output is correct
96 Correct 4 ms 5468 KB Output is correct
97 Correct 6 ms 5460 KB Output is correct
98 Correct 4 ms 5460 KB Output is correct
99 Correct 96 ms 14660 KB Output is correct
100 Correct 18 ms 7756 KB Output is correct
101 Correct 33 ms 8832 KB Output is correct
102 Correct 49 ms 10644 KB Output is correct
103 Correct 81 ms 13084 KB Output is correct
104 Correct 60 ms 12384 KB Output is correct
105 Correct 75 ms 12896 KB Output is correct
106 Correct 12 ms 7392 KB Output is correct
107 Correct 14 ms 7112 KB Output is correct
108 Correct 87 ms 13356 KB Output is correct
109 Correct 87 ms 11808 KB Output is correct
110 Correct 69 ms 12924 KB Output is correct
111 Correct 14 ms 7372 KB Output is correct
112 Correct 16 ms 7116 KB Output is correct
113 Correct 70 ms 12416 KB Output is correct
114 Correct 50 ms 10248 KB Output is correct
115 Correct 162 ms 12572 KB Output is correct
116 Correct 131 ms 11204 KB Output is correct
117 Correct 10 ms 6692 KB Output is correct
118 Correct 58 ms 12684 KB Output is correct
119 Correct 19 ms 7624 KB Output is correct
120 Correct 66 ms 16568 KB Output is correct
121 Correct 44 ms 11576 KB Output is correct
122 Correct 3 ms 5332 KB Output is correct
123 Correct 11 ms 6732 KB Output is correct
124 Correct 27 ms 8548 KB Output is correct
125 Correct 39 ms 8784 KB Output is correct
126 Correct 16 ms 7388 KB Output is correct
127 Correct 20 ms 7404 KB Output is correct
128 Correct 73 ms 12008 KB Output is correct
129 Correct 48 ms 10280 KB Output is correct
130 Correct 63 ms 10812 KB Output is correct
131 Correct 16 ms 7624 KB Output is correct
132 Correct 16 ms 7756 KB Output is correct
133 Correct 180 ms 13616 KB Output is correct
134 Correct 54 ms 8520 KB Output is correct
135 Correct 17 ms 6736 KB Output is correct
136 Correct 38 ms 8876 KB Output is correct
137 Correct 50 ms 9084 KB Output is correct
138 Correct 15 ms 6760 KB Output is correct