Submission #441526

# Submission time Handle Problem Language Result Execution time Memory
441526 2021-07-05T10:12:30 Z abc864197532 Alternating Current (BOI18_alternating) C++17
100 / 100
334 ms 35652 KB
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define test(args...) abc("[" + string(#args) + "]", args)
void abc() {cerr << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
    cerr << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
    while (l != r) cout << *l << " \n"[++l == r];
}
const int mod = 1e9 + 7, N = 100000;

vector <int> ans, adj[N];

struct Seg {
    int l, r, m;
    vector <int> unknown;
    int one = 0, zero = 0;
    Seg* ch[2];
    Seg (int _l, int _r) : l(_l), r(_r), m(l + r >> 1) {
        if (r - l > 1) {
            ch[0] = new Seg(l, m);
            ch[1] = new Seg(m, r);
        }
    }
    void add(int a, int b, int id, int x) {
        if (a <= l && r <= b) {
            if (x == -1) unknown.pb(id);
            if (x == 0) zero++;
            if (x == 1) one++;
        } else {
            if (a < m) ch[0]->add(a, b, id, x);
            if (m < b) ch[1]->add(a, b, id, x);
        }
    }
    int query(int p) {
        if (r - l == 1) return unknown.size();
        return ch[p >= m]->query(p) + unknown.size();
    }
    vector <int> get(int p) {
        if (r - l == 1) return unknown;
        vector <int> a = unknown, b = ch[p >= m]->get(p);
        for (int i : b) a.pb(i);
        return a;
    }
    int val() {return (zero ? 1 : 0) + (one ? 2 : 0);}
    int get2(int p) {
        if (r - l == 1) return val();
        return val() | ch[p >= m]->get2(p);
    }
    int get_unknown(int p) {
        while (!unknown.empty()) {
            int v = unknown.back(); unknown.pop_back();
            if (ans[v] == -1) return v;
        }
        return ch[p >= m]->get_unknown(p);
    }
};

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    vector <pii> a(m);
    Seg root(0, n);
    for (int i = 0, l, r; i < m; ++i) {
        cin >> a[i].X >> a[i].Y, --a[i].X;
        tie(l, r) = a[i];
        if (l < r) {
            root.add(l, r, i, -1);
        } else {
            root.add(l, n, i, -1);
            root.add(0, r, i, -1);
        }
    }
    for (int i = 0; i < n; ++i) {
        if (root.query(i) < 2) return cout << "impossible\n", 0;
        if (root.query(i) == 2) {
            vector <int> tmp = root.get(i);
            adj[tmp[0]].pb(tmp[1]), adj[tmp[1]].pb(tmp[0]);
        }
    }
    auto chg = [&](int i, int x) {
        ans[i] = x;
        int l, r;
        tie(l, r) = a[i];
        if (l < r) {
            root.add(l, r, i, x);
        } else {
            root.add(l, n, i, x);
            root.add(0, r, i, x);
        }
    };
    ans.assign(m, -1);
    bool odd_cycle = false;
    function<void(int)> dfs = [&](int v) {
        for (int u : adj[v]) {
            if (ans[u] == -1) {
                chg(u, ans[v] ^ 1);
                dfs(u);
            } else if (ans[v] == ans[u]) {
                odd_cycle = true;
            }
        }
    };
    for (int i = 0; i < m; ++i) if (ans[i] == -1 && !adj[i].empty()) {
        chg(i, 0);
        dfs(i);
    }
    if (odd_cycle) return cout << "impossible\n", 0;
    for (int i = 0; i < n; ++i) {
        int ok = root.get2(i);
        if (~ok & 1) {
            int p = root.get_unknown(i);
            chg(p, 0);
        }
        if (~ok & 2) {
            int p = root.get_unknown(i);
            chg(p, 1);
        }
    }
    for (int i : ans) {
        cout << (i == -1 ? 0 : i);
    }
    cout << endl;
}

Compilation message

alternating.cpp: In constructor 'Seg::Seg(int, int)':
alternating.cpp:29:46: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   29 |     Seg (int _l, int _r) : l(_l), r(_r), m(l + r >> 1) {
      |                                            ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 2 ms 2636 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2636 KB Output is correct
29 Correct 2 ms 2636 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 2 ms 2636 KB Output is correct
32 Correct 2 ms 2636 KB Output is correct
33 Correct 2 ms 2636 KB Output is correct
34 Correct 2 ms 2636 KB Output is correct
35 Correct 2 ms 2636 KB Output is correct
36 Correct 2 ms 2636 KB Output is correct
37 Correct 2 ms 2636 KB Output is correct
38 Correct 2 ms 2636 KB Output is correct
39 Correct 2 ms 2636 KB Output is correct
40 Correct 2 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 2 ms 2636 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2636 KB Output is correct
29 Correct 2 ms 2636 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 2 ms 2636 KB Output is correct
32 Correct 2 ms 2636 KB Output is correct
33 Correct 2 ms 2636 KB Output is correct
34 Correct 2 ms 2636 KB Output is correct
35 Correct 2 ms 2636 KB Output is correct
36 Correct 2 ms 2636 KB Output is correct
37 Correct 2 ms 2636 KB Output is correct
38 Correct 2 ms 2636 KB Output is correct
39 Correct 2 ms 2636 KB Output is correct
40 Correct 2 ms 2636 KB Output is correct
41 Correct 2 ms 2636 KB Output is correct
42 Correct 2 ms 2636 KB Output is correct
43 Correct 2 ms 2636 KB Output is correct
44 Correct 2 ms 2636 KB Output is correct
45 Correct 2 ms 2636 KB Output is correct
46 Correct 2 ms 2636 KB Output is correct
47 Correct 2 ms 2636 KB Output is correct
48 Correct 2 ms 2636 KB Output is correct
49 Correct 2 ms 2636 KB Output is correct
50 Correct 2 ms 2636 KB Output is correct
51 Correct 2 ms 2636 KB Output is correct
52 Correct 2 ms 2636 KB Output is correct
53 Correct 2 ms 2636 KB Output is correct
54 Correct 2 ms 2636 KB Output is correct
55 Correct 2 ms 2636 KB Output is correct
56 Correct 2 ms 2636 KB Output is correct
57 Correct 3 ms 2764 KB Output is correct
58 Correct 2 ms 2636 KB Output is correct
59 Correct 2 ms 2636 KB Output is correct
60 Correct 2 ms 2636 KB Output is correct
61 Correct 2 ms 2636 KB Output is correct
62 Correct 2 ms 2636 KB Output is correct
63 Correct 2 ms 2636 KB Output is correct
64 Correct 2 ms 2636 KB Output is correct
65 Correct 2 ms 2636 KB Output is correct
66 Correct 2 ms 2636 KB Output is correct
67 Correct 2 ms 2636 KB Output is correct
68 Correct 2 ms 2636 KB Output is correct
69 Correct 2 ms 2564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 2 ms 2636 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2636 KB Output is correct
29 Correct 2 ms 2636 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 2 ms 2636 KB Output is correct
32 Correct 2 ms 2636 KB Output is correct
33 Correct 2 ms 2636 KB Output is correct
34 Correct 2 ms 2636 KB Output is correct
35 Correct 2 ms 2636 KB Output is correct
36 Correct 2 ms 2636 KB Output is correct
37 Correct 2 ms 2636 KB Output is correct
38 Correct 2 ms 2636 KB Output is correct
39 Correct 2 ms 2636 KB Output is correct
40 Correct 2 ms 2636 KB Output is correct
41 Correct 2 ms 2636 KB Output is correct
42 Correct 2 ms 2636 KB Output is correct
43 Correct 2 ms 2636 KB Output is correct
44 Correct 2 ms 2636 KB Output is correct
45 Correct 2 ms 2636 KB Output is correct
46 Correct 2 ms 2636 KB Output is correct
47 Correct 2 ms 2636 KB Output is correct
48 Correct 2 ms 2636 KB Output is correct
49 Correct 2 ms 2636 KB Output is correct
50 Correct 2 ms 2636 KB Output is correct
51 Correct 2 ms 2636 KB Output is correct
52 Correct 2 ms 2636 KB Output is correct
53 Correct 2 ms 2636 KB Output is correct
54 Correct 2 ms 2636 KB Output is correct
55 Correct 2 ms 2636 KB Output is correct
56 Correct 2 ms 2636 KB Output is correct
57 Correct 3 ms 2764 KB Output is correct
58 Correct 2 ms 2636 KB Output is correct
59 Correct 2 ms 2636 KB Output is correct
60 Correct 2 ms 2636 KB Output is correct
61 Correct 2 ms 2636 KB Output is correct
62 Correct 2 ms 2636 KB Output is correct
63 Correct 2 ms 2636 KB Output is correct
64 Correct 2 ms 2636 KB Output is correct
65 Correct 2 ms 2636 KB Output is correct
66 Correct 2 ms 2636 KB Output is correct
67 Correct 2 ms 2636 KB Output is correct
68 Correct 2 ms 2636 KB Output is correct
69 Correct 2 ms 2564 KB Output is correct
70 Correct 2 ms 2764 KB Output is correct
71 Correct 4 ms 3020 KB Output is correct
72 Correct 3 ms 2764 KB Output is correct
73 Correct 3 ms 2764 KB Output is correct
74 Correct 3 ms 2764 KB Output is correct
75 Correct 2 ms 2636 KB Output is correct
76 Correct 2 ms 2764 KB Output is correct
77 Correct 2 ms 2764 KB Output is correct
78 Correct 3 ms 2764 KB Output is correct
79 Correct 2 ms 2764 KB Output is correct
80 Correct 2 ms 2764 KB Output is correct
81 Correct 3 ms 2764 KB Output is correct
82 Correct 2 ms 2764 KB Output is correct
83 Correct 3 ms 2764 KB Output is correct
84 Correct 4 ms 2892 KB Output is correct
85 Correct 3 ms 2764 KB Output is correct
86 Correct 2 ms 2764 KB Output is correct
87 Correct 4 ms 2892 KB Output is correct
88 Correct 2 ms 2764 KB Output is correct
89 Correct 2 ms 2764 KB Output is correct
90 Correct 3 ms 2892 KB Output is correct
91 Correct 2 ms 2764 KB Output is correct
92 Correct 2 ms 2636 KB Output is correct
93 Correct 2 ms 2764 KB Output is correct
94 Correct 2 ms 2764 KB Output is correct
95 Correct 2 ms 2764 KB Output is correct
96 Correct 3 ms 2764 KB Output is correct
97 Correct 3 ms 2764 KB Output is correct
98 Correct 3 ms 2764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 20048 KB Output is correct
2 Correct 58 ms 19184 KB Output is correct
3 Correct 54 ms 24740 KB Output is correct
4 Correct 73 ms 25024 KB Output is correct
5 Correct 308 ms 26084 KB Output is correct
6 Correct 118 ms 23492 KB Output is correct
7 Correct 281 ms 25964 KB Output is correct
8 Correct 95 ms 19484 KB Output is correct
9 Correct 73 ms 18296 KB Output is correct
10 Correct 334 ms 26176 KB Output is correct
11 Correct 276 ms 25172 KB Output is correct
12 Correct 214 ms 25644 KB Output is correct
13 Correct 72 ms 19180 KB Output is correct
14 Correct 77 ms 19156 KB Output is correct
15 Correct 149 ms 22240 KB Output is correct
16 Correct 59 ms 24772 KB Output is correct
17 Correct 227 ms 30164 KB Output is correct
18 Correct 82 ms 10820 KB Output is correct
19 Correct 25 ms 19440 KB Output is correct
20 Correct 126 ms 16384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 2 ms 2636 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2636 KB Output is correct
29 Correct 2 ms 2636 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 2 ms 2636 KB Output is correct
32 Correct 2 ms 2636 KB Output is correct
33 Correct 2 ms 2636 KB Output is correct
34 Correct 2 ms 2636 KB Output is correct
35 Correct 2 ms 2636 KB Output is correct
36 Correct 2 ms 2636 KB Output is correct
37 Correct 2 ms 2636 KB Output is correct
38 Correct 2 ms 2636 KB Output is correct
39 Correct 2 ms 2636 KB Output is correct
40 Correct 2 ms 2636 KB Output is correct
41 Correct 2 ms 2636 KB Output is correct
42 Correct 2 ms 2636 KB Output is correct
43 Correct 2 ms 2636 KB Output is correct
44 Correct 2 ms 2636 KB Output is correct
45 Correct 2 ms 2636 KB Output is correct
46 Correct 2 ms 2636 KB Output is correct
47 Correct 2 ms 2636 KB Output is correct
48 Correct 2 ms 2636 KB Output is correct
49 Correct 2 ms 2636 KB Output is correct
50 Correct 2 ms 2636 KB Output is correct
51 Correct 2 ms 2636 KB Output is correct
52 Correct 2 ms 2636 KB Output is correct
53 Correct 2 ms 2636 KB Output is correct
54 Correct 2 ms 2636 KB Output is correct
55 Correct 2 ms 2636 KB Output is correct
56 Correct 2 ms 2636 KB Output is correct
57 Correct 3 ms 2764 KB Output is correct
58 Correct 2 ms 2636 KB Output is correct
59 Correct 2 ms 2636 KB Output is correct
60 Correct 2 ms 2636 KB Output is correct
61 Correct 2 ms 2636 KB Output is correct
62 Correct 2 ms 2636 KB Output is correct
63 Correct 2 ms 2636 KB Output is correct
64 Correct 2 ms 2636 KB Output is correct
65 Correct 2 ms 2636 KB Output is correct
66 Correct 2 ms 2636 KB Output is correct
67 Correct 2 ms 2636 KB Output is correct
68 Correct 2 ms 2636 KB Output is correct
69 Correct 2 ms 2564 KB Output is correct
70 Correct 2 ms 2764 KB Output is correct
71 Correct 4 ms 3020 KB Output is correct
72 Correct 3 ms 2764 KB Output is correct
73 Correct 3 ms 2764 KB Output is correct
74 Correct 3 ms 2764 KB Output is correct
75 Correct 2 ms 2636 KB Output is correct
76 Correct 2 ms 2764 KB Output is correct
77 Correct 2 ms 2764 KB Output is correct
78 Correct 3 ms 2764 KB Output is correct
79 Correct 2 ms 2764 KB Output is correct
80 Correct 2 ms 2764 KB Output is correct
81 Correct 3 ms 2764 KB Output is correct
82 Correct 2 ms 2764 KB Output is correct
83 Correct 3 ms 2764 KB Output is correct
84 Correct 4 ms 2892 KB Output is correct
85 Correct 3 ms 2764 KB Output is correct
86 Correct 2 ms 2764 KB Output is correct
87 Correct 4 ms 2892 KB Output is correct
88 Correct 2 ms 2764 KB Output is correct
89 Correct 2 ms 2764 KB Output is correct
90 Correct 3 ms 2892 KB Output is correct
91 Correct 2 ms 2764 KB Output is correct
92 Correct 2 ms 2636 KB Output is correct
93 Correct 2 ms 2764 KB Output is correct
94 Correct 2 ms 2764 KB Output is correct
95 Correct 2 ms 2764 KB Output is correct
96 Correct 3 ms 2764 KB Output is correct
97 Correct 3 ms 2764 KB Output is correct
98 Correct 3 ms 2764 KB Output is correct
99 Correct 56 ms 20048 KB Output is correct
100 Correct 58 ms 19184 KB Output is correct
101 Correct 54 ms 24740 KB Output is correct
102 Correct 73 ms 25024 KB Output is correct
103 Correct 308 ms 26084 KB Output is correct
104 Correct 118 ms 23492 KB Output is correct
105 Correct 281 ms 25964 KB Output is correct
106 Correct 95 ms 19484 KB Output is correct
107 Correct 73 ms 18296 KB Output is correct
108 Correct 334 ms 26176 KB Output is correct
109 Correct 276 ms 25172 KB Output is correct
110 Correct 214 ms 25644 KB Output is correct
111 Correct 72 ms 19180 KB Output is correct
112 Correct 77 ms 19156 KB Output is correct
113 Correct 149 ms 22240 KB Output is correct
114 Correct 59 ms 24772 KB Output is correct
115 Correct 227 ms 30164 KB Output is correct
116 Correct 82 ms 10820 KB Output is correct
117 Correct 25 ms 19440 KB Output is correct
118 Correct 126 ms 16384 KB Output is correct
119 Correct 101 ms 19308 KB Output is correct
120 Correct 271 ms 35652 KB Output is correct
121 Correct 222 ms 25796 KB Output is correct
122 Correct 2 ms 2636 KB Output is correct
123 Correct 40 ms 19256 KB Output is correct
124 Correct 78 ms 21844 KB Output is correct
125 Correct 166 ms 22396 KB Output is correct
126 Correct 90 ms 19196 KB Output is correct
127 Correct 94 ms 20092 KB Output is correct
128 Correct 280 ms 26208 KB Output is correct
129 Correct 221 ms 24124 KB Output is correct
130 Correct 243 ms 24744 KB Output is correct
131 Correct 85 ms 19224 KB Output is correct
132 Correct 102 ms 19396 KB Output is correct
133 Correct 234 ms 31556 KB Output is correct
134 Correct 86 ms 22980 KB Output is correct
135 Correct 35 ms 18380 KB Output is correct
136 Correct 92 ms 23348 KB Output is correct
137 Correct 122 ms 24260 KB Output is correct
138 Correct 35 ms 18508 KB Output is correct