This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int N, M, Q;
int parents[600069];
int sz[600069];
int ans[600069];
#define ROOT 20
struct E {
int i, a;
int j, b;
};
struct R {
int i, l, r, bl;
};
vector<E> moves;
void reset() {
for (int i = 0; i < 2 * N; i++) {
parents[i] = i;
sz[i] = 1;
}
moves.clear();
}
int getp(int i) {
return i == parents[i] ? i : getp(parents[i]);
}
bool join(int a, int b) {
a = getp(a);
b = getp(b);
if(a == b) {
moves.push_back({-1});
return false;
}
if(sz[a] > sz[b]) {
moves.push_back({b, parents[b], a, sz[a]});
parents[b] = a;
sz[a] += sz[b];
}else{
moves.push_back({a, parents[a], b, sz[b]});
parents[a] = b;
sz[b] += sz[a];
}
return getp(a) == getp((a + N) % (2 * N)) or getp(b) == getp((b + N) % (2 * N));
}
void reload() {
auto p = moves.back();
moves.pop_back();
if(p.i == -1)
return;
parents[p.i] = p.a;
sz[p.j] = p.b;
}
int A[200069];
int B[200069];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M >> Q;
for (int i = 1; i <= M; i++) {
int a, b;
cin >> a >> b;
A[i] = --a;
B[i] = --b;
}
vector<R> queries;
for (int i = 0; i < Q; i++) {
int l, r;
cin >> l >> r;
queries.push_back({i, l, r, l / ROOT});
}
sort(queries.begin(), queries.end(), [](R& a, R& b) {
return a.bl < b.bl or (a.bl == b.bl and a.r > b.r);
});
bool made = false;
int pbl = -1;
int nxt = 0;
int up = 0;
int start = 1;
bool doable = false;
reset();
for (const R& a : queries) {
int Bs = a.bl * ROOT;
if(a.bl != pbl) {
doable = false;
for(int i = 0; i < up; i++) {
reload();
}
for (; start < Bs; start++) {
made |= join(A[start], B[start] + N);
made |= join(A[start] + N, B[start]);
}
nxt = M;
pbl = a.bl;
}
for(; nxt > a.r; nxt--) {
doable |= join(A[nxt], B[nxt] + N);
doable |= join(A[nxt] + N, B[nxt]);
up += 2;
}
bool k = false;
int cnt = 0;
for (int i = Bs; i < a.l; i++) {
if(i == 0) continue;
k |= join(A[i], B[i] + N);
k |= join(A[i] + N, B[i]);
cnt += 2;
}
ans[a.i] = made or k or doable;
for (int i = 0; i < cnt; i++) {
reload();
}
}
for(int i = 0; i < Q; i++) {
cout << (ans[i] ? "YES" : "NO") << '\n';
}
return 0;
}
# | 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... |