Submission #442082

# Submission time Handle Problem Language Result Execution time Memory
442082 2021-07-07T06:01:12 Z abc864197532 Alternating Current (BOI18_alternating) C++17
100 / 100
375 ms 20056 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 3 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 3 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 2764 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 2668 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 2672 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 3 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 3 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 2764 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 2668 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 2672 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 2672 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 3 ms 2668 KB Output is correct
46 Correct 2 ms 2636 KB Output is correct
47 Correct 2 ms 2668 KB Output is correct
48 Correct 3 ms 2672 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 2648 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 2576 KB Output is correct
56 Correct 2 ms 2636 KB Output is correct
57 Correct 3 ms 2636 KB Output is correct
58 Correct 3 ms 2672 KB Output is correct
59 Correct 2 ms 2636 KB Output is correct
60 Correct 2 ms 2672 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 3 ms 2636 KB Output is correct
69 Correct 2 ms 2764 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 3 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 3 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 2764 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 2668 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 2672 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 2672 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 3 ms 2668 KB Output is correct
46 Correct 2 ms 2636 KB Output is correct
47 Correct 2 ms 2668 KB Output is correct
48 Correct 3 ms 2672 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 2648 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 2576 KB Output is correct
56 Correct 2 ms 2636 KB Output is correct
57 Correct 3 ms 2636 KB Output is correct
58 Correct 3 ms 2672 KB Output is correct
59 Correct 2 ms 2636 KB Output is correct
60 Correct 2 ms 2672 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 3 ms 2636 KB Output is correct
69 Correct 2 ms 2764 KB Output is correct
70 Correct 3 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 3 ms 2764 KB Output is correct
78 Correct 2 ms 2636 KB Output is correct
79 Correct 2 ms 2672 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 2 ms 2668 KB Output is correct
86 Correct 3 ms 2764 KB Output is correct
87 Correct 3 ms 2764 KB Output is correct
88 Correct 3 ms 2672 KB Output is correct
89 Correct 2 ms 2676 KB Output is correct
90 Correct 4 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 2668 KB Output is correct
95 Correct 2 ms 2764 KB Output is correct
96 Correct 4 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 83 ms 19624 KB Output is correct
2 Correct 14 ms 12880 KB Output is correct
3 Correct 19 ms 4772 KB Output is correct
4 Correct 98 ms 14040 KB Output is correct
5 Correct 110 ms 18916 KB Output is correct
6 Correct 50 ms 6652 KB Output is correct
7 Correct 95 ms 18740 KB Output is correct
8 Correct 17 ms 12808 KB Output is correct
9 Correct 13 ms 12236 KB Output is correct
10 Correct 108 ms 19272 KB Output is correct
11 Correct 93 ms 17596 KB Output is correct
12 Correct 80 ms 18828 KB Output is correct
13 Correct 4 ms 3700 KB Output is correct
14 Correct 4 ms 3532 KB Output is correct
15 Correct 179 ms 15948 KB Output is correct
16 Correct 36 ms 6724 KB Output is correct
17 Correct 337 ms 16044 KB Output is correct
18 Correct 227 ms 8764 KB Output is correct
19 Correct 4 ms 2892 KB Output is correct
20 Correct 77 ms 14360 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 3 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 3 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 2764 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 2668 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 2672 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 2672 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 3 ms 2668 KB Output is correct
46 Correct 2 ms 2636 KB Output is correct
47 Correct 2 ms 2668 KB Output is correct
48 Correct 3 ms 2672 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 2648 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 2576 KB Output is correct
56 Correct 2 ms 2636 KB Output is correct
57 Correct 3 ms 2636 KB Output is correct
58 Correct 3 ms 2672 KB Output is correct
59 Correct 2 ms 2636 KB Output is correct
60 Correct 2 ms 2672 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 3 ms 2636 KB Output is correct
69 Correct 2 ms 2764 KB Output is correct
70 Correct 3 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 3 ms 2764 KB Output is correct
78 Correct 2 ms 2636 KB Output is correct
79 Correct 2 ms 2672 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 2 ms 2668 KB Output is correct
86 Correct 3 ms 2764 KB Output is correct
87 Correct 3 ms 2764 KB Output is correct
88 Correct 3 ms 2672 KB Output is correct
89 Correct 2 ms 2676 KB Output is correct
90 Correct 4 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 2668 KB Output is correct
95 Correct 2 ms 2764 KB Output is correct
96 Correct 4 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 83 ms 19624 KB Output is correct
100 Correct 14 ms 12880 KB Output is correct
101 Correct 19 ms 4772 KB Output is correct
102 Correct 98 ms 14040 KB Output is correct
103 Correct 110 ms 18916 KB Output is correct
104 Correct 50 ms 6652 KB Output is correct
105 Correct 95 ms 18740 KB Output is correct
106 Correct 17 ms 12808 KB Output is correct
107 Correct 13 ms 12236 KB Output is correct
108 Correct 108 ms 19272 KB Output is correct
109 Correct 93 ms 17596 KB Output is correct
110 Correct 80 ms 18828 KB Output is correct
111 Correct 4 ms 3700 KB Output is correct
112 Correct 4 ms 3532 KB Output is correct
113 Correct 179 ms 15948 KB Output is correct
114 Correct 36 ms 6724 KB Output is correct
115 Correct 337 ms 16044 KB Output is correct
116 Correct 227 ms 8764 KB Output is correct
117 Correct 4 ms 2892 KB Output is correct
118 Correct 77 ms 14360 KB Output is correct
119 Correct 13 ms 13004 KB Output is correct
120 Correct 92 ms 20056 KB Output is correct
121 Correct 75 ms 16112 KB Output is correct
122 Correct 2 ms 2636 KB Output is correct
123 Correct 5 ms 3068 KB Output is correct
124 Correct 20 ms 4556 KB Output is correct
125 Correct 46 ms 14292 KB Output is correct
126 Correct 17 ms 12880 KB Output is correct
127 Correct 32 ms 13052 KB Output is correct
128 Correct 99 ms 17980 KB Output is correct
129 Correct 54 ms 16000 KB Output is correct
130 Correct 68 ms 16460 KB Output is correct
131 Correct 13 ms 12856 KB Output is correct
132 Correct 14 ms 13080 KB Output is correct
133 Correct 375 ms 18728 KB Output is correct
134 Correct 100 ms 13880 KB Output is correct
135 Correct 11 ms 12108 KB Output is correct
136 Correct 29 ms 4812 KB Output is correct
137 Correct 47 ms 6388 KB Output is correct
138 Correct 14 ms 12108 KB Output is correct