Submission #877425

# Submission time Handle Problem Language Result Execution time Memory
877425 2023-11-23T08:35:13 Z CDuong Joker (BOI20_joker) C++17
0 / 100
123 ms 14916 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);
    }

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

    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] = rootu.ss ^ rootv.ss ^ edge_val;
            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) {
    // cout << l << " " << r << " " << optl << " " << optr << endl;

    if (l > r) return;

    int otimer = dsu.get_timer();

    int mid = (l + r) >> 1;
    for (int i = l; i <= mid; ++i)
        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;

    // cout << l << " " << r << " = " << opt << endl;

    dsu.reverse_to(timer);
    for (int i = optl; i <= opt; ++i)
        dsu.merge(edges[i].ff, edges[i].ss, 1);
    solveDP(mid + 1, r, opt, optr);

    // cout << "Finish right " << l << " " << r << endl;

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

    // cout << l << " " << r << " " << optl << " " << optr << endl;
}

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 <= n && 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(1, ptr - 1, 1, m);

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

    // for (int i = 1; i <= m; ++i) {
    //     cout << i << " " << best[i] << endl;
    // }
}

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

}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 348 KB Output is correct
5 Incorrect 1 ms 348 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 348 KB Output is correct
5 Incorrect 1 ms 348 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 123 ms 14916 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 348 KB Output is correct
5 Incorrect 1 ms 348 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 348 KB Output is correct
5 Incorrect 1 ms 348 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 348 KB Output is correct
5 Incorrect 1 ms 348 KB Output isn't correct
6 Halted 0 ms 0 KB -