Submission #505180

# Submission time Handle Problem Language Result Execution time Memory
505180 2022-01-10T23:01:54 Z mjhmjh1104 Alternating Current (BOI18_alternating) C++17
100 / 100
286 ms 12208 KB
#include <set>
#include <cstdio>
#include <vector>
#include <utility>
#include <iterator>
#include <algorithm>
using namespace std;

int tree[262144], lazy[262144];

void propagate(int i, int b, int e) {
    if (!lazy[i]) return;
    if (b != e) {
        lazy[i * 2 + 1] += lazy[i];
        lazy[i * 2 + 2] += lazy[i];
    }
    tree[i] += lazy[i];
    lazy[i] = 0;
}

int query(int i, int b, int e, int l, int r) {
    propagate(i, b, e);
    if (r < b || e < l) return (int)1e9;
    if (l <= b && e <= r) return tree[i];
    int m = (b + e) / 2;
    return min(query(i * 2 + 1, b, m, l, r), query(i * 2 + 2, m + 1, e, l, r));
}

int update(int i, int b, int e, int l, int r, int v) {
    propagate(i, b, e);
    if (r < b || e < l) return tree[i];
    if (l <= b && e <= r) {
        lazy[i] += v;
        propagate(i, b, e);
        return tree[i];
    }
    int m = (b + e) / 2;
    return tree[i] = min(update(i * 2 + 1, b, m, l, r, v), update(i * 2 + 2, m + 1, e, l, r, v));
}

int n, m, x[100006], y[100006];
vector<int> adj[100006];
set<int> s;
vector<pair<int, int>> v;
int cl[100006];

bool dfs(int x) {
    for (auto &i: adj[x]) {
        if (!cl[i]) {
            cl[i] = -cl[x];
            if (dfs(i)) return true;
        } else if (cl[i] == cl[x]) return true;
    }
    return false;
}

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 0; i < m; i++) {
        scanf("%d%d", x + i, y + i);
        x[i]--, y[i]--;
        if (x[i] > y[i]) s.insert(i);
        v.push_back({ x[i], -i - 1 });
        v.push_back({ y[i], i });
    }
    sort(v.begin(), v.end());
    int k = 0;
    for (int i = 0; i < n; i++) {
        while (k < (int)v.size() && v[k] < make_pair(i, 0)) {
            if (v[k].second < 0) s.insert(-v[k].second - 1);
            else s.erase(v[k].second);
            k++;
        }
        if ((int)s.size() < 2) return puts("impossible"), 0;
        if ((int)s.size() == 2) {
            int first = *s.begin();
            int second = *next(s.begin());
            adj[first].push_back(second);
            adj[second].push_back(first);
        }
    }
    for (int i = 0; i < m; i++) if (!cl[i] && !adj[i].empty()) {
        cl[i] = -1;
        if (dfs(i)) return puts("impossible"), 0;
    }
    for (int i = 0; i < m; i++) if (cl[i] >= 0) {
        if (x[i] <= y[i]) update(0, 0, 131071, x[i], y[i], 1);
        else update(0, 0, 131071, 0, y[i], 1), update(0, 0, 131071, x[i], n - 1, 1);
    }
    for (int i = 0; i < m; i++) if (!cl[i]) {
        int t = 0;
        if (x[i] <= y[i]) t = query(0, 0, 131071, x[i], y[i]);
        else t = min(query(0, 0, 131071, 0, y[i]), query(0, 0, 131071, x[i], n - 1));
        if (t <= 1) cl[i] = 1;
        else {
            cl[i] = -1;
            if (x[i] <= y[i]) update(0, 0, 131071, x[i], y[i], -1);
            else update(0, 0, 131071, 0, y[i], -1), update(0, 0, 131071, x[i], n - 1, -1);
        }
    }
    for (int i = 0; i < m; i++) {
        if (cl[i] == -1) printf("0");
        else printf("1");
    }
}

Compilation message

alternating.cpp: In function 'int main()':
alternating.cpp:58:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
alternating.cpp:60:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |         scanf("%d%d", x + i, y + i);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 1 ms 2636 KB Output is correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 1 ms 2636 KB Output is correct
8 Correct 1 ms 2636 KB Output is correct
9 Correct 1 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 1 ms 2640 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 1 ms 2636 KB Output is correct
14 Correct 1 ms 2636 KB Output is correct
15 Correct 1 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 1 ms 2692 KB Output is correct
18 Correct 1 ms 2640 KB Output is correct
19 Correct 1 ms 2636 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 1 ms 2636 KB Output is correct
22 Correct 1 ms 2612 KB Output is correct
23 Correct 1 ms 2636 KB Output is correct
24 Correct 2 ms 2640 KB Output is correct
25 Correct 1 ms 2636 KB Output is correct
26 Correct 1 ms 2636 KB Output is correct
27 Correct 1 ms 2636 KB Output is correct
28 Correct 1 ms 2636 KB Output is correct
29 Correct 1 ms 2636 KB Output is correct
30 Correct 2 ms 2640 KB Output is correct
31 Correct 1 ms 2636 KB Output is correct
32 Correct 1 ms 2636 KB Output is correct
33 Correct 1 ms 2636 KB Output is correct
34 Correct 1 ms 2636 KB Output is correct
35 Correct 1 ms 2636 KB Output is correct
36 Correct 1 ms 2628 KB Output is correct
37 Correct 1 ms 2636 KB Output is correct
38 Correct 1 ms 2636 KB Output is correct
39 Correct 2 ms 2636 KB Output is correct
40 Correct 1 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 1 ms 2636 KB Output is correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 1 ms 2636 KB Output is correct
8 Correct 1 ms 2636 KB Output is correct
9 Correct 1 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 1 ms 2640 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 1 ms 2636 KB Output is correct
14 Correct 1 ms 2636 KB Output is correct
15 Correct 1 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 1 ms 2692 KB Output is correct
18 Correct 1 ms 2640 KB Output is correct
19 Correct 1 ms 2636 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 1 ms 2636 KB Output is correct
22 Correct 1 ms 2612 KB Output is correct
23 Correct 1 ms 2636 KB Output is correct
24 Correct 2 ms 2640 KB Output is correct
25 Correct 1 ms 2636 KB Output is correct
26 Correct 1 ms 2636 KB Output is correct
27 Correct 1 ms 2636 KB Output is correct
28 Correct 1 ms 2636 KB Output is correct
29 Correct 1 ms 2636 KB Output is correct
30 Correct 2 ms 2640 KB Output is correct
31 Correct 1 ms 2636 KB Output is correct
32 Correct 1 ms 2636 KB Output is correct
33 Correct 1 ms 2636 KB Output is correct
34 Correct 1 ms 2636 KB Output is correct
35 Correct 1 ms 2636 KB Output is correct
36 Correct 1 ms 2628 KB Output is correct
37 Correct 1 ms 2636 KB Output is correct
38 Correct 1 ms 2636 KB Output is correct
39 Correct 2 ms 2636 KB Output is correct
40 Correct 1 ms 2636 KB Output is correct
41 Correct 1 ms 2636 KB Output is correct
42 Correct 1 ms 2636 KB Output is correct
43 Correct 1 ms 2636 KB Output is correct
44 Correct 1 ms 2636 KB Output is correct
45 Correct 2 ms 2684 KB Output is correct
46 Correct 2 ms 2636 KB Output is correct
47 Correct 2 ms 2636 KB Output is correct
48 Correct 1 ms 2636 KB Output is correct
49 Correct 1 ms 2636 KB Output is correct
50 Correct 1 ms 2640 KB Output is correct
51 Correct 1 ms 2636 KB Output is correct
52 Correct 1 ms 2636 KB Output is correct
53 Correct 1 ms 2636 KB Output is correct
54 Correct 2 ms 2640 KB Output is correct
55 Correct 2 ms 2636 KB Output is correct
56 Correct 2 ms 2636 KB Output is correct
57 Correct 1 ms 2636 KB Output is correct
58 Correct 1 ms 2636 KB Output is correct
59 Correct 1 ms 2636 KB Output is correct
60 Correct 2 ms 2636 KB Output is correct
61 Correct 1 ms 2644 KB Output is correct
62 Correct 1 ms 2636 KB Output is correct
63 Correct 2 ms 2636 KB Output is correct
64 Correct 1 ms 2632 KB Output is correct
65 Correct 1 ms 2640 KB Output is correct
66 Correct 1 ms 2664 KB Output is correct
67 Correct 2 ms 2644 KB Output is correct
68 Correct 2 ms 2644 KB Output is correct
69 Correct 1 ms 2640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 1 ms 2636 KB Output is correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 1 ms 2636 KB Output is correct
8 Correct 1 ms 2636 KB Output is correct
9 Correct 1 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 1 ms 2640 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 1 ms 2636 KB Output is correct
14 Correct 1 ms 2636 KB Output is correct
15 Correct 1 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 1 ms 2692 KB Output is correct
18 Correct 1 ms 2640 KB Output is correct
19 Correct 1 ms 2636 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 1 ms 2636 KB Output is correct
22 Correct 1 ms 2612 KB Output is correct
23 Correct 1 ms 2636 KB Output is correct
24 Correct 2 ms 2640 KB Output is correct
25 Correct 1 ms 2636 KB Output is correct
26 Correct 1 ms 2636 KB Output is correct
27 Correct 1 ms 2636 KB Output is correct
28 Correct 1 ms 2636 KB Output is correct
29 Correct 1 ms 2636 KB Output is correct
30 Correct 2 ms 2640 KB Output is correct
31 Correct 1 ms 2636 KB Output is correct
32 Correct 1 ms 2636 KB Output is correct
33 Correct 1 ms 2636 KB Output is correct
34 Correct 1 ms 2636 KB Output is correct
35 Correct 1 ms 2636 KB Output is correct
36 Correct 1 ms 2628 KB Output is correct
37 Correct 1 ms 2636 KB Output is correct
38 Correct 1 ms 2636 KB Output is correct
39 Correct 2 ms 2636 KB Output is correct
40 Correct 1 ms 2636 KB Output is correct
41 Correct 1 ms 2636 KB Output is correct
42 Correct 1 ms 2636 KB Output is correct
43 Correct 1 ms 2636 KB Output is correct
44 Correct 1 ms 2636 KB Output is correct
45 Correct 2 ms 2684 KB Output is correct
46 Correct 2 ms 2636 KB Output is correct
47 Correct 2 ms 2636 KB Output is correct
48 Correct 1 ms 2636 KB Output is correct
49 Correct 1 ms 2636 KB Output is correct
50 Correct 1 ms 2640 KB Output is correct
51 Correct 1 ms 2636 KB Output is correct
52 Correct 1 ms 2636 KB Output is correct
53 Correct 1 ms 2636 KB Output is correct
54 Correct 2 ms 2640 KB Output is correct
55 Correct 2 ms 2636 KB Output is correct
56 Correct 2 ms 2636 KB Output is correct
57 Correct 1 ms 2636 KB Output is correct
58 Correct 1 ms 2636 KB Output is correct
59 Correct 1 ms 2636 KB Output is correct
60 Correct 2 ms 2636 KB Output is correct
61 Correct 1 ms 2644 KB Output is correct
62 Correct 1 ms 2636 KB Output is correct
63 Correct 2 ms 2636 KB Output is correct
64 Correct 1 ms 2632 KB Output is correct
65 Correct 1 ms 2640 KB Output is correct
66 Correct 1 ms 2664 KB Output is correct
67 Correct 2 ms 2644 KB Output is correct
68 Correct 2 ms 2644 KB Output is correct
69 Correct 1 ms 2640 KB Output is correct
70 Correct 2 ms 2764 KB Output is correct
71 Correct 2 ms 2892 KB Output is correct
72 Correct 2 ms 2636 KB Output is correct
73 Correct 1 ms 2636 KB Output is correct
74 Correct 2 ms 2636 KB Output is correct
75 Correct 1 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 1 ms 2644 KB Output is correct
79 Correct 1 ms 2636 KB Output is correct
80 Correct 2 ms 2636 KB Output is correct
81 Correct 2 ms 2636 KB Output is correct
82 Correct 1 ms 2636 KB Output is correct
83 Correct 2 ms 2636 KB Output is correct
84 Correct 2 ms 2764 KB Output is correct
85 Correct 1 ms 2636 KB Output is correct
86 Correct 1 ms 2636 KB Output is correct
87 Correct 2 ms 2772 KB Output is correct
88 Correct 2 ms 2636 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 2636 KB Output is correct
92 Correct 3 ms 2764 KB Output is correct
93 Correct 1 ms 2636 KB Output is correct
94 Correct 1 ms 2636 KB Output is correct
95 Correct 2 ms 2636 KB Output is correct
96 Correct 3 ms 2892 KB Output is correct
97 Correct 3 ms 2640 KB Output is correct
98 Correct 2 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 10192 KB Output is correct
2 Correct 3 ms 3656 KB Output is correct
3 Correct 15 ms 3992 KB Output is correct
4 Correct 84 ms 7952 KB Output is correct
5 Correct 77 ms 10052 KB Output is correct
6 Correct 44 ms 5340 KB Output is correct
7 Correct 61 ms 9856 KB Output is correct
8 Correct 7 ms 5068 KB Output is correct
9 Correct 4 ms 4812 KB Output is correct
10 Correct 76 ms 11320 KB Output is correct
11 Correct 56 ms 9808 KB Output is correct
12 Correct 54 ms 9416 KB Output is correct
13 Correct 3 ms 3700 KB Output is correct
14 Correct 3 ms 3532 KB Output is correct
15 Correct 112 ms 8224 KB Output is correct
16 Correct 27 ms 6640 KB Output is correct
17 Correct 237 ms 10620 KB Output is correct
18 Correct 207 ms 8844 KB Output is correct
19 Correct 3 ms 2892 KB Output is correct
20 Correct 65 ms 10428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 1 ms 2636 KB Output is correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 1 ms 2636 KB Output is correct
8 Correct 1 ms 2636 KB Output is correct
9 Correct 1 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 1 ms 2640 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 1 ms 2636 KB Output is correct
14 Correct 1 ms 2636 KB Output is correct
15 Correct 1 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 1 ms 2692 KB Output is correct
18 Correct 1 ms 2640 KB Output is correct
19 Correct 1 ms 2636 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 1 ms 2636 KB Output is correct
22 Correct 1 ms 2612 KB Output is correct
23 Correct 1 ms 2636 KB Output is correct
24 Correct 2 ms 2640 KB Output is correct
25 Correct 1 ms 2636 KB Output is correct
26 Correct 1 ms 2636 KB Output is correct
27 Correct 1 ms 2636 KB Output is correct
28 Correct 1 ms 2636 KB Output is correct
29 Correct 1 ms 2636 KB Output is correct
30 Correct 2 ms 2640 KB Output is correct
31 Correct 1 ms 2636 KB Output is correct
32 Correct 1 ms 2636 KB Output is correct
33 Correct 1 ms 2636 KB Output is correct
34 Correct 1 ms 2636 KB Output is correct
35 Correct 1 ms 2636 KB Output is correct
36 Correct 1 ms 2628 KB Output is correct
37 Correct 1 ms 2636 KB Output is correct
38 Correct 1 ms 2636 KB Output is correct
39 Correct 2 ms 2636 KB Output is correct
40 Correct 1 ms 2636 KB Output is correct
41 Correct 1 ms 2636 KB Output is correct
42 Correct 1 ms 2636 KB Output is correct
43 Correct 1 ms 2636 KB Output is correct
44 Correct 1 ms 2636 KB Output is correct
45 Correct 2 ms 2684 KB Output is correct
46 Correct 2 ms 2636 KB Output is correct
47 Correct 2 ms 2636 KB Output is correct
48 Correct 1 ms 2636 KB Output is correct
49 Correct 1 ms 2636 KB Output is correct
50 Correct 1 ms 2640 KB Output is correct
51 Correct 1 ms 2636 KB Output is correct
52 Correct 1 ms 2636 KB Output is correct
53 Correct 1 ms 2636 KB Output is correct
54 Correct 2 ms 2640 KB Output is correct
55 Correct 2 ms 2636 KB Output is correct
56 Correct 2 ms 2636 KB Output is correct
57 Correct 1 ms 2636 KB Output is correct
58 Correct 1 ms 2636 KB Output is correct
59 Correct 1 ms 2636 KB Output is correct
60 Correct 2 ms 2636 KB Output is correct
61 Correct 1 ms 2644 KB Output is correct
62 Correct 1 ms 2636 KB Output is correct
63 Correct 2 ms 2636 KB Output is correct
64 Correct 1 ms 2632 KB Output is correct
65 Correct 1 ms 2640 KB Output is correct
66 Correct 1 ms 2664 KB Output is correct
67 Correct 2 ms 2644 KB Output is correct
68 Correct 2 ms 2644 KB Output is correct
69 Correct 1 ms 2640 KB Output is correct
70 Correct 2 ms 2764 KB Output is correct
71 Correct 2 ms 2892 KB Output is correct
72 Correct 2 ms 2636 KB Output is correct
73 Correct 1 ms 2636 KB Output is correct
74 Correct 2 ms 2636 KB Output is correct
75 Correct 1 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 1 ms 2644 KB Output is correct
79 Correct 1 ms 2636 KB Output is correct
80 Correct 2 ms 2636 KB Output is correct
81 Correct 2 ms 2636 KB Output is correct
82 Correct 1 ms 2636 KB Output is correct
83 Correct 2 ms 2636 KB Output is correct
84 Correct 2 ms 2764 KB Output is correct
85 Correct 1 ms 2636 KB Output is correct
86 Correct 1 ms 2636 KB Output is correct
87 Correct 2 ms 2772 KB Output is correct
88 Correct 2 ms 2636 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 2636 KB Output is correct
92 Correct 3 ms 2764 KB Output is correct
93 Correct 1 ms 2636 KB Output is correct
94 Correct 1 ms 2636 KB Output is correct
95 Correct 2 ms 2636 KB Output is correct
96 Correct 3 ms 2892 KB Output is correct
97 Correct 3 ms 2640 KB Output is correct
98 Correct 2 ms 2636 KB Output is correct
99 Correct 90 ms 10192 KB Output is correct
100 Correct 3 ms 3656 KB Output is correct
101 Correct 15 ms 3992 KB Output is correct
102 Correct 84 ms 7952 KB Output is correct
103 Correct 77 ms 10052 KB Output is correct
104 Correct 44 ms 5340 KB Output is correct
105 Correct 61 ms 9856 KB Output is correct
106 Correct 7 ms 5068 KB Output is correct
107 Correct 4 ms 4812 KB Output is correct
108 Correct 76 ms 11320 KB Output is correct
109 Correct 56 ms 9808 KB Output is correct
110 Correct 54 ms 9416 KB Output is correct
111 Correct 3 ms 3700 KB Output is correct
112 Correct 3 ms 3532 KB Output is correct
113 Correct 112 ms 8224 KB Output is correct
114 Correct 27 ms 6640 KB Output is correct
115 Correct 237 ms 10620 KB Output is correct
116 Correct 207 ms 8844 KB Output is correct
117 Correct 3 ms 2892 KB Output is correct
118 Correct 65 ms 10428 KB Output is correct
119 Correct 4 ms 4428 KB Output is correct
120 Correct 60 ms 12208 KB Output is correct
121 Correct 43 ms 8248 KB Output is correct
122 Correct 2 ms 2636 KB Output is correct
123 Correct 4 ms 3020 KB Output is correct
124 Correct 17 ms 4436 KB Output is correct
125 Correct 29 ms 6460 KB Output is correct
126 Correct 4 ms 4816 KB Output is correct
127 Correct 13 ms 5192 KB Output is correct
128 Correct 65 ms 10012 KB Output is correct
129 Correct 36 ms 8104 KB Output is correct
130 Correct 47 ms 8612 KB Output is correct
131 Correct 4 ms 4376 KB Output is correct
132 Correct 4 ms 4684 KB Output is correct
133 Correct 286 ms 10764 KB Output is correct
134 Correct 76 ms 5944 KB Output is correct
135 Correct 2 ms 3276 KB Output is correct
136 Correct 24 ms 4816 KB Output is correct
137 Correct 40 ms 6228 KB Output is correct
138 Correct 5 ms 4300 KB Output is correct