Submission #451182

# Submission time Handle Problem Language Result Execution time Memory
451182 2021-08-03T06:29:20 Z mjhmjh1104 Joker (BOI20_joker) C++17
100 / 100
391 ms 11908 KB
#include <tuple>
#include <cstdio>
#include <vector>
#include <utility>
using namespace std;

int uf[200006], sz[200006], ds[200006], y[200006];

int _find(int x) {
    if (uf[x] == -1) return x;
    return _find(uf[x]);
}

int _dist(int x) {
    if (uf[x] == -1) return 0;
    return ds[x] + _dist(uf[x]);
}

vector<tuple<int, int, int>> st;

bool _merge(int x, int y) {
    int a = _dist(x), b = _dist(y);
    x = _find(x), y = _find(y);
    if (x != y) {
        if (sz[x] < sz[y]) swap(x, y);
        st.push_back({ 0, y, uf[y] });
        uf[y] = x;
        if (sz[x] == sz[y]) {
            st.push_back({ 1, x, sz[x] });
            sz[x]++;
        }
        if ((a + b) % 2 == 0) {
            st.push_back({ 2, y, ds[y] });
            ds[y]++;
        }
        return true;
    }
    return false;
}

void _rollback() {
    auto [ a, b, c ] = st.back(); st.pop_back();
    if (a == 0) uf[b] = c;
    if (a == 1) sz[b] = c;
    if (a == 2) ds[b] = c;
}

int n, M, q;
vector<pair<int, int>> edges;

void dnc(int b, int e, int l, int r) {
    if (b > e) return;
    int x = -1;
    int m = (b + e) / 2;
    int s2 = (int)st.size();
    for (int i = b; i < m; i++) if (!_merge(edges[i].first, edges[i].second) && (_dist(edges[i].first) + _dist(edges[i].second) + 1) % 2) {
        x = r;
        break;
    }
    int s = (int)st.size();
    if (x == -1) for (int i = min(r, M - 1); i > l; i--) if (!_merge(edges[i].first, edges[i].second) && (_dist(edges[i].first) + _dist(edges[i].second) + 1) % 2) {
        x = i;
        break;
    }
    if (x == -1) x = l;
    while ((int)st.size() > s) _rollback();
    y[m] = x;
    if (!_merge(edges[m].first, edges[m].second) && (_dist(edges[m].first) + _dist(edges[m].second) + 1) % 2) dnc(m + 1, e, r, r);
    else dnc(m + 1, e, x, r);
    while ((int)st.size() > s2) _rollback();
    for (int i = min(r, M - 1); i > x; i--) _merge(edges[i].first, edges[i].second);
    dnc(b, m - 1, l, x);
}

int main() {
    scanf("%d%d%d", &n, &M, &q);
    for (int i = 0; i < M; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        u--, v--;
        edges.push_back({ u, v });
    }
    for (int i = 0; i < n; i++) uf[i] = -1, sz[i] = 0, ds[i] = 0;
    dnc(0, M - 1, 0, M);
    for (int i = 0; i < q; i++) {
        int l, r;
        scanf("%d%d", &l, &r);
        l--, r--;
        if (r < y[l]) puts("YES");
        else puts("NO");
    }
}

Compilation message

Joker.cpp: In function 'int main()':
Joker.cpp:76:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |     scanf("%d%d%d", &n, &M, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:79:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |         scanf("%d%d", &u, &v);
      |         ~~~~~^~~~~~~~~~~~~~~~
Joker.cpp:87:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |         scanf("%d%d", &l, &r);
      |         ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 0 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 0 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 2 ms 332 KB Output is correct
31 Correct 3 ms 332 KB Output is correct
32 Correct 2 ms 332 KB Output is correct
33 Correct 2 ms 332 KB Output is correct
34 Correct 2 ms 332 KB Output is correct
35 Correct 3 ms 332 KB Output is correct
36 Correct 2 ms 332 KB Output is correct
37 Correct 3 ms 448 KB Output is correct
38 Correct 3 ms 332 KB Output is correct
39 Correct 3 ms 332 KB Output is correct
40 Correct 2 ms 332 KB Output is correct
41 Correct 2 ms 332 KB Output is correct
42 Correct 3 ms 332 KB Output is correct
43 Correct 2 ms 332 KB Output is correct
44 Correct 2 ms 332 KB Output is correct
45 Correct 2 ms 332 KB Output is correct
46 Correct 3 ms 332 KB Output is correct
47 Correct 2 ms 332 KB Output is correct
48 Correct 2 ms 332 KB Output is correct
49 Correct 3 ms 332 KB Output is correct
50 Correct 3 ms 332 KB Output is correct
51 Correct 2 ms 332 KB Output is correct
52 Correct 2 ms 332 KB Output is correct
53 Correct 2 ms 332 KB Output is correct
54 Correct 3 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 197 ms 7724 KB Output is correct
4 Correct 387 ms 11876 KB Output is correct
5 Correct 201 ms 11700 KB Output is correct
6 Correct 190 ms 8072 KB Output is correct
7 Correct 196 ms 8244 KB Output is correct
8 Correct 213 ms 6940 KB Output is correct
9 Correct 242 ms 8104 KB Output is correct
10 Correct 326 ms 10144 KB Output is correct
11 Correct 263 ms 7844 KB Output is correct
12 Correct 296 ms 9540 KB Output is correct
13 Correct 218 ms 4656 KB Output is correct
14 Correct 220 ms 6876 KB Output is correct
15 Correct 332 ms 8832 KB Output is correct
16 Correct 339 ms 9968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 0 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 197 ms 7724 KB Output is correct
30 Correct 387 ms 11876 KB Output is correct
31 Correct 201 ms 11700 KB Output is correct
32 Correct 190 ms 8072 KB Output is correct
33 Correct 196 ms 8244 KB Output is correct
34 Correct 213 ms 6940 KB Output is correct
35 Correct 242 ms 8104 KB Output is correct
36 Correct 326 ms 10144 KB Output is correct
37 Correct 263 ms 7844 KB Output is correct
38 Correct 296 ms 9540 KB Output is correct
39 Correct 218 ms 4656 KB Output is correct
40 Correct 220 ms 6876 KB Output is correct
41 Correct 332 ms 8832 KB Output is correct
42 Correct 339 ms 9968 KB Output is correct
43 Correct 208 ms 7800 KB Output is correct
44 Correct 367 ms 11800 KB Output is correct
45 Correct 341 ms 11908 KB Output is correct
46 Correct 196 ms 8228 KB Output is correct
47 Correct 190 ms 8232 KB Output is correct
48 Correct 303 ms 8036 KB Output is correct
49 Correct 362 ms 10072 KB Output is correct
50 Correct 256 ms 8204 KB Output is correct
51 Correct 303 ms 8744 KB Output is correct
52 Correct 307 ms 9776 KB Output is correct
53 Correct 220 ms 4660 KB Output is correct
54 Correct 287 ms 7396 KB Output is correct
55 Correct 326 ms 8720 KB Output is correct
56 Correct 329 ms 9900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 0 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 2 ms 332 KB Output is correct
31 Correct 3 ms 332 KB Output is correct
32 Correct 2 ms 332 KB Output is correct
33 Correct 2 ms 332 KB Output is correct
34 Correct 2 ms 332 KB Output is correct
35 Correct 3 ms 332 KB Output is correct
36 Correct 2 ms 332 KB Output is correct
37 Correct 3 ms 448 KB Output is correct
38 Correct 3 ms 332 KB Output is correct
39 Correct 3 ms 332 KB Output is correct
40 Correct 2 ms 332 KB Output is correct
41 Correct 2 ms 332 KB Output is correct
42 Correct 3 ms 332 KB Output is correct
43 Correct 2 ms 332 KB Output is correct
44 Correct 2 ms 332 KB Output is correct
45 Correct 2 ms 332 KB Output is correct
46 Correct 3 ms 332 KB Output is correct
47 Correct 2 ms 332 KB Output is correct
48 Correct 2 ms 332 KB Output is correct
49 Correct 3 ms 332 KB Output is correct
50 Correct 3 ms 332 KB Output is correct
51 Correct 2 ms 332 KB Output is correct
52 Correct 2 ms 332 KB Output is correct
53 Correct 2 ms 332 KB Output is correct
54 Correct 3 ms 332 KB Output is correct
55 Correct 155 ms 7540 KB Output is correct
56 Correct 287 ms 11892 KB Output is correct
57 Correct 175 ms 11648 KB Output is correct
58 Correct 143 ms 7608 KB Output is correct
59 Correct 224 ms 7560 KB Output is correct
60 Correct 284 ms 8540 KB Output is correct
61 Correct 221 ms 7604 KB Output is correct
62 Correct 239 ms 8748 KB Output is correct
63 Correct 170 ms 5528 KB Output is correct
64 Correct 231 ms 7800 KB Output is correct
65 Correct 274 ms 8840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 0 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 2 ms 332 KB Output is correct
31 Correct 3 ms 332 KB Output is correct
32 Correct 2 ms 332 KB Output is correct
33 Correct 2 ms 332 KB Output is correct
34 Correct 2 ms 332 KB Output is correct
35 Correct 3 ms 332 KB Output is correct
36 Correct 2 ms 332 KB Output is correct
37 Correct 3 ms 448 KB Output is correct
38 Correct 3 ms 332 KB Output is correct
39 Correct 3 ms 332 KB Output is correct
40 Correct 2 ms 332 KB Output is correct
41 Correct 2 ms 332 KB Output is correct
42 Correct 3 ms 332 KB Output is correct
43 Correct 2 ms 332 KB Output is correct
44 Correct 2 ms 332 KB Output is correct
45 Correct 2 ms 332 KB Output is correct
46 Correct 3 ms 332 KB Output is correct
47 Correct 2 ms 332 KB Output is correct
48 Correct 2 ms 332 KB Output is correct
49 Correct 3 ms 332 KB Output is correct
50 Correct 3 ms 332 KB Output is correct
51 Correct 2 ms 332 KB Output is correct
52 Correct 2 ms 332 KB Output is correct
53 Correct 2 ms 332 KB Output is correct
54 Correct 3 ms 332 KB Output is correct
55 Correct 197 ms 7724 KB Output is correct
56 Correct 387 ms 11876 KB Output is correct
57 Correct 201 ms 11700 KB Output is correct
58 Correct 190 ms 8072 KB Output is correct
59 Correct 196 ms 8244 KB Output is correct
60 Correct 213 ms 6940 KB Output is correct
61 Correct 242 ms 8104 KB Output is correct
62 Correct 326 ms 10144 KB Output is correct
63 Correct 263 ms 7844 KB Output is correct
64 Correct 296 ms 9540 KB Output is correct
65 Correct 218 ms 4656 KB Output is correct
66 Correct 220 ms 6876 KB Output is correct
67 Correct 332 ms 8832 KB Output is correct
68 Correct 339 ms 9968 KB Output is correct
69 Correct 208 ms 7800 KB Output is correct
70 Correct 367 ms 11800 KB Output is correct
71 Correct 341 ms 11908 KB Output is correct
72 Correct 196 ms 8228 KB Output is correct
73 Correct 190 ms 8232 KB Output is correct
74 Correct 303 ms 8036 KB Output is correct
75 Correct 362 ms 10072 KB Output is correct
76 Correct 256 ms 8204 KB Output is correct
77 Correct 303 ms 8744 KB Output is correct
78 Correct 307 ms 9776 KB Output is correct
79 Correct 220 ms 4660 KB Output is correct
80 Correct 287 ms 7396 KB Output is correct
81 Correct 326 ms 8720 KB Output is correct
82 Correct 329 ms 9900 KB Output is correct
83 Correct 155 ms 7540 KB Output is correct
84 Correct 287 ms 11892 KB Output is correct
85 Correct 175 ms 11648 KB Output is correct
86 Correct 143 ms 7608 KB Output is correct
87 Correct 224 ms 7560 KB Output is correct
88 Correct 284 ms 8540 KB Output is correct
89 Correct 221 ms 7604 KB Output is correct
90 Correct 239 ms 8748 KB Output is correct
91 Correct 170 ms 5528 KB Output is correct
92 Correct 231 ms 7800 KB Output is correct
93 Correct 274 ms 8840 KB Output is correct
94 Correct 213 ms 7824 KB Output is correct
95 Correct 308 ms 11828 KB Output is correct
96 Correct 214 ms 11680 KB Output is correct
97 Correct 209 ms 8288 KB Output is correct
98 Correct 216 ms 8300 KB Output is correct
99 Correct 255 ms 6928 KB Output is correct
100 Correct 339 ms 10064 KB Output is correct
101 Correct 250 ms 8128 KB Output is correct
102 Correct 328 ms 8792 KB Output is correct
103 Correct 355 ms 9872 KB Output is correct
104 Correct 236 ms 6192 KB Output is correct
105 Correct 326 ms 8496 KB Output is correct
106 Correct 391 ms 9560 KB Output is correct
107 Correct 189 ms 11820 KB Output is correct
108 Correct 211 ms 7664 KB Output is correct
109 Correct 208 ms 7716 KB Output is correct
110 Correct 206 ms 7648 KB Output is correct
111 Correct 208 ms 7728 KB Output is correct
112 Correct 224 ms 7756 KB Output is correct
113 Correct 205 ms 7672 KB Output is correct
114 Correct 219 ms 7700 KB Output is correct
115 Correct 208 ms 7728 KB Output is correct
116 Correct 209 ms 7640 KB Output is correct