답안 #441753

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
441753 2021-07-06T02:14:22 Z abc864197532 Alternating Current (BOI18_alternating) C++17
100 / 100
347 ms 36056 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;
    vector <int> pp(n);
    iota(all(pp), 0);
    random_shuffle(all(pp));
    for (int i : pp) {
        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) {
      |                                            ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2764 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 2656 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 2660 KB Output is correct
10 Correct 2 ms 2664 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 2660 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 2608 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 2 ms 2660 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 2660 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 2664 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
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2764 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 2656 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 2660 KB Output is correct
10 Correct 2 ms 2664 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 2660 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 2608 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 2 ms 2660 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 2660 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 2664 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 2664 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 2660 KB Output is correct
57 Correct 2 ms 2636 KB Output is correct
58 Correct 2 ms 2636 KB Output is correct
59 Correct 2 ms 2668 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 2660 KB Output is correct
68 Correct 2 ms 2636 KB Output is correct
69 Correct 2 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2764 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 2656 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 2660 KB Output is correct
10 Correct 2 ms 2664 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 2660 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 2608 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 2 ms 2660 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 2660 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 2664 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 2664 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 2660 KB Output is correct
57 Correct 2 ms 2636 KB Output is correct
58 Correct 2 ms 2636 KB Output is correct
59 Correct 2 ms 2668 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 2660 KB Output is correct
68 Correct 2 ms 2636 KB Output is correct
69 Correct 2 ms 2636 KB Output is correct
70 Correct 2 ms 2764 KB Output is correct
71 Correct 5 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 3 ms 2764 KB Output is correct
78 Correct 3 ms 2808 KB Output is correct
79 Correct 2 ms 2788 KB Output is correct
80 Correct 3 ms 2764 KB Output is correct
81 Correct 3 ms 2784 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 3 ms 2764 KB Output is correct
87 Correct 3 ms 2892 KB Output is correct
88 Correct 3 ms 2764 KB Output is correct
89 Correct 3 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
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 21280 KB Output is correct
2 Correct 106 ms 19560 KB Output is correct
3 Correct 57 ms 25380 KB Output is correct
4 Correct 117 ms 25980 KB Output is correct
5 Correct 346 ms 27436 KB Output is correct
6 Correct 129 ms 24772 KB Output is correct
7 Correct 321 ms 27408 KB Output is correct
8 Correct 145 ms 19844 KB Output is correct
9 Correct 114 ms 18708 KB Output is correct
10 Correct 347 ms 27752 KB Output is correct
11 Correct 315 ms 26308 KB Output is correct
12 Correct 251 ms 27056 KB Output is correct
13 Correct 74 ms 19252 KB Output is correct
14 Correct 76 ms 19140 KB Output is correct
15 Correct 201 ms 23888 KB Output is correct
16 Correct 64 ms 25284 KB Output is correct
17 Correct 281 ms 31660 KB Output is correct
18 Correct 78 ms 11716 KB Output is correct
19 Correct 28 ms 19488 KB Output is correct
20 Correct 143 ms 17792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2764 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 2656 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 2660 KB Output is correct
10 Correct 2 ms 2664 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 2660 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 2608 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 2 ms 2660 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 2660 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 2664 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 2664 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 2660 KB Output is correct
57 Correct 2 ms 2636 KB Output is correct
58 Correct 2 ms 2636 KB Output is correct
59 Correct 2 ms 2668 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 2660 KB Output is correct
68 Correct 2 ms 2636 KB Output is correct
69 Correct 2 ms 2636 KB Output is correct
70 Correct 2 ms 2764 KB Output is correct
71 Correct 5 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 3 ms 2764 KB Output is correct
78 Correct 3 ms 2808 KB Output is correct
79 Correct 2 ms 2788 KB Output is correct
80 Correct 3 ms 2764 KB Output is correct
81 Correct 3 ms 2784 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 3 ms 2764 KB Output is correct
87 Correct 3 ms 2892 KB Output is correct
88 Correct 3 ms 2764 KB Output is correct
89 Correct 3 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 100 ms 21280 KB Output is correct
100 Correct 106 ms 19560 KB Output is correct
101 Correct 57 ms 25380 KB Output is correct
102 Correct 117 ms 25980 KB Output is correct
103 Correct 346 ms 27436 KB Output is correct
104 Correct 129 ms 24772 KB Output is correct
105 Correct 321 ms 27408 KB Output is correct
106 Correct 145 ms 19844 KB Output is correct
107 Correct 114 ms 18708 KB Output is correct
108 Correct 347 ms 27752 KB Output is correct
109 Correct 315 ms 26308 KB Output is correct
110 Correct 251 ms 27056 KB Output is correct
111 Correct 74 ms 19252 KB Output is correct
112 Correct 76 ms 19140 KB Output is correct
113 Correct 201 ms 23888 KB Output is correct
114 Correct 64 ms 25284 KB Output is correct
115 Correct 281 ms 31660 KB Output is correct
116 Correct 78 ms 11716 KB Output is correct
117 Correct 28 ms 19488 KB Output is correct
118 Correct 143 ms 17792 KB Output is correct
119 Correct 148 ms 19692 KB Output is correct
120 Correct 323 ms 36056 KB Output is correct
121 Correct 258 ms 26180 KB Output is correct
122 Correct 2 ms 2636 KB Output is correct
123 Correct 43 ms 19296 KB Output is correct
124 Correct 80 ms 21828 KB Output is correct
125 Correct 214 ms 22808 KB Output is correct
126 Correct 133 ms 19652 KB Output is correct
127 Correct 144 ms 20460 KB Output is correct
128 Correct 332 ms 26512 KB Output is correct
129 Correct 258 ms 24388 KB Output is correct
130 Correct 277 ms 25096 KB Output is correct
131 Correct 132 ms 19776 KB Output is correct
132 Correct 147 ms 19792 KB Output is correct
133 Correct 274 ms 31920 KB Output is correct
134 Correct 131 ms 23592 KB Output is correct
135 Correct 76 ms 18780 KB Output is correct
136 Correct 91 ms 23364 KB Output is correct
137 Correct 115 ms 24344 KB Output is correct
138 Correct 83 ms 18948 KB Output is correct