This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
pair<int, int> E[200008];
int best[200008];
struct Disjoint_Set_Union {
static const int N = 2e5;
int par[N + 8], dis[N + 8], sz[N + 8];
vector<pair<int*, int>> history; int is_bi = 1;
Disjoint_Set_Union() {
iota(par, par + N + 8, 0);
fill(dis, dis + N + 8, 0);
fill(sz, sz + N + 8, 1);
}
pair<int, int> pu, pv; int du, dv;
pair<int, int> find(int u) {
du = 0;
while (par[u] != u) du ^= dis[u], u = par[u];
return {u, du};
}
void join(int u, int v) {
pu = find(u); pv = find(v);
u = pu.first; du = pu.second; v = pv.first; dv = pv.second;
if (u == v) {
if (du == dv) history.push_back({&is_bi, is_bi}), is_bi = 0;
return;
}
if (sz[u] < sz[v]) swap(u, v);
history.push_back({par + v, par[v]});
history.push_back({dis + v, dis[v]});
history.push_back({sz + u, sz[u]});
par[v] = u; dis[v] = du ^ dv ^ 1; sz[u] += sz[v];
}
int current_snapshot() {return history.size();}
void rollback_to(int snapshot) {
while (history.size() > snapshot) {
*history.back().first = history.back().second;
history.pop_back();
}
}
} DSU;
void dnc(int l, int r, int optl, int optr) {
if (l > r) return;
int mid = (l + r) / 2, cur = DSU.current_snapshot();
for (int i = l; i < mid; ++i) DSU.join(E[i].fi, E[i].se);
int post_L = DSU.current_snapshot();
for (int i = optr; i >= max(mid, optl); --i) {
if (!DSU.is_bi) {best[mid] = i; break;}
DSU.join(E[i].fi, E[i].se);
}
if (!best[mid]) best[mid] = mid - 1;
DSU.rollback_to(post_L); DSU.join(E[mid].fi, E[mid].se);
dnc(mid + 1, r, best[mid], optr);
DSU.rollback_to(cur);
for (int i = optr; i > best[mid]; --i) DSU.join(E[i].fi, E[i].se);
dnc(l, mid - 1, optl, best[mid]);
DSU.rollback_to(cur);
}
signed main() {
// freopen("NOHOME.INP", "r", stdin);
// freopen("NOHOME.OUT", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, m, q, l, r; cin >> n >> m >> q;
for (int i = 1; i <= m; ++i) cin >> E[i].first >> E[i].second;
dnc(1, m, 1, m);
// for (int i = 1; i <= m; ++i) cout << best[i] << ' ';
// cout << '\n';
while (q--) cin >> l >> r, cout << (best[l] >= r ? "YES\n" : "NO\n");
}
Compilation message (stderr)
Joker.cpp: In member function 'void Disjoint_Set_Union::rollback_to(int)':
Joker.cpp:40:25: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int*, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
40 | while (history.size() > snapshot) {
| ~~~~~~~~~~~~~~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |