#include <bits/stdc++.h>
using namespace std;
struct DSU
{
vector<int> rt, sz;
vector<int> stk;
DSU(int n)
{
rt.resize(n);
iota(rt.begin(), rt.end(), 0);
sz.resize(n, 1);
}
int find(int u)
{
if (u == rt[u])
{
return u;
}
return find(rt[u]);
}
bool same(int u, int v)
{
return find(u) == find(v);
}
void unite(int u, int v)
{
u = find(u), v = find(v);
if (u == v)
{
stk.push_back(-1);
return;
}
if (sz[u] > sz[v])
{
swap(u, v);
}
rt[u] = v;
sz[v] += sz[u];
stk.push_back(u);
}
void undo()
{
int u = stk.back();
stk.pop_back();
if (u == -1)
{
return;
}
int v = rt[u];
sz[v] -= sz[u];
rt[u] = u;
}
};
const int B = 1, C = 1, D = 1;
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n, m, q;
cin >> n >> m >> q;
vector<pair<int, int>> ed(m);
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v;
u--, v--;
ed[i] = make_pair(u, v);
}
vector<int> rr(m, -1);
for (int c = 0; c * C * B < m; c++)
{
int L1 = c * C * B, L2 = min(m, (c + 1) * C * B);
int R = -1;
DSU dsu(2 * n);
{
for (int i = 0; i < L1; i++)
{
auto [u, v] = ed[i];
dsu.unite(u, v + n);
dsu.unite(u + n, v);
if (dsu.same(u, v))
{
R = i;
break;
}
}
if (R != -1)
{
break;
}
for (int i = L1; i < L2; i++)
{
auto [u, v] = ed[i];
dsu.unite(u, v + n);
dsu.unite(u + n, v);
if (dsu.same(u, v))
{
R = i;
break;
}
}
if (R == -1)
{
R = m - 1;
while (R >= L2)
{
auto [u, v] = ed[R];
dsu.unite(u, v + n);
dsu.unite(u + n, v);
if (dsu.same(u, v))
{
dsu.undo();
dsu.undo();
break;
}
R--;
}
for (int i = m - 1; i > R; i--)
{
dsu.undo();
dsu.undo();
}
for (int i = L1; i < L2; i++)
{
dsu.undo();
dsu.undo();
}
}
else
{
for (int i = L1; i <= R; i++)
{
dsu.undo();
dsu.undo();
}
R = m - 1;
}
for (int i = m - 1; i > R; i--)
{
auto [u, v] = ed[i];
dsu.unite(u, v + n);
dsu.unite(u + n, v);
}
}
for (int d = (L2 - 1 - L1) / (B * D); d >= 0; d--)
{
int ll = L1 + d * B * D;
int r2 = -1;
for (int i = L1; i < ll; i++)
{
auto [u, v] = ed[i];
dsu.unite(u, v + n);
dsu.unite(u + n, v);
if (dsu.same(u, v))
{
r2 = i;
break;
}
}
if (r2 != -1)
{
for (int i = L1; i <= r2; i++)
{
dsu.undo();
dsu.undo();
}
continue;
}
for (int i = ll; i < L2; i++)
{
auto [u, v] = ed[i];
dsu.unite(u, v + n);
dsu.unite(u + n, v);
}
r2 = R;
for (int b = (L2 - 1 - ll) / B; b >= 0; b--)
{
int l1 = ll + b * B, l2 = min(L2, l1 + B);
for (int i = l1; i < l2; i++)
{
dsu.undo();
dsu.undo();
}
for (int i = R; i > r2; i--)
{
auto [u, v] = ed[i];
dsu.unite(u, v + n);
dsu.unite(u + n, v);
}
int r = r2;
bool f = 0;
for (int i = r; i >= l1; i--)
{
int l = l1;
while (l < l2 && l <= i)
{
auto [u, v] = ed[l];
dsu.unite(u, v + n);
dsu.unite(u + n, v);
if (dsu.same(u, v))
{
break;
}
l++;
}
rr[i] = max(rr[i], l);
l = min(l, l2 - 1);
l = min(l, i);
for (int j = l1; j <= l; j++)
{
dsu.undo();
dsu.undo();
}
auto [u, v] = ed[i];
dsu.unite(u, v + n);
dsu.unite(u + n, v);
if (dsu.same(u, v))
{
r2 = i;
f = 1;
break;
}
}
if (!f)
{
r2 = l1;
}
for (int i = r2; i <= R; i++)
{
dsu.undo();
dsu.undo();
}
}
for (int i = L1; i < ll; i++)
{
dsu.undo();
dsu.undo();
}
for (int i = r2 + 1; i <= R; i++)
{
auto [u, v] = ed[i];
dsu.unite(u, v + n);
dsu.unite(u + n, v);
}
R = r2;
}
}
while (q--)
{
int l, r;
cin >> l >> r;
l--, r--;
cout << (rr[r] >= l ? "NO" : "YES") << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
456 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
456 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
18 ms |
348 KB |
Output is correct |
30 |
Correct |
18 ms |
600 KB |
Output is correct |
31 |
Correct |
15 ms |
344 KB |
Output is correct |
32 |
Correct |
7 ms |
344 KB |
Output is correct |
33 |
Correct |
3 ms |
344 KB |
Output is correct |
34 |
Correct |
17 ms |
548 KB |
Output is correct |
35 |
Correct |
23 ms |
476 KB |
Output is correct |
36 |
Correct |
17 ms |
344 KB |
Output is correct |
37 |
Correct |
56 ms |
348 KB |
Output is correct |
38 |
Correct |
73 ms |
348 KB |
Output is correct |
39 |
Correct |
24 ms |
344 KB |
Output is correct |
40 |
Correct |
8 ms |
600 KB |
Output is correct |
41 |
Correct |
10 ms |
548 KB |
Output is correct |
42 |
Correct |
9 ms |
348 KB |
Output is correct |
43 |
Correct |
11 ms |
548 KB |
Output is correct |
44 |
Correct |
11 ms |
348 KB |
Output is correct |
45 |
Correct |
12 ms |
344 KB |
Output is correct |
46 |
Correct |
22 ms |
348 KB |
Output is correct |
47 |
Correct |
12 ms |
348 KB |
Output is correct |
48 |
Correct |
14 ms |
348 KB |
Output is correct |
49 |
Correct |
16 ms |
348 KB |
Output is correct |
50 |
Correct |
20 ms |
344 KB |
Output is correct |
51 |
Correct |
13 ms |
348 KB |
Output is correct |
52 |
Correct |
15 ms |
460 KB |
Output is correct |
53 |
Correct |
15 ms |
348 KB |
Output is correct |
54 |
Correct |
22 ms |
576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Execution timed out |
2061 ms |
6408 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
456 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Execution timed out |
2061 ms |
6408 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
456 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
18 ms |
348 KB |
Output is correct |
30 |
Correct |
18 ms |
600 KB |
Output is correct |
31 |
Correct |
15 ms |
344 KB |
Output is correct |
32 |
Correct |
7 ms |
344 KB |
Output is correct |
33 |
Correct |
3 ms |
344 KB |
Output is correct |
34 |
Correct |
17 ms |
548 KB |
Output is correct |
35 |
Correct |
23 ms |
476 KB |
Output is correct |
36 |
Correct |
17 ms |
344 KB |
Output is correct |
37 |
Correct |
56 ms |
348 KB |
Output is correct |
38 |
Correct |
73 ms |
348 KB |
Output is correct |
39 |
Correct |
24 ms |
344 KB |
Output is correct |
40 |
Correct |
8 ms |
600 KB |
Output is correct |
41 |
Correct |
10 ms |
548 KB |
Output is correct |
42 |
Correct |
9 ms |
348 KB |
Output is correct |
43 |
Correct |
11 ms |
548 KB |
Output is correct |
44 |
Correct |
11 ms |
348 KB |
Output is correct |
45 |
Correct |
12 ms |
344 KB |
Output is correct |
46 |
Correct |
22 ms |
348 KB |
Output is correct |
47 |
Correct |
12 ms |
348 KB |
Output is correct |
48 |
Correct |
14 ms |
348 KB |
Output is correct |
49 |
Correct |
16 ms |
348 KB |
Output is correct |
50 |
Correct |
20 ms |
344 KB |
Output is correct |
51 |
Correct |
13 ms |
348 KB |
Output is correct |
52 |
Correct |
15 ms |
460 KB |
Output is correct |
53 |
Correct |
15 ms |
348 KB |
Output is correct |
54 |
Correct |
22 ms |
576 KB |
Output is correct |
55 |
Execution timed out |
2054 ms |
8688 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
456 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
18 ms |
348 KB |
Output is correct |
30 |
Correct |
18 ms |
600 KB |
Output is correct |
31 |
Correct |
15 ms |
344 KB |
Output is correct |
32 |
Correct |
7 ms |
344 KB |
Output is correct |
33 |
Correct |
3 ms |
344 KB |
Output is correct |
34 |
Correct |
17 ms |
548 KB |
Output is correct |
35 |
Correct |
23 ms |
476 KB |
Output is correct |
36 |
Correct |
17 ms |
344 KB |
Output is correct |
37 |
Correct |
56 ms |
348 KB |
Output is correct |
38 |
Correct |
73 ms |
348 KB |
Output is correct |
39 |
Correct |
24 ms |
344 KB |
Output is correct |
40 |
Correct |
8 ms |
600 KB |
Output is correct |
41 |
Correct |
10 ms |
548 KB |
Output is correct |
42 |
Correct |
9 ms |
348 KB |
Output is correct |
43 |
Correct |
11 ms |
548 KB |
Output is correct |
44 |
Correct |
11 ms |
348 KB |
Output is correct |
45 |
Correct |
12 ms |
344 KB |
Output is correct |
46 |
Correct |
22 ms |
348 KB |
Output is correct |
47 |
Correct |
12 ms |
348 KB |
Output is correct |
48 |
Correct |
14 ms |
348 KB |
Output is correct |
49 |
Correct |
16 ms |
348 KB |
Output is correct |
50 |
Correct |
20 ms |
344 KB |
Output is correct |
51 |
Correct |
13 ms |
348 KB |
Output is correct |
52 |
Correct |
15 ms |
460 KB |
Output is correct |
53 |
Correct |
15 ms |
348 KB |
Output is correct |
54 |
Correct |
22 ms |
576 KB |
Output is correct |
55 |
Execution timed out |
2061 ms |
6408 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |