답안 #877725

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
877725 2023-11-23T13:22:29 Z CDuong Joker (BOI20_joker) C++17
100 / 100
172 ms 16948 KB
/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/

#include <bits/stdc++.h>
#define taskname "NOHOME"
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define i64 long long
#define pb push_back
#define ff first
#define ss second
#define isz(x) (int)x.size()
using namespace std;

const int mxN = 2e5 + 5;
const int mod = 1e9 + 7;
const i64 oo = 1e18;

struct DisjointSetUnion01Rollback {
    vector<int> lab, val;
    vector<pair<int, int>> changes;

    DisjointSetUnion01Rollback() {}
    DisjointSetUnion01Rollback(int n) {
        init(n);
    }

    void init(int n) {
        lab.assign(n + 1, -1);
        val.assign(n + 1, 0);
        changes.clear();
    }

    pair<int, int> root(int v) {
        if (lab[v] < 0) return {v, val[v]};
        auto cpar = root(lab[v]);
        return {cpar.ff, val[v] ^ cpar.ss};
    }

    bool merge(int u, int v, int edge_val) {
        auto rootu = root(u);
        auto rootv = root(v);
        if (rootu.ff != rootv.ff) {
            if (-lab[rootu.ff] < -lab[rootv.ff])
                swap(u, v), swap(rootu, rootv);
            changes.emplace_back(rootu.ff, lab[rootu.ff]);
            lab[rootu.ff] += lab[rootv.ff];
            changes.emplace_back(rootv.ff, lab[rootv.ff]);
            lab[rootv.ff] = rootu.ff;
            changes.emplace_back(-rootv.ff, val[rootv.ff]);
            val[rootv.ff] = val[rootu.ff] ^ rootu.ss ^ edge_val ^ rootv.ss;
            return true;
        }
        return (rootu.ss ^ rootv.ss ^ edge_val ^ 1);
    }

    int get_timer() {
        return isz(changes);
    }

    void reverse_to(int timer) {
        while (isz(changes) > timer) {
            int idx = changes.back().ff;
            int oval = changes.back().ss;
            changes.pop_back();
            if (idx > 0) lab[idx] = oval;
            else val[-idx] = oval;
        }
    }
} dsu;

int n, m, q;
vector<pair<int, int>> edges;
int cntA, cntB, best[mxN];

void solveDP(int l, int r, int optl, int optr) {
    if (l > r) return;

    int otimer = dsu.get_timer();

    int mid = (l + r) >> 1;
    for (int i = l; i <= mid; ++i)
        if (i) assert(dsu.merge(edges[i].ff, edges[i].ss, 1));

    int timer = dsu.get_timer();

    int opt = optr;
    while (opt >= optl && dsu.merge(edges[opt].ff, edges[opt].ss, 1)) --opt;
    best[mid] = opt;

    dsu.reverse_to(timer);
    solveDP(mid + 1, r, opt, optr);

    dsu.reverse_to(otimer);
    for (int i = optr; i > opt; --i)
        dsu.merge(edges[i].ff, edges[i].ss, 1);
    solveDP(l, mid - 1, optl, opt);

    dsu.reverse_to(otimer);
}

void solve() {
    cin >> n >> m >> q;
    edges.emplace_back(0, 0);
    for (int i = 1; i <= m; ++i) {
        int u, v; cin >> u >> v;
        edges.emplace_back(u, v);
    }

    dsu.init(n);
    int ptr = 1;
    while (ptr <= m && dsu.merge(edges[ptr].ff, edges[ptr].ss, 1)) ++ptr;
    for (int i = ptr; i <= m; ++i) best[i] = m + 1;

    dsu.init(n);
    solveDP(0, ptr - 1, 1, m);

    for (int i = 1; i <= q; ++i) {
        int l, r; cin >> l >> r;
        cout << (best[l - 1] > r ? "YES" : "NO") << "\n";
    }

}

signed main() {

#ifndef CDuongg
    if(fopen(taskname".inp", "r"))
        assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
    freopen("bai3.inp", "r", stdin);
    freopen("bai3.out", "w", stdout);
    auto start = chrono::high_resolution_clock::now();
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1; //cin >> t;
    while(t--) solve();

#ifdef CDuongg
   auto end = chrono::high_resolution_clock::now();
   // cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
   // cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif

}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 444 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 500 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 444 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 500 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 1 ms 480 KB Output is correct
31 Correct 2 ms 600 KB Output is correct
32 Correct 1 ms 476 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 2 ms 708 KB Output is correct
37 Correct 2 ms 604 KB Output is correct
38 Correct 2 ms 604 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 600 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 1 ms 604 KB Output is correct
47 Correct 1 ms 604 KB Output is correct
48 Correct 1 ms 476 KB Output is correct
49 Correct 1 ms 600 KB Output is correct
50 Correct 1 ms 604 KB Output is correct
51 Correct 2 ms 732 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Correct 1 ms 604 KB Output is correct
54 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 107 ms 11904 KB Output is correct
4 Correct 164 ms 16456 KB Output is correct
5 Correct 105 ms 14656 KB Output is correct
6 Correct 89 ms 12192 KB Output is correct
7 Correct 93 ms 11584 KB Output is correct
8 Correct 98 ms 8776 KB Output is correct
9 Correct 115 ms 10056 KB Output is correct
10 Correct 157 ms 11836 KB Output is correct
11 Correct 113 ms 10148 KB Output is correct
12 Correct 131 ms 12800 KB Output is correct
13 Correct 107 ms 8232 KB Output is correct
14 Correct 102 ms 9284 KB Output is correct
15 Correct 143 ms 11328 KB Output is correct
16 Correct 155 ms 12728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 444 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 500 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 107 ms 11904 KB Output is correct
30 Correct 164 ms 16456 KB Output is correct
31 Correct 105 ms 14656 KB Output is correct
32 Correct 89 ms 12192 KB Output is correct
33 Correct 93 ms 11584 KB Output is correct
34 Correct 98 ms 8776 KB Output is correct
35 Correct 115 ms 10056 KB Output is correct
36 Correct 157 ms 11836 KB Output is correct
37 Correct 113 ms 10148 KB Output is correct
38 Correct 131 ms 12800 KB Output is correct
39 Correct 107 ms 8232 KB Output is correct
40 Correct 102 ms 9284 KB Output is correct
41 Correct 143 ms 11328 KB Output is correct
42 Correct 155 ms 12728 KB Output is correct
43 Correct 107 ms 10932 KB Output is correct
44 Correct 172 ms 15636 KB Output is correct
45 Correct 172 ms 15744 KB Output is correct
46 Correct 89 ms 11664 KB Output is correct
47 Correct 90 ms 12292 KB Output is correct
48 Correct 128 ms 10280 KB Output is correct
49 Correct 154 ms 12040 KB Output is correct
50 Correct 113 ms 10092 KB Output is correct
51 Correct 117 ms 11984 KB Output is correct
52 Correct 123 ms 13376 KB Output is correct
53 Correct 113 ms 8640 KB Output is correct
54 Correct 134 ms 9960 KB Output is correct
55 Correct 136 ms 12604 KB Output is correct
56 Correct 155 ms 12280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 444 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 500 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 1 ms 480 KB Output is correct
31 Correct 2 ms 600 KB Output is correct
32 Correct 1 ms 476 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 2 ms 708 KB Output is correct
37 Correct 2 ms 604 KB Output is correct
38 Correct 2 ms 604 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 600 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 1 ms 604 KB Output is correct
47 Correct 1 ms 604 KB Output is correct
48 Correct 1 ms 476 KB Output is correct
49 Correct 1 ms 600 KB Output is correct
50 Correct 1 ms 604 KB Output is correct
51 Correct 2 ms 732 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Correct 1 ms 604 KB Output is correct
54 Correct 1 ms 604 KB Output is correct
55 Correct 77 ms 9920 KB Output is correct
56 Correct 143 ms 14840 KB Output is correct
57 Correct 75 ms 15176 KB Output is correct
58 Correct 63 ms 10568 KB Output is correct
59 Correct 109 ms 8100 KB Output is correct
60 Correct 126 ms 10324 KB Output is correct
61 Correct 95 ms 8148 KB Output is correct
62 Correct 111 ms 11336 KB Output is correct
63 Correct 76 ms 7132 KB Output is correct
64 Correct 102 ms 10564 KB Output is correct
65 Correct 123 ms 11084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 444 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 500 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 1 ms 480 KB Output is correct
31 Correct 2 ms 600 KB Output is correct
32 Correct 1 ms 476 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 2 ms 708 KB Output is correct
37 Correct 2 ms 604 KB Output is correct
38 Correct 2 ms 604 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 600 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 1 ms 604 KB Output is correct
47 Correct 1 ms 604 KB Output is correct
48 Correct 1 ms 476 KB Output is correct
49 Correct 1 ms 600 KB Output is correct
50 Correct 1 ms 604 KB Output is correct
51 Correct 2 ms 732 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Correct 1 ms 604 KB Output is correct
54 Correct 1 ms 604 KB Output is correct
55 Correct 107 ms 11904 KB Output is correct
56 Correct 164 ms 16456 KB Output is correct
57 Correct 105 ms 14656 KB Output is correct
58 Correct 89 ms 12192 KB Output is correct
59 Correct 93 ms 11584 KB Output is correct
60 Correct 98 ms 8776 KB Output is correct
61 Correct 115 ms 10056 KB Output is correct
62 Correct 157 ms 11836 KB Output is correct
63 Correct 113 ms 10148 KB Output is correct
64 Correct 131 ms 12800 KB Output is correct
65 Correct 107 ms 8232 KB Output is correct
66 Correct 102 ms 9284 KB Output is correct
67 Correct 143 ms 11328 KB Output is correct
68 Correct 155 ms 12728 KB Output is correct
69 Correct 107 ms 10932 KB Output is correct
70 Correct 172 ms 15636 KB Output is correct
71 Correct 172 ms 15744 KB Output is correct
72 Correct 89 ms 11664 KB Output is correct
73 Correct 90 ms 12292 KB Output is correct
74 Correct 128 ms 10280 KB Output is correct
75 Correct 154 ms 12040 KB Output is correct
76 Correct 113 ms 10092 KB Output is correct
77 Correct 117 ms 11984 KB Output is correct
78 Correct 123 ms 13376 KB Output is correct
79 Correct 113 ms 8640 KB Output is correct
80 Correct 134 ms 9960 KB Output is correct
81 Correct 136 ms 12604 KB Output is correct
82 Correct 155 ms 12280 KB Output is correct
83 Correct 77 ms 9920 KB Output is correct
84 Correct 143 ms 14840 KB Output is correct
85 Correct 75 ms 15176 KB Output is correct
86 Correct 63 ms 10568 KB Output is correct
87 Correct 109 ms 8100 KB Output is correct
88 Correct 126 ms 10324 KB Output is correct
89 Correct 95 ms 8148 KB Output is correct
90 Correct 111 ms 11336 KB Output is correct
91 Correct 76 ms 7132 KB Output is correct
92 Correct 102 ms 10564 KB Output is correct
93 Correct 123 ms 11084 KB Output is correct
94 Correct 121 ms 13380 KB Output is correct
95 Correct 155 ms 15680 KB Output is correct
96 Correct 105 ms 15736 KB Output is correct
97 Correct 96 ms 11836 KB Output is correct
98 Correct 100 ms 13300 KB Output is correct
99 Correct 115 ms 10308 KB Output is correct
100 Correct 130 ms 13212 KB Output is correct
101 Correct 117 ms 10820 KB Output is correct
102 Correct 127 ms 12768 KB Output is correct
103 Correct 147 ms 14056 KB Output is correct
104 Correct 109 ms 10636 KB Output is correct
105 Correct 139 ms 11844 KB Output is correct
106 Correct 156 ms 12304 KB Output is correct
107 Correct 97 ms 16948 KB Output is correct
108 Correct 110 ms 12608 KB Output is correct
109 Correct 109 ms 12860 KB Output is correct
110 Correct 111 ms 11860 KB Output is correct
111 Correct 113 ms 12444 KB Output is correct
112 Correct 109 ms 11404 KB Output is correct
113 Correct 134 ms 12480 KB Output is correct
114 Correct 124 ms 13016 KB Output is correct
115 Correct 117 ms 11472 KB Output is correct
116 Correct 109 ms 11580 KB Output is correct