Submission #678867

# Submission time Handle Problem Language Result Execution time Memory
678867 2023-01-06T19:19:04 Z sysia Joker (BOI20_joker) C++17
100 / 100
465 ms 18064 KB
//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;

void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef DEBUG
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

const int oo = 1e9+7;

struct UF{
    vector<int>rep, sz;
    vector<tuple<int, int, int, int>> st;
    int s;

    UF(int n){
        s = n;
        rep.assign(s, 0);
        iota(rep.begin(), rep.end(), 0);
        sz.assign(s, 1);
    }
    int f(int a){return a == rep[a] ? a : f(rep[a]);}
    bool sameset(int a, int b){return f(a) == f(b);}
    void undo(int t){
        while ((int)st.size() > t){
            auto [a, b, sA, sB] = st.back();
            st.pop_back();
            rep[a] = a;rep[b] = b;
            sz[a] = sA;sz[b] = sB;
        }
    }
    void u(int a, int b){
        a = f(a); b = f(b);
        if (a == b) return;
        if (sz[a] < sz[b]) swap(a, b);
        st.emplace_back(a, b, sz[a], sz[b]);
        sz[a] += sz[b];
        rep[b] = a;
    }
};

void solve(){
    int n, m, q; cin >> n >> m >> q;
    int N = 2*n+2;
    vector<pair<int, int>>edges;
    for (int i = 0; i<m; i++){
        int a, b; cin >> a >> b;
        edges.emplace_back(a, b);
    }
    vector<int>R(m, oo);
    UF dsu(N+2);
    auto merge = [&](int i){
        if (i == m) return true;
        auto [a, b] = edges[i];
        dsu.u(a, b+n);
        dsu.u(a+n, b);
        if (dsu.sameset(a, a+n)) return false;
        return true;
    };
   
    function<void(int, int, int, int)>rec = [&](int l, int r, int a, int b){
        //na przedziale [l, r] lewych końców odpowiedź należy do przedziału [a, b]
        if (l > r) return;
        if (a == b){
            for (int i = l; i <= r; i++) R[i] = a;
            return;
        }
        int mid = (l+r)/2;
        bool ok = 1;
        
        int t = (int)dsu.st.size();
        for (int i = l; i < mid; i++) ok &= merge(i); 
        if (ok && R[mid] == oo){
            for (int i = b; i >= mid; i--){ //odpowiedz in [a, b]
                ok &= merge(i);
                if (!ok){
                    R[mid] = i;
                    break;
                }
            }
            if (R[mid] == oo) R[mid] = mid;
        } else if (!ok) R[mid] = m;
        dsu.undo(t);
        // debug(l, r, mid, R[mid]);

        //left side
        ok = 1;
        for (int i = b; i>R[mid]; i--) ok &= merge(i);
        if (!ok) for (int i = l; i < mid; i++) R[i] = R[mid]; //is not bipartite
        else rec(l, mid-1, a, R[mid]); //check next
        dsu.undo(t);

        //right side
        ok = 1;
        for (int i = l; i<=mid; i++) ok &= merge(i);
        if (!ok) for (int i = mid+1; i<=r; i++) R[i] = m; 
        else rec(mid+1, r, R[mid], b);
        dsu.undo(t);
    };
    rec(0, m-1, 0, m);
    // debug(R);
    while (q--){
        int a, b; cin >> a >> b;
        --a;--b;
        cout << ((R[a] <= b) ? "NO\n" : "YES\n");
    }
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    solve();

    return 0;
}
# Verdict Execution time Memory 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 1 ms 212 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 328 KB Output is correct
20 Correct 1 ms 320 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 324 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory 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 1 ms 212 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 328 KB Output is correct
20 Correct 1 ms 320 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 324 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 2 ms 460 KB Output is correct
31 Correct 2 ms 468 KB Output is correct
32 Correct 2 ms 460 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 2 ms 468 KB Output is correct
35 Correct 3 ms 468 KB Output is correct
36 Correct 2 ms 468 KB Output is correct
37 Correct 3 ms 468 KB Output is correct
38 Correct 3 ms 468 KB Output is correct
39 Correct 2 ms 500 KB Output is correct
40 Correct 2 ms 468 KB Output is correct
41 Correct 2 ms 384 KB Output is correct
42 Correct 2 ms 468 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 2 ms 460 KB Output is correct
45 Correct 2 ms 460 KB Output is correct
46 Correct 3 ms 468 KB Output is correct
47 Correct 2 ms 460 KB Output is correct
48 Correct 2 ms 464 KB Output is correct
49 Correct 2 ms 468 KB Output is correct
50 Correct 2 ms 468 KB Output is correct
51 Correct 2 ms 340 KB Output is correct
52 Correct 2 ms 340 KB Output is correct
53 Correct 2 ms 468 KB Output is correct
54 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 228 ms 8448 KB Output is correct
4 Correct 464 ms 16740 KB Output is correct
5 Correct 139 ms 16424 KB Output is correct
6 Correct 148 ms 12096 KB Output is correct
7 Correct 162 ms 12148 KB Output is correct
8 Correct 184 ms 9632 KB Output is correct
9 Correct 213 ms 11444 KB Output is correct
10 Correct 307 ms 16796 KB Output is correct
11 Correct 215 ms 11880 KB Output is correct
12 Correct 238 ms 16400 KB Output is correct
13 Correct 162 ms 8996 KB Output is correct
14 Correct 178 ms 10180 KB Output is correct
15 Correct 305 ms 13444 KB Output is correct
16 Correct 309 ms 16708 KB Output is correct
# Verdict Execution time Memory 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 1 ms 212 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 328 KB Output is correct
20 Correct 1 ms 320 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 324 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 228 ms 8448 KB Output is correct
30 Correct 464 ms 16740 KB Output is correct
31 Correct 139 ms 16424 KB Output is correct
32 Correct 148 ms 12096 KB Output is correct
33 Correct 162 ms 12148 KB Output is correct
34 Correct 184 ms 9632 KB Output is correct
35 Correct 213 ms 11444 KB Output is correct
36 Correct 307 ms 16796 KB Output is correct
37 Correct 215 ms 11880 KB Output is correct
38 Correct 238 ms 16400 KB Output is correct
39 Correct 162 ms 8996 KB Output is correct
40 Correct 178 ms 10180 KB Output is correct
41 Correct 305 ms 13444 KB Output is correct
42 Correct 309 ms 16708 KB Output is correct
43 Correct 235 ms 12384 KB Output is correct
44 Correct 465 ms 16788 KB Output is correct
45 Correct 267 ms 17324 KB Output is correct
46 Correct 138 ms 12456 KB Output is correct
47 Correct 169 ms 12348 KB Output is correct
48 Correct 245 ms 11836 KB Output is correct
49 Correct 344 ms 16776 KB Output is correct
50 Correct 207 ms 11308 KB Output is correct
51 Correct 236 ms 13756 KB Output is correct
52 Correct 256 ms 16828 KB Output is correct
53 Correct 155 ms 9536 KB Output is correct
54 Correct 234 ms 11444 KB Output is correct
55 Correct 294 ms 13848 KB Output is correct
56 Correct 337 ms 16768 KB Output is correct
# Verdict Execution time Memory 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 1 ms 212 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 328 KB Output is correct
20 Correct 1 ms 320 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 324 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 2 ms 460 KB Output is correct
31 Correct 2 ms 468 KB Output is correct
32 Correct 2 ms 460 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 2 ms 468 KB Output is correct
35 Correct 3 ms 468 KB Output is correct
36 Correct 2 ms 468 KB Output is correct
37 Correct 3 ms 468 KB Output is correct
38 Correct 3 ms 468 KB Output is correct
39 Correct 2 ms 500 KB Output is correct
40 Correct 2 ms 468 KB Output is correct
41 Correct 2 ms 384 KB Output is correct
42 Correct 2 ms 468 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 2 ms 460 KB Output is correct
45 Correct 2 ms 460 KB Output is correct
46 Correct 3 ms 468 KB Output is correct
47 Correct 2 ms 460 KB Output is correct
48 Correct 2 ms 464 KB Output is correct
49 Correct 2 ms 468 KB Output is correct
50 Correct 2 ms 468 KB Output is correct
51 Correct 2 ms 340 KB Output is correct
52 Correct 2 ms 340 KB Output is correct
53 Correct 2 ms 468 KB Output is correct
54 Correct 2 ms 468 KB Output is correct
55 Correct 199 ms 10772 KB Output is correct
56 Correct 399 ms 16664 KB Output is correct
57 Correct 102 ms 16316 KB Output is correct
58 Correct 117 ms 10716 KB Output is correct
59 Correct 203 ms 10700 KB Output is correct
60 Correct 303 ms 11880 KB Output is correct
61 Correct 198 ms 10772 KB Output is correct
62 Correct 209 ms 16420 KB Output is correct
63 Correct 130 ms 8540 KB Output is correct
64 Correct 239 ms 11140 KB Output is correct
65 Correct 303 ms 16272 KB Output is correct
# Verdict Execution time Memory 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 1 ms 212 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 328 KB Output is correct
20 Correct 1 ms 320 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 324 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 2 ms 460 KB Output is correct
31 Correct 2 ms 468 KB Output is correct
32 Correct 2 ms 460 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 2 ms 468 KB Output is correct
35 Correct 3 ms 468 KB Output is correct
36 Correct 2 ms 468 KB Output is correct
37 Correct 3 ms 468 KB Output is correct
38 Correct 3 ms 468 KB Output is correct
39 Correct 2 ms 500 KB Output is correct
40 Correct 2 ms 468 KB Output is correct
41 Correct 2 ms 384 KB Output is correct
42 Correct 2 ms 468 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 2 ms 460 KB Output is correct
45 Correct 2 ms 460 KB Output is correct
46 Correct 3 ms 468 KB Output is correct
47 Correct 2 ms 460 KB Output is correct
48 Correct 2 ms 464 KB Output is correct
49 Correct 2 ms 468 KB Output is correct
50 Correct 2 ms 468 KB Output is correct
51 Correct 2 ms 340 KB Output is correct
52 Correct 2 ms 340 KB Output is correct
53 Correct 2 ms 468 KB Output is correct
54 Correct 2 ms 468 KB Output is correct
55 Correct 228 ms 8448 KB Output is correct
56 Correct 464 ms 16740 KB Output is correct
57 Correct 139 ms 16424 KB Output is correct
58 Correct 148 ms 12096 KB Output is correct
59 Correct 162 ms 12148 KB Output is correct
60 Correct 184 ms 9632 KB Output is correct
61 Correct 213 ms 11444 KB Output is correct
62 Correct 307 ms 16796 KB Output is correct
63 Correct 215 ms 11880 KB Output is correct
64 Correct 238 ms 16400 KB Output is correct
65 Correct 162 ms 8996 KB Output is correct
66 Correct 178 ms 10180 KB Output is correct
67 Correct 305 ms 13444 KB Output is correct
68 Correct 309 ms 16708 KB Output is correct
69 Correct 235 ms 12384 KB Output is correct
70 Correct 465 ms 16788 KB Output is correct
71 Correct 267 ms 17324 KB Output is correct
72 Correct 138 ms 12456 KB Output is correct
73 Correct 169 ms 12348 KB Output is correct
74 Correct 245 ms 11836 KB Output is correct
75 Correct 344 ms 16776 KB Output is correct
76 Correct 207 ms 11308 KB Output is correct
77 Correct 236 ms 13756 KB Output is correct
78 Correct 256 ms 16828 KB Output is correct
79 Correct 155 ms 9536 KB Output is correct
80 Correct 234 ms 11444 KB Output is correct
81 Correct 294 ms 13848 KB Output is correct
82 Correct 337 ms 16768 KB Output is correct
83 Correct 199 ms 10772 KB Output is correct
84 Correct 399 ms 16664 KB Output is correct
85 Correct 102 ms 16316 KB Output is correct
86 Correct 117 ms 10716 KB Output is correct
87 Correct 203 ms 10700 KB Output is correct
88 Correct 303 ms 11880 KB Output is correct
89 Correct 198 ms 10772 KB Output is correct
90 Correct 209 ms 16420 KB Output is correct
91 Correct 130 ms 8540 KB Output is correct
92 Correct 239 ms 11140 KB Output is correct
93 Correct 303 ms 16272 KB Output is correct
94 Correct 237 ms 13176 KB Output is correct
95 Correct 280 ms 17952 KB Output is correct
96 Correct 140 ms 17008 KB Output is correct
97 Correct 147 ms 13140 KB Output is correct
98 Correct 173 ms 13096 KB Output is correct
99 Correct 206 ms 11160 KB Output is correct
100 Correct 281 ms 16776 KB Output is correct
101 Correct 205 ms 12060 KB Output is correct
102 Correct 255 ms 14512 KB Output is correct
103 Correct 289 ms 16824 KB Output is correct
104 Correct 174 ms 10152 KB Output is correct
105 Correct 277 ms 13756 KB Output is correct
106 Correct 344 ms 16416 KB Output is correct
107 Correct 121 ms 18064 KB Output is correct
108 Correct 242 ms 12920 KB Output is correct
109 Correct 239 ms 12896 KB Output is correct
110 Correct 238 ms 12880 KB Output is correct
111 Correct 237 ms 12856 KB Output is correct
112 Correct 242 ms 12988 KB Output is correct
113 Correct 235 ms 12840 KB Output is correct
114 Correct 251 ms 12964 KB Output is correct
115 Correct 230 ms 12864 KB Output is correct
116 Correct 235 ms 12864 KB Output is correct