Submission #442083

# Submission time Handle Problem Language Result Execution time Memory
442083 2021-07-07T06:04:27 Z abc864197532 Alternating Current (BOI18_alternating) C++17
100 / 100
344 ms 19492 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;

struct Seg {
    int l, r, m, mn, lz;
    Seg* ch[2];
    Seg (int _l, int _r) : l(_l), r(_r), m(l + r >> 1), mn(0), lz(0) {
        if (r - l > 1) {
            ch[0] = new Seg(l, m);
            ch[1] = new Seg(m, r);
        }
    }
    void pull() {mn = min(ch[0]->mn, ch[1]->mn);}
    void give(int v) {mn += v, lz += v;}
    void push() {ch[0]->give(lz), ch[1]->give(lz), lz = 0;}
    void add(int a, int b, int v) {
        if (a <= l && r <= b) give(v);
        else {
            push();
            if (a < m) ch[0]->add(a, b, v);
            if (m < b) ch[1]->add(a, b, v);
            pull();
        }
    }
    int query(int a, int b) {
        if (a <= l && r <= b) return mn;
        push();
        int ans = 1 << 30;
        if (a < m) ans = min(ans, ch[0]->query(a, b));
        if (m < b) ans = min(ans, ch[1]->query(a, b));
        return ans;
    }
};

int color[N];
bool odd_cycle;
vector <int> adj[N];

void dfs(int v) {
    for (int u : adj[v]) {
        if (!color[u]) {
            color[u] = 3 ^ color[v];
            dfs(u);
        } else if (color[u] == color[v]) {
            odd_cycle = true;
        }
    }
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    vector <int> l(m), r(m);
    vector <pii> event;
    for (int i = 0; i < m; ++i) {
        cin >> l[i] >> r[i], --l[i];
        if (l[i] != r[i]) event.eb(l[i], i), event.eb(r[i], i);
        if (l[i] >= r[i]) event.eb(0, i);
    }
    sort(all(event));
    set <int> on;
    for (int i = 0, j = 0; i < n; ++i) {
        for (; j < event.size() && event[j].X == i; ++j) {
            if (on.count(event[j].Y)) on.erase(event[j].Y);
            else on.insert(event[j].Y);
        }
        if (on.size() < 2) return cout << "impossible\n", 0;
        else if (on.size() == 2) {
            int u = *on.begin(), v = *on.rbegin();
            adj[u].pb(v), adj[v].pb(u);
        }
    }
    for (int i = 0; i < m; ++i) if (!color[i] && !adj[i].empty()) {
        color[i] = 1;
        dfs(i);
    }
    if (odd_cycle) return cout << "impossible\n", 0;
    Seg root(0, n);
    auto add = [&](int i, int v) {
        if (l[i] >= r[i]) root.add(l[i], n, v), root.add(0, r[i], v);
        else root.add(l[i], r[i], v);
    };
    auto qry = [&](int i) {
        if (l[i] >= r[i]) return min(root.query(l[i], n), root.query(0, r[i]));
        return root.query(l[i], r[i]);
    };
    for (int i = 0; i < m; ++i) if (color[i] != 1) add(i, 1);
    for (int i = 0; i < m; ++i) if (!color[i]) {
        if (qry(i) == 1) color[i] = 2;
        else color[i] = 1, add(i, -1);
    }
    for (int i = 0; i < m; ++i) cout << color[i] - 1;
    cout << endl;
}

Compilation message

alternating.cpp: In constructor 'Seg::Seg(int, int)':
alternating.cpp:25:46: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   25 |     Seg (int _l, int _r) : l(_l), r(_r), m(l + r >> 1), mn(0), lz(0) {
      |                                            ~~^~~
alternating.cpp: In function 'int main()':
alternating.cpp:83:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |         for (; j < event.size() && event[j].X == i; ++j) {
      |                ~~^~~~~~~~~~~~~~
# 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 2560 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 2560 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 2 ms 2636 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 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 2560 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 2 ms 2636 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 2636 KB Output is correct
70 Correct 2 ms 2764 KB Output is correct
71 Correct 2 ms 2764 KB Output is correct
72 Correct 2 ms 2764 KB Output is correct
73 Correct 2 ms 2764 KB Output is correct
74 Correct 2 ms 2764 KB Output is correct
75 Correct 2 ms 2636 KB Output is correct
76 Correct 2 ms 2636 KB Output is correct
77 Correct 2 ms 2764 KB Output is correct
78 Correct 2 ms 2636 KB Output is correct
79 Correct 2 ms 2636 KB Output is correct
80 Correct 2 ms 2636 KB Output is correct
81 Correct 2 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 3 ms 2764 KB Output is correct
85 Correct 3 ms 2764 KB Output is correct
86 Correct 2 ms 2764 KB Output is correct
87 Correct 3 ms 2764 KB Output is correct
88 Correct 2 ms 2764 KB Output is correct
89 Correct 2 ms 2636 KB Output is correct
90 Correct 3 ms 2764 KB Output is correct
91 Correct 2 ms 2764 KB Output is correct
92 Correct 3 ms 2636 KB Output is correct
93 Correct 2 ms 2764 KB Output is correct
94 Correct 2 ms 2636 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 2 ms 2764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 19492 KB Output is correct
2 Correct 15 ms 12736 KB Output is correct
3 Correct 18 ms 4172 KB Output is correct
4 Correct 101 ms 13504 KB Output is correct
5 Correct 102 ms 17788 KB Output is correct
6 Correct 46 ms 5540 KB Output is correct
7 Correct 88 ms 17652 KB Output is correct
8 Correct 17 ms 12876 KB Output is correct
9 Correct 13 ms 12220 KB Output is correct
10 Correct 110 ms 18192 KB Output is correct
11 Correct 78 ms 16700 KB Output is correct
12 Correct 75 ms 17792 KB Output is correct
13 Correct 4 ms 3700 KB Output is correct
14 Correct 4 ms 3532 KB Output is correct
15 Correct 163 ms 14928 KB Output is correct
16 Correct 34 ms 6088 KB Output is correct
17 Correct 321 ms 14796 KB Output is correct
18 Correct 225 ms 7868 KB Output is correct
19 Correct 4 ms 2892 KB Output is correct
20 Correct 73 ms 13244 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 2560 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 2 ms 2636 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 2636 KB Output is correct
70 Correct 2 ms 2764 KB Output is correct
71 Correct 2 ms 2764 KB Output is correct
72 Correct 2 ms 2764 KB Output is correct
73 Correct 2 ms 2764 KB Output is correct
74 Correct 2 ms 2764 KB Output is correct
75 Correct 2 ms 2636 KB Output is correct
76 Correct 2 ms 2636 KB Output is correct
77 Correct 2 ms 2764 KB Output is correct
78 Correct 2 ms 2636 KB Output is correct
79 Correct 2 ms 2636 KB Output is correct
80 Correct 2 ms 2636 KB Output is correct
81 Correct 2 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 3 ms 2764 KB Output is correct
85 Correct 3 ms 2764 KB Output is correct
86 Correct 2 ms 2764 KB Output is correct
87 Correct 3 ms 2764 KB Output is correct
88 Correct 2 ms 2764 KB Output is correct
89 Correct 2 ms 2636 KB Output is correct
90 Correct 3 ms 2764 KB Output is correct
91 Correct 2 ms 2764 KB Output is correct
92 Correct 3 ms 2636 KB Output is correct
93 Correct 2 ms 2764 KB Output is correct
94 Correct 2 ms 2636 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 2 ms 2764 KB Output is correct
99 Correct 79 ms 19492 KB Output is correct
100 Correct 15 ms 12736 KB Output is correct
101 Correct 18 ms 4172 KB Output is correct
102 Correct 101 ms 13504 KB Output is correct
103 Correct 102 ms 17788 KB Output is correct
104 Correct 46 ms 5540 KB Output is correct
105 Correct 88 ms 17652 KB Output is correct
106 Correct 17 ms 12876 KB Output is correct
107 Correct 13 ms 12220 KB Output is correct
108 Correct 110 ms 18192 KB Output is correct
109 Correct 78 ms 16700 KB Output is correct
110 Correct 75 ms 17792 KB Output is correct
111 Correct 4 ms 3700 KB Output is correct
112 Correct 4 ms 3532 KB Output is correct
113 Correct 163 ms 14928 KB Output is correct
114 Correct 34 ms 6088 KB Output is correct
115 Correct 321 ms 14796 KB Output is correct
116 Correct 225 ms 7868 KB Output is correct
117 Correct 4 ms 2892 KB Output is correct
118 Correct 73 ms 13244 KB Output is correct
119 Correct 13 ms 12984 KB Output is correct
120 Correct 82 ms 19272 KB Output is correct
121 Correct 58 ms 15512 KB Output is correct
122 Correct 2 ms 2636 KB Output is correct
123 Correct 4 ms 2892 KB Output is correct
124 Correct 20 ms 4044 KB Output is correct
125 Correct 47 ms 14020 KB Output is correct
126 Correct 14 ms 12796 KB Output is correct
127 Correct 25 ms 12852 KB Output is correct
128 Correct 86 ms 16960 KB Output is correct
129 Correct 52 ms 15428 KB Output is correct
130 Correct 66 ms 15812 KB Output is correct
131 Correct 13 ms 12872 KB Output is correct
132 Correct 13 ms 13056 KB Output is correct
133 Correct 344 ms 17588 KB Output is correct
134 Correct 97 ms 13496 KB Output is correct
135 Correct 12 ms 12100 KB Output is correct
136 Correct 26 ms 4192 KB Output is correct
137 Correct 50 ms 5416 KB Output is correct
138 Correct 13 ms 11980 KB Output is correct