This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// "We create our own demons."
#include <bits/stdc++.h>
using namespace std;
#ifdef ONPC
#include "debug.h"
#else
#define dbg(...)
#endif
#define int long long
#define ll long long
#define ld long double
#define pi pair<int, int>
// vector
#define sz(a) (int)((a).size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(x) (*max_element(all(x)))
#define minel(x) (*min_element(all(x)))
#define maxpos(x) (max_element(all(x)) - x.begin())
#define minpos(x) (min_element(all(x)) - x.begin())
#define srt(x) (sort(all(x)))
#define rev(x) (reverse(all(x)))
// bitwise operations
#define cnt_bit(n) __builtin_popcountll(n)
#define low_bit(n) ((n) & (-(n)))
#define bit(n, i) (((n) >> (i)) & 1)
#define set_bit(n, i) ((n) | (1LL << (i)))
#define reset_bit(n, i) ((n) & ~(1LL << (i)))
#define flip_bit(n, i) ((n) ^ (1LL << (i)))
// math
#define sqr(n) ((n) * (n))
#define divup(a, b) (((a) + (b)-1) / (b))
// ostream
#define Fixed(a) cout << fixed << setprecision(12) << a;
template <class T> void chmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template <class T> void chmax(T& a, const T& b) { return b > a ? a = b, 1 : 0; }
template <class T> using min_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> using max_queue = priority_queue<T, vector<T>, less<T>>;
template <class T> using V = vector<T>;
using vi = V<int>;
using vd = V<ld>;
using vb = V<bool>;
using vpi = V<pi>;
using vvi = V<vi>;
using vvb = V<vb>;
const int mod = 1e9 + 7; // 998244353 1e9 + 7
const ll inf = (int)(1e18) + 7;
const int inf_s = 1e9 + 7;
const ld eps = 1e-9;
const int B = 32;
const int N = 1000 + 3;
const int logn = 20;
const int maxn = 2e5 + 7;
/////////////////////////solve/////////////////////////
int n, m, q;
pi e[maxn];
int lst[maxn];
int p[maxn], sz[maxn], len[maxn];
vi hist;
pi get(int a) {
if (a != p[a]) {
pi cur = get(p[a]);
return {cur.first, cur.second ^ len[a]};
}
return {a, 0};
}
bool unite(int i) {
if (i == m) return 0;
auto [a, x] = get(e[i].first);
auto [b, y] = get(e[i].second);
if (a == b) {
return x ^ y ^ 1;
}
if (sz[a] > sz[b]) swap(a, b);
sz[b] += sz[a];
p[a] = b;
len[a] = x ^ y ^ 1;
hist.push_back(a);
return 0;
}
void rb(int k) {
while (sz(hist) > k) {
int a = hist.back();
hist.pop_back();
sz[p[a]] -= sz[a];
p[a] = a;
len[a] = 0;
}
}
void divi(int l, int r, int tl, int tr) {
if (l > r) return;
int mid = (l + r) / 2;
bool ok = 0;
int k = sz(hist);
for (int i = l; i < mid; ++i) {
ok |= unite(i);
}
if (ok) {
lst[mid] = m;
dbg("d");
}
else {
for (int i = tr; i >= tl; --i) {
ok |= unite(i);
if (ok) {
lst[mid] = i;
break;
}
}
}
dbg(l, r, tl, tr, mid, lst[mid]);
rb(k);
ok = 0;
for (int i = l; i <= mid; ++i)
ok |= unite(i);
if (ok) {
for (int i = mid + 1; i <= r; ++i) {
lst[i] = m;
}
}
else divi(mid + 1, r, lst[mid], tr);
rb(k);
ok = 0;
for (int i = lst[mid] + 1; i <= tr; ++i)
ok |= unite(i);
divi(l, mid - 1, tl, lst[mid]);
rb(k);
}
void solve() {
cin >> n >> m >> q;
for (int i = 0; i < n; ++i) {
sz[i] = 1;
p[i] = i;
len[i] = 0;
}
for (int i = 0; i < m; ++i) {
int v, u;
cin >> v >> u;
--v; --u;
e[i] = {v, u};
}
divi(0, m - 1, 0, m);
for (int i = 0; i < m; ++i) {
dbg(lst[i], i);
}
while (q--) {
int l, r;
cin >> l >> r;
--l; --r;
cout << (r < lst[l] ? "YES" : "NO") << "\n";
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef ONPC
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
int t = 1;
//cin >> t;
for (int i = 1; i <= t; ++i) {
#ifdef ONPC
cerr << "===========" << i << "===========" << '\n';
#endif
solve();
}
#ifdef ONPC
cerr << endl << "Time " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
}
# | 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... |