답안 #644597

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
644597 2022-09-25T03:43:46 Z Cyanmond Alternating Current (BOI18_alternating) C++17
100 / 100
241 ms 27316 KB
#include <bits/stdc++.h>

int main() {
    // input
    int N, M;
    std::cin >> N >> M;
    
    std::vector<int> A(M), B(M);
    for (int i = 0; i < M; ++i) {
        std::cin >> A[i] >> B[i];
        --A[i];
        if (A[i] >= B[i]) {
            B[i] += N;
        }
    }
    
    // calc root
    std::vector<int> root(M, -1);

    std::vector<std::vector<int>> in(2 * N);
    for (int i = 0; i < M; ++i) {
        in[A[i]].push_back(i);
        in[A[i] + N].push_back(i);
    }

    std::pair<int, int> max_right = {-1, 0};
    for (int i = 0; i < 2 * N; ++i) {
        // reverse-sort
        std::sort(in[i].begin(), in[i].end(), [&](const int a, const int b) {
            int r1 = B[a], r2 = B[b];
            while (r1 <= i) {
                r1 += N;
            }
            while (r2 <= i) {
                r2 += N;
            }
            return r1 > r2;
        });

        for (const int e : in[i]) {
            int l = i, r = B[e];
            while (r <= l) {
                r += N;
            }

            if (r < max_right.first) {
                root[e] = max_right.second;
            } else {
                max_right.first = r;
                max_right.second = e;
            }
        }
    }

    for (int i = 0; i < M; ++i) {
        auto rec = [&](auto &&self, const int v) {
            if (root[v] == -1) {
                return v;
            } else {
                return root[v] = self(self, root[v]);
            }
        };
        rec(rec, i);
    }

    std::vector<int> roots;
    for (int i = 0; i < M; ++i) {
        if (root[i] == -1) {
            roots.push_back(root[i] = i);
        }
    }
    std::sort(roots.begin(), roots.end(), [&](const int a, const int b) {
        return A[a] < A[b];
    });
    
    const int K = (int)roots.size();
    std::vector<int> reorder(M, -1);
    for (int i = 0; i < K; ++i) {
        reorder[roots[i]] = i;
    }

    // calc limit
    auto calc_time = [&](const std::vector<int> roots,
                            const std::vector<std::vector<int>> od) {
        std::vector<int> time_x(N, K), time_y(N, K);
        std::set<int> bad_x, bad_y;
        for (int i = 0; i < N; ++i) {
            bad_x.insert(i);
            bad_y.insert(i);
        }

        auto fill = [&](std::set<int> &s, std::vector<int> &tiv, const int i, const int t) {
            auto itr = s.lower_bound(A[i]);
            while (itr != s.end() and *itr < B[i]) {
                tiv[*itr] = t;
                itr = s.erase(itr);
            }

            itr = s.begin();
            while (itr != s.end() and *itr < B[i] - N) {
                tiv[*itr] = t;
                itr = s.erase(itr);
            }            
        };


        for (int i = 0; i < K; ++i) {
            const bool is_x = i % 2 == 0;
            fill(is_x ? bad_x : bad_y, is_x ? time_x : time_y, roots[i], i);
            for (const int e : od[i]) {
                fill(is_x ? bad_y : bad_x, is_x ? time_y : time_x, e, i);
            }
        }

        return std::make_pair(time_x, time_y);
    };
    
    std::vector<std::vector<int>> od(K);
    for (int i = 0; i < M; ++i) {
        if (root[i] == i) {
            continue;
        }
        od[reorder[root[i]]].push_back(i);
    }

    const auto [time_x1, time_y1] = calc_time(roots, od);
    std::reverse(roots.begin(), roots.end());
    std::reverse(od.begin(), od.end());
    auto [time_y2, time_x2] = calc_time(roots, od);
    for (auto &e : time_x2) {
        e = K - e;
    }
    for (auto &e : time_y2) {
        e = K - e;
    }

    // skip
    int ma = -1;
    for (const int e : time_x1) {
        ma = std::max(ma, e);
    }
    for (const int e : time_y1) {
        ma = std::max(ma, e);
    }
    if (ma != K) {
        for (int i = 0; i < M; ++i) {
            bool answer = root[i] == i;
            answer ^= reorder[root[i]] % 2 == 1;
            std::cout << answer;
        }
        std::cout << std::endl;
        return 0;
    }

    // solve
    std::set<int> ok_pos;
    for (int i = 1; i < K; ++i) {
        ok_pos.insert(i);
    }

    for (int i = 0; i < N; ++i) {
        auto erase = [&](const std::vector<int> &t1, const std::vector<int> &t2) {
            const int l = t2[i], r = t1[i] + 1;
            auto itr = ok_pos.lower_bound(l);
            while (itr != ok_pos.end() and *itr < r) {
                itr = ok_pos.erase(itr);
            }
        };

        erase(time_x1, time_x2);
        erase(time_y1, time_y2);
    }

    // output
    if (ok_pos.empty()) {
        std::cout << "impossible" << std::endl;
    } else {
        const int p = *ok_pos.begin();
        for (int i = 0; i < M; ++i) {
            bool answer = root[i] == i;
            answer ^= reorder[root[i]] >= p;
            answer ^= reorder[root[i]] % 2 == 1;
            std::cout << answer;
        }
        std::cout << std::endl;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 260 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 284 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 1 ms 300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 260 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 284 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 1 ms 300 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 300 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 300 KB Output is correct
49 Correct 1 ms 308 KB Output is correct
50 Correct 0 ms 300 KB Output is correct
51 Correct 1 ms 212 KB Output is correct
52 Correct 1 ms 212 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 1 ms 304 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 0 ms 212 KB Output is correct
57 Correct 0 ms 212 KB Output is correct
58 Correct 1 ms 304 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 0 ms 212 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 0 ms 212 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 1 ms 212 KB Output is correct
67 Correct 0 ms 300 KB Output is correct
68 Correct 1 ms 212 KB Output is correct
69 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 260 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 284 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 1 ms 300 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 300 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 300 KB Output is correct
49 Correct 1 ms 308 KB Output is correct
50 Correct 0 ms 300 KB Output is correct
51 Correct 1 ms 212 KB Output is correct
52 Correct 1 ms 212 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 1 ms 304 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 0 ms 212 KB Output is correct
57 Correct 0 ms 212 KB Output is correct
58 Correct 1 ms 304 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 0 ms 212 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 0 ms 212 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 1 ms 212 KB Output is correct
67 Correct 0 ms 300 KB Output is correct
68 Correct 1 ms 212 KB Output is correct
69 Correct 0 ms 212 KB Output is correct
70 Correct 1 ms 468 KB Output is correct
71 Correct 2 ms 468 KB Output is correct
72 Correct 1 ms 348 KB Output is correct
73 Correct 1 ms 476 KB Output is correct
74 Correct 1 ms 476 KB Output is correct
75 Correct 1 ms 300 KB Output is correct
76 Correct 1 ms 468 KB Output is correct
77 Correct 1 ms 476 KB Output is correct
78 Correct 1 ms 436 KB Output is correct
79 Correct 1 ms 348 KB Output is correct
80 Correct 2 ms 476 KB Output is correct
81 Correct 1 ms 476 KB Output is correct
82 Correct 1 ms 468 KB Output is correct
83 Correct 2 ms 468 KB Output is correct
84 Correct 2 ms 572 KB Output is correct
85 Correct 1 ms 356 KB Output is correct
86 Correct 1 ms 340 KB Output is correct
87 Correct 2 ms 432 KB Output is correct
88 Correct 1 ms 428 KB Output is correct
89 Correct 2 ms 468 KB Output is correct
90 Correct 2 ms 432 KB Output is correct
91 Correct 1 ms 468 KB Output is correct
92 Correct 1 ms 340 KB Output is correct
93 Correct 1 ms 420 KB Output is correct
94 Correct 1 ms 340 KB Output is correct
95 Correct 1 ms 468 KB Output is correct
96 Correct 2 ms 468 KB Output is correct
97 Correct 2 ms 468 KB Output is correct
98 Correct 2 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 155 ms 23692 KB Output is correct
2 Correct 93 ms 15936 KB Output is correct
3 Correct 126 ms 20240 KB Output is correct
4 Correct 131 ms 20340 KB Output is correct
5 Correct 199 ms 26836 KB Output is correct
6 Correct 209 ms 25632 KB Output is correct
7 Correct 211 ms 24516 KB Output is correct
8 Correct 99 ms 16228 KB Output is correct
9 Correct 91 ms 15236 KB Output is correct
10 Correct 209 ms 27316 KB Output is correct
11 Correct 187 ms 23840 KB Output is correct
12 Correct 241 ms 23660 KB Output is correct
13 Correct 96 ms 15920 KB Output is correct
14 Correct 99 ms 15916 KB Output is correct
15 Correct 231 ms 21920 KB Output is correct
16 Correct 150 ms 22500 KB Output is correct
17 Correct 183 ms 21896 KB Output is correct
18 Correct 70 ms 4964 KB Output is correct
19 Correct 104 ms 16500 KB Output is correct
20 Correct 129 ms 19524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 260 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 284 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 1 ms 300 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 300 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 300 KB Output is correct
49 Correct 1 ms 308 KB Output is correct
50 Correct 0 ms 300 KB Output is correct
51 Correct 1 ms 212 KB Output is correct
52 Correct 1 ms 212 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 1 ms 304 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 0 ms 212 KB Output is correct
57 Correct 0 ms 212 KB Output is correct
58 Correct 1 ms 304 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 0 ms 212 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 0 ms 212 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 1 ms 212 KB Output is correct
67 Correct 0 ms 300 KB Output is correct
68 Correct 1 ms 212 KB Output is correct
69 Correct 0 ms 212 KB Output is correct
70 Correct 1 ms 468 KB Output is correct
71 Correct 2 ms 468 KB Output is correct
72 Correct 1 ms 348 KB Output is correct
73 Correct 1 ms 476 KB Output is correct
74 Correct 1 ms 476 KB Output is correct
75 Correct 1 ms 300 KB Output is correct
76 Correct 1 ms 468 KB Output is correct
77 Correct 1 ms 476 KB Output is correct
78 Correct 1 ms 436 KB Output is correct
79 Correct 1 ms 348 KB Output is correct
80 Correct 2 ms 476 KB Output is correct
81 Correct 1 ms 476 KB Output is correct
82 Correct 1 ms 468 KB Output is correct
83 Correct 2 ms 468 KB Output is correct
84 Correct 2 ms 572 KB Output is correct
85 Correct 1 ms 356 KB Output is correct
86 Correct 1 ms 340 KB Output is correct
87 Correct 2 ms 432 KB Output is correct
88 Correct 1 ms 428 KB Output is correct
89 Correct 2 ms 468 KB Output is correct
90 Correct 2 ms 432 KB Output is correct
91 Correct 1 ms 468 KB Output is correct
92 Correct 1 ms 340 KB Output is correct
93 Correct 1 ms 420 KB Output is correct
94 Correct 1 ms 340 KB Output is correct
95 Correct 1 ms 468 KB Output is correct
96 Correct 2 ms 468 KB Output is correct
97 Correct 2 ms 468 KB Output is correct
98 Correct 2 ms 468 KB Output is correct
99 Correct 155 ms 23692 KB Output is correct
100 Correct 93 ms 15936 KB Output is correct
101 Correct 126 ms 20240 KB Output is correct
102 Correct 131 ms 20340 KB Output is correct
103 Correct 199 ms 26836 KB Output is correct
104 Correct 209 ms 25632 KB Output is correct
105 Correct 211 ms 24516 KB Output is correct
106 Correct 99 ms 16228 KB Output is correct
107 Correct 91 ms 15236 KB Output is correct
108 Correct 209 ms 27316 KB Output is correct
109 Correct 187 ms 23840 KB Output is correct
110 Correct 241 ms 23660 KB Output is correct
111 Correct 96 ms 15920 KB Output is correct
112 Correct 99 ms 15916 KB Output is correct
113 Correct 231 ms 21920 KB Output is correct
114 Correct 150 ms 22500 KB Output is correct
115 Correct 183 ms 21896 KB Output is correct
116 Correct 70 ms 4964 KB Output is correct
117 Correct 104 ms 16500 KB Output is correct
118 Correct 129 ms 19524 KB Output is correct
119 Correct 97 ms 15840 KB Output is correct
120 Correct 188 ms 26376 KB Output is correct
121 Correct 142 ms 22380 KB Output is correct
122 Correct 0 ms 308 KB Output is correct
123 Correct 105 ms 16612 KB Output is correct
124 Correct 140 ms 20572 KB Output is correct
125 Correct 125 ms 19532 KB Output is correct
126 Correct 100 ms 15948 KB Output is correct
127 Correct 112 ms 16916 KB Output is correct
128 Correct 198 ms 25868 KB Output is correct
129 Correct 148 ms 21028 KB Output is correct
130 Correct 161 ms 22936 KB Output is correct
131 Correct 104 ms 15928 KB Output is correct
132 Correct 98 ms 15912 KB Output is correct
133 Correct 192 ms 23732 KB Output is correct
134 Correct 122 ms 18500 KB Output is correct
135 Correct 98 ms 15948 KB Output is correct
136 Correct 160 ms 23452 KB Output is correct
137 Correct 184 ms 23636 KB Output is correct
138 Correct 96 ms 16056 KB Output is correct