답안 #898716

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
898716 2024-01-05T03:59:19 Z GrandTiger1729 Joker (BOI20_joker) C++17
100 / 100
444 ms 8140 KB
#include <bits/stdc++.h>
using namespace std;

struct DSU
{
    vector<int> rt, sz;
    vector<int> stk;
    DSU(int n)
    {
        rt.resize(n);
        iota(rt.begin(), rt.end(), 0);
        sz.resize(n, 1);
    }
    int find(int u)
    {
        if (u == rt[u])
        {
            return u;
        }
        return find(rt[u]);
    }
    bool same(int u, int v)
    {
        return find(u) == find(v);
    }
    void unite(int u, int v)
    {
        u = find(u), v = find(v);
        if (u == v)
        {
            stk.push_back(-1);
            return;
        }
        if (sz[u] > sz[v])
        {
            swap(u, v);
        }
        rt[u] = v;
        sz[v] += sz[u];
        stk.push_back(u);
    }
    void undo()
    {
        int u = stk.back();
        stk.pop_back();
        if (u == -1)
        {
            return;
        }
        int v = rt[u];
        sz[v] -= sz[u];
        rt[u] = u;
    }
};

const int B = 17, C = 400, D = 21;
int main()
{
    cin.tie(0)->sync_with_stdio(0);
    int n, m, q;
    cin >> n >> m >> q;
    vector<pair<int, int>> ed(m);
    for (int i = 0; i < m; i++)
    {
        int u, v;
        cin >> u >> v;
        u--, v--;
        ed[i] = make_pair(u, v);
    }
    vector<int> rr(m, -1);
    DSU dsu(2 * n);
    for (int c = 0; c * C * B < m; c++)
    {
        int L1 = c * C * B, L2 = min(m, (c + 1) * C * B);
        int R = -1;
        for (int i = 0; i < L1; i++)
        {
            auto [u, v] = ed[i];
            dsu.unite(u, v + n);
            dsu.unite(u + n, v);
            if (dsu.same(u, v))
            {
                R = i;
                break;
            }
        }
        if (R != -1)
        {
            break;
        }
        for (int i = L1; i < L2; i++)
        {
            auto [u, v] = ed[i];
            dsu.unite(u, v + n);
            dsu.unite(u + n, v);
            if (dsu.same(u, v))
            {
                R = i;
                break;
            }
        }
        if (R == -1)
        {
            R = m - 1;
            while (R >= L2)
            {
                auto [u, v] = ed[R];
                dsu.unite(u, v + n);
                dsu.unite(u + n, v);
                if (dsu.same(u, v))
                {
                    dsu.undo();
                    dsu.undo();
                    break;
                }
                R--;
            }
            for (int i = m - 1; i > R; i--)
            {
                dsu.undo();
                dsu.undo();
            }
            for (int i = L1; i < L2; i++)
            {
                dsu.undo();
                dsu.undo();
            }
        }
        else
        {
            for (int i = L1; i <= R; i++)
            {
                dsu.undo();
                dsu.undo();
            }
            R = m - 1;
        }
        for (int i = m - 1; i > R; i--)
        {
            auto [u, v] = ed[i];
            dsu.unite(u, v + n);
            dsu.unite(u + n, v);
        }
        for (int d = (L2 - 1 - L1) / (B * D); d >= 0; d--)
        {
            int ll1 = L1 + d * B * D, ll2 = min(L2, ll1 + B * D);
            int r2 = -1;
            for (int i = L1; i < ll1; i++)
            {
                auto [u, v] = ed[i];
                dsu.unite(u, v + n);
                dsu.unite(u + n, v);
                if (dsu.same(u, v))
                {
                    r2 = i;
                    break;
                }
            }
            if (r2 != -1)
            {
                for (int i = L1; i <= r2; i++)
                {
                    dsu.undo();
                    dsu.undo();
                }
                continue;
            }
            int ll3 = ll2 - 1;
            for (int i = ll1; i < ll2; i++)
            {
                auto [u, v] = ed[i];
                dsu.unite(u, v + n);
                dsu.unite(u + n, v);
                if (dsu.same(u, v))
                {
                    ll3 = i;
                    break;
                }
            }
            r2 = R;
            for (int b = (ll3 - ll1) / B; b >= 0; b--)
            {
                int l1 = ll1 + b * B, l2 = min(ll3 + 1, l1 + B);
                for (int i = l1; i < l2; i++)
                {
                    dsu.undo();
                    dsu.undo();
                }
                for (int i = R; i > r2; i--)
                {
                    auto [u, v] = ed[i];
                    dsu.unite(u, v + n);
                    dsu.unite(u + n, v);
                }
                int r = r2;
                bool f = 0;
                for (int i = r; i >= l1; i--)
                {
                    int l = l1;
                    while (l < l2 && l <= i)
                    {
                        auto [u, v] = ed[l];
                        dsu.unite(u, v + n);
                        dsu.unite(u + n, v);
                        if (dsu.same(u, v))
                        {
                            break;
                        }
                        l++;
                    }
                    rr[i] = max(rr[i], l);
                    l = min(l, l2 - 1);
                    l = min(l, i);
                    for (int j = l1; j <= l; j++)
                    {
                        dsu.undo();
                        dsu.undo();
                    }
                    auto [u, v] = ed[i];
                    dsu.unite(u, v + n);
                    dsu.unite(u + n, v);
                    if (dsu.same(u, v))
                    {
                        r2 = i;
                        f = 1;
                        break;
                    }
                }
                if (!f)
                {
                    r2 = l1;
                }
                for (int i = r2; i <= R; i++)
                {
                    dsu.undo();
                    dsu.undo();
                }
            }
            for (int i = L1; i < ll1; i++)
            {
                dsu.undo();
                dsu.undo();
            }
            for (int i = r2 + 1; i <= R; i++)
            {
                auto [u, v] = ed[i];
                dsu.unite(u, v + n);
                dsu.unite(u + n, v);
            }
            R = r2;
        }
        while (dsu.stk.size())
        {
            dsu.undo();
        }
    }
    while (q--)
    {
        int l, r;
        cin >> l >> r;
        l--, r--;
        cout << (rr[r] >= l ? "NO" : "YES") << '\n';
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 504 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 460 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 344 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 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 504 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 460 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 344 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 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 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 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 2 ms 344 KB Output is correct
38 Correct 2 ms 348 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 1 ms 344 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 2 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 164 ms 5796 KB Output is correct
4 Correct 437 ms 8136 KB Output is correct
5 Correct 185 ms 7900 KB Output is correct
6 Correct 120 ms 5800 KB Output is correct
7 Correct 132 ms 5840 KB Output is correct
8 Correct 206 ms 5432 KB Output is correct
9 Correct 199 ms 5900 KB Output is correct
10 Correct 234 ms 7572 KB Output is correct
11 Correct 183 ms 5840 KB Output is correct
12 Correct 207 ms 7368 KB Output is correct
13 Correct 193 ms 4488 KB Output is correct
14 Correct 187 ms 5228 KB Output is correct
15 Correct 212 ms 6480 KB Output is correct
16 Correct 261 ms 7272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 504 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 460 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 344 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 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 164 ms 5796 KB Output is correct
30 Correct 437 ms 8136 KB Output is correct
31 Correct 185 ms 7900 KB Output is correct
32 Correct 120 ms 5800 KB Output is correct
33 Correct 132 ms 5840 KB Output is correct
34 Correct 206 ms 5432 KB Output is correct
35 Correct 199 ms 5900 KB Output is correct
36 Correct 234 ms 7572 KB Output is correct
37 Correct 183 ms 5840 KB Output is correct
38 Correct 207 ms 7368 KB Output is correct
39 Correct 193 ms 4488 KB Output is correct
40 Correct 187 ms 5228 KB Output is correct
41 Correct 212 ms 6480 KB Output is correct
42 Correct 261 ms 7272 KB Output is correct
43 Correct 177 ms 5840 KB Output is correct
44 Correct 436 ms 8140 KB Output is correct
45 Correct 310 ms 8136 KB Output is correct
46 Correct 122 ms 5836 KB Output is correct
47 Correct 138 ms 5836 KB Output is correct
48 Correct 203 ms 5916 KB Output is correct
49 Correct 256 ms 7520 KB Output is correct
50 Correct 183 ms 5488 KB Output is correct
51 Correct 196 ms 6508 KB Output is correct
52 Correct 191 ms 7376 KB Output is correct
53 Correct 180 ms 4556 KB Output is correct
54 Correct 209 ms 5492 KB Output is correct
55 Correct 224 ms 6424 KB Output is correct
56 Correct 242 ms 7372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 504 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 460 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 344 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 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 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 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 2 ms 344 KB Output is correct
38 Correct 2 ms 348 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 1 ms 344 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 2 ms 348 KB Output is correct
55 Correct 144 ms 5488 KB Output is correct
56 Correct 444 ms 7888 KB Output is correct
57 Correct 153 ms 7632 KB Output is correct
58 Correct 97 ms 5328 KB Output is correct
59 Correct 176 ms 5324 KB Output is correct
60 Correct 199 ms 6352 KB Output is correct
61 Correct 149 ms 5328 KB Output is correct
62 Correct 201 ms 6748 KB Output is correct
63 Correct 158 ms 4568 KB Output is correct
64 Correct 190 ms 5844 KB Output is correct
65 Correct 213 ms 6608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 504 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 460 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 344 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 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 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 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 2 ms 344 KB Output is correct
38 Correct 2 ms 348 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 1 ms 344 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 2 ms 348 KB Output is correct
55 Correct 164 ms 5796 KB Output is correct
56 Correct 437 ms 8136 KB Output is correct
57 Correct 185 ms 7900 KB Output is correct
58 Correct 120 ms 5800 KB Output is correct
59 Correct 132 ms 5840 KB Output is correct
60 Correct 206 ms 5432 KB Output is correct
61 Correct 199 ms 5900 KB Output is correct
62 Correct 234 ms 7572 KB Output is correct
63 Correct 183 ms 5840 KB Output is correct
64 Correct 207 ms 7368 KB Output is correct
65 Correct 193 ms 4488 KB Output is correct
66 Correct 187 ms 5228 KB Output is correct
67 Correct 212 ms 6480 KB Output is correct
68 Correct 261 ms 7272 KB Output is correct
69 Correct 177 ms 5840 KB Output is correct
70 Correct 436 ms 8140 KB Output is correct
71 Correct 310 ms 8136 KB Output is correct
72 Correct 122 ms 5836 KB Output is correct
73 Correct 138 ms 5836 KB Output is correct
74 Correct 203 ms 5916 KB Output is correct
75 Correct 256 ms 7520 KB Output is correct
76 Correct 183 ms 5488 KB Output is correct
77 Correct 196 ms 6508 KB Output is correct
78 Correct 191 ms 7376 KB Output is correct
79 Correct 180 ms 4556 KB Output is correct
80 Correct 209 ms 5492 KB Output is correct
81 Correct 224 ms 6424 KB Output is correct
82 Correct 242 ms 7372 KB Output is correct
83 Correct 144 ms 5488 KB Output is correct
84 Correct 444 ms 7888 KB Output is correct
85 Correct 153 ms 7632 KB Output is correct
86 Correct 97 ms 5328 KB Output is correct
87 Correct 176 ms 5324 KB Output is correct
88 Correct 199 ms 6352 KB Output is correct
89 Correct 149 ms 5328 KB Output is correct
90 Correct 201 ms 6748 KB Output is correct
91 Correct 158 ms 4568 KB Output is correct
92 Correct 190 ms 5844 KB Output is correct
93 Correct 213 ms 6608 KB Output is correct
94 Correct 171 ms 5804 KB Output is correct
95 Correct 371 ms 8140 KB Output is correct
96 Correct 188 ms 7784 KB Output is correct
97 Correct 127 ms 5840 KB Output is correct
98 Correct 131 ms 5840 KB Output is correct
99 Correct 202 ms 5432 KB Output is correct
100 Correct 199 ms 7512 KB Output is correct
101 Correct 176 ms 5564 KB Output is correct
102 Correct 201 ms 6692 KB Output is correct
103 Correct 240 ms 7628 KB Output is correct
104 Correct 180 ms 5068 KB Output is correct
105 Correct 212 ms 6268 KB Output is correct
106 Correct 245 ms 7116 KB Output is correct
107 Correct 125 ms 8132 KB Output is correct
108 Correct 169 ms 5832 KB Output is correct
109 Correct 171 ms 6088 KB Output is correct
110 Correct 177 ms 5836 KB Output is correct
111 Correct 178 ms 5856 KB Output is correct
112 Correct 168 ms 5812 KB Output is correct
113 Correct 175 ms 5836 KB Output is correct
114 Correct 174 ms 5792 KB Output is correct
115 Correct 167 ms 5840 KB Output is correct
116 Correct 174 ms 5836 KB Output is correct