#include<bits/stdc++.h>
#define ll long long
#define task ""
using namespace std;
const int maxn = 4e5 + 2;
int n, m, q, max_r[maxn], u[maxn], v[maxn], l, r, par[maxn];
vector<pair<int, int>> rb;
int find_set (int u)
{
if (par[u] < 0) return u;
return find_set(par[u]);
}
void union_set (int u, int v)
{
int pu = find_set(u), pv = find_set(v);
if (pu == pv) return;
if (par[pu] > par[pv]) swap(pu, pv);
rb.push_back({pu, par[pu]});
rb.push_back({pv, par[pv]});
par[pu] += par[pv];
par[pv] = pu;
}
void rollback ()
{
if (rb.empty()) return;
par[rb.back().first] = rb.back().second;
rb.pop_back();
}
void pbs (int low, int high, int r)
{
if (low > high) return;
//cout << low << " " << high << " " << r << endl;
int mid = (low + high)/2, tmp = rb.size();
for (int i = max(low - 1, 1); i < mid; i++)
{
//cout << i << endl;
union_set(u[i], v[i] + n);
union_set(u[i] + n, v[i]);
if (find_set(u[i]) == find_set(u[i] + n))
{
for (int j = mid; j <= high; j++) max_r[j] = r;
while ((int)rb.size() > tmp) rollback();
pbs(low, mid - 1, r);
return;
}
}
int tmp2 = rb.size();
for (int i = r - 1; i >= low; i--)
{
union_set(u[i], v[i] + n);
union_set(u[i] + n, v[i]);
if (find_set(u[i]) == find_set(u[i] + n))
{
max_r[mid] = i;
break;
}
}
while ((int)rb.size() > tmp2) rollback();
pbs(mid + 1, high, r);
while ((int)rb.size() > tmp) rollback();
for (int i = r - 1; i >= max(max_r[mid] - 1, low); i--)
{
union_set(u[i], v[i] + n);
union_set(u[i] + n, v[i]);
if (find_set(u[i]) == find_set(u[i] + n))
{
for (int j = low; j <= mid; j++) max_r[j] = max_r[mid];
return;
}
}
pbs(low, mid - 1, max_r[mid]);
}
int main ()
{
//freopen(task".INP", "r", stdin);
//freopen(task".OUT", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> q;
for (int i = 1; i <= 2*n; i++) par[i] = -1;
for (int i = 1; i <= m; i++) cin >> u[i] >> v[i];
pbs(1, m, m + 1);
while (q--)
{
cin >> l >> r;
cout << (r < max_r[l] ? "YES" : "NO") << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
4444 KB |
Output is correct |
5 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
4444 KB |
Output is correct |
5 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
109 ms |
9732 KB |
Output is correct |
4 |
Correct |
92 ms |
14532 KB |
Output is correct |
5 |
Correct |
61 ms |
14276 KB |
Output is correct |
6 |
Correct |
63 ms |
9424 KB |
Output is correct |
7 |
Correct |
66 ms |
9428 KB |
Output is correct |
8 |
Correct |
80 ms |
8136 KB |
Output is correct |
9 |
Correct |
79 ms |
9168 KB |
Output is correct |
10 |
Correct |
104 ms |
9676 KB |
Output is correct |
11 |
Correct |
86 ms |
9160 KB |
Output is correct |
12 |
Correct |
91 ms |
9672 KB |
Output is correct |
13 |
Correct |
71 ms |
6864 KB |
Output is correct |
14 |
Correct |
65 ms |
7960 KB |
Output is correct |
15 |
Correct |
101 ms |
9172 KB |
Output is correct |
16 |
Correct |
109 ms |
10196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
4444 KB |
Output is correct |
5 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
4444 KB |
Output is correct |
5 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
4444 KB |
Output is correct |
5 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |