Submission #678851

# Submission time Handle Problem Language Result Execution time Memory
678851 2023-01-06T17:23:38 Z sysia Joker (BOI20_joker) C++17
0 / 100
238 ms 19200 KB
//Sylwia Sapkowska
#include <bits/stdc++.h>
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

#define int long long
const int oo = 1e18, oo2 = 1e9+7, K = 30;
const int mod = 998244353;

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

    UF(int n){
        s = n;
        clear();
    }

    void clear(){
        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]);
    }

    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;
    }

    bool sameset(int a, int b){
        return f(a) == f(b);
    }
};

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){
        auto [a, b] = edges[i];
        dsu.u(a, b+n);
        dsu.u(a+n, b);
        if (dsu.sameset(a, b) || dsu.sameset(a+n, b+n)) return false;
        return true;
    };
   
    function<void(int, int, int, int)>rec = [&](int l, int r, int a, int 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;
        //calculate R[mid]
        int t = (int)dsu.st.size();
        for (int i = min(l, a); i < mid; i++) ok &= merge(i); 
        R[mid] = b;
        if (ok){
            for (int i = max(b-1, r); i >= mid; i--){
                ok &= merge(i);
                if (!ok){
                    R[mid] = i+1;
                    break;
                }
            }
        }
        dsu.undo(t);
        ok = 1;
        for (int i = r; i>=max(mid, R[mid]+1); i--) ok &= merge(i);
        if (ok) rec(l, mid-1, a, R[mid]);
        dsu.undo(t);
        ok = 1;
        for (int i = l; i<=min(mid, R[mid]-1); i++) ok &= merge(i);
        if (ok) rec(mid+1, r, max(mid+1, R[mid]), b);
        dsu.undo(t);
    };
    rec(0, m-1, 0, m);
    // debug(R);
    while (q--){
        int a, b; cin >> a >> b;
        --a;
        cout << ((R[a] > b) ? "YES\n" : "NO\n");
    }
}

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

    int t = 1;
    // cin >> t;
    while (t--) 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 1 ms 320 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Incorrect 1 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 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 1 ms 320 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Incorrect 1 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 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 Incorrect 238 ms 19200 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 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 1 ms 320 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Incorrect 1 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 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 1 ms 320 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Incorrect 1 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 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 1 ms 320 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Incorrect 1 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -