#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
struct dsu{
vector<pair<int&, int>> hist;
vector<int> sz, p;
vector<bool> f;
int n;
dsu(int ns){
n = ns;
p.resize(2 * n + 1);
sz.resize(2 * n + 1);
for (int i = 1; i <= 2 * n; i++){
p[i] = i;
sz[i] = 1;
}
}
int get(int v){
if (p[v] != v){
return get(p[v]);
}
return v;
}
int op(int v){
return v + n;
}
void unite(int x, int y){
x = get(x); y = get(y);
if (x == y) return;
if (sz[x] > sz[y]) swap(x, y);
hist.pb({p[x], p[x]});
hist.pb({sz[y], sz[y]});
p[x] = y;
sz[y] += sz[x];
}
bool add(pii P){
auto [x, y] = P;
if (!x) return 0;
unite(x, op(y));
unite(y, op(x));
bool k = ((!f.empty() && f.back()) | (get(x) == get(y)));
f.pb(k);
return k;
}
void clear(){
while (!hist.empty()){
hist.back().ff = hist.back().ss;
hist.pop_back();
}
f.clear();
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, M, q; cin>>n>>M>>q;
vector<pii> ed = {{0, 0}};
for (int i = 1; i <= M; i++){
int u, v; cin>>u>>v;
ed.pb({u, v});
}
dsu T(n);
for (int i = 1; i <= M; i++){
T.add(ed[i]);
}
if (!T.f.back()){
for (int i = 1; i <= q; i++){
cout<<"NO"<<"\n";
}
return 0;
}
T.clear();
vector<int> last(M + 1);
function<void(int, int, pii)> solve = [&](int l, int r, pii R){
if (l > r) return;
vector<int> x1(2 * n + 1);
for (int i = 1; i <= 2 * n; i++){
x1[i] = T.get(i);
}
int m = (l + r) / 2;
int S = (int) T.hist.size(), Sf = (int) T.f.size();
for (int i = l; i < m; i++){
T.add(ed[i]);
}
int i = R.ss;
while (T.f.empty() || !T.f.back()){
T.add(ed[i--]);
}
last[m] = i;
while (T.hist.size() != S){
T.hist.back().ff = T.hist.back().ss;
T.hist.pop_back();
}
while (T.f.size() != Sf){
T.f.pop_back();
}
S = (int) T.hist.size(); Sf = (int) T.f.size();
for (int i = last[m] + 1; i <= R.ss; i++){
T.add(ed[i]);
}
solve(l, m - 1, {R.ff, last[m]});
while (T.hist.size() != S){
T.hist.back().ff = T.hist.back().ss;
T.hist.pop_back();
}
while (T.f.size() != Sf){
T.f.pop_back();
}
S = (int) T.hist.size(); Sf = (int) T.f.size();
for (int i = l; i <= m; i++){
T.add(ed[i]);
}
solve(m + 1, r, {last[m], R.ss});
while (T.hist.size() != S){
T.hist.back().ff = T.hist.back().ss;
T.hist.pop_back();
}
while (T.f.size() != Sf){
T.f.pop_back();
}
};
solve(1, M, {0, M});
while (q--){
int l, r; cin>>l>>r;
cout<<((r <= last[l]) ? "YES" : "NO")<<"\n";
}
}
Compilation message
Joker.cpp: In lambda function:
Joker.cpp:102:30: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int&, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
102 | while (T.hist.size() != S){
| ~~~~~~~~~~~~~~^~~~
Joker.cpp:106:27: warning: comparison of integer expressions of different signedness: 'std::vector<bool>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
106 | while (T.f.size() != Sf){
| ~~~~~~~~~~~^~~~~
Joker.cpp:115:30: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int&, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
115 | while (T.hist.size() != S){
| ~~~~~~~~~~~~~~^~~~
Joker.cpp:119:27: warning: comparison of integer expressions of different signedness: 'std::vector<bool>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
119 | while (T.f.size() != Sf){
| ~~~~~~~~~~~^~~~~
Joker.cpp:128:30: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int&, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
128 | while (T.hist.size() != S){
| ~~~~~~~~~~~~~~^~~~
Joker.cpp:132:27: warning: comparison of integer expressions of different signedness: 'std::vector<bool>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
132 | while (T.f.size() != Sf){
| ~~~~~~~~~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 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 |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
42 ms |
608 KB |
Output is correct |
31 |
Correct |
34 ms |
724 KB |
Output is correct |
32 |
Correct |
17 ms |
600 KB |
Output is correct |
33 |
Correct |
11 ms |
344 KB |
Output is correct |
34 |
Correct |
39 ms |
808 KB |
Output is correct |
35 |
Correct |
51 ms |
808 KB |
Output is correct |
36 |
Correct |
10 ms |
604 KB |
Output is correct |
37 |
Correct |
26 ms |
828 KB |
Output is correct |
38 |
Correct |
2 ms |
604 KB |
Output is correct |
39 |
Correct |
22 ms |
604 KB |
Output is correct |
40 |
Correct |
15 ms |
656 KB |
Output is correct |
41 |
Correct |
16 ms |
600 KB |
Output is correct |
42 |
Correct |
14 ms |
604 KB |
Output is correct |
43 |
Correct |
10 ms |
588 KB |
Output is correct |
44 |
Correct |
18 ms |
600 KB |
Output is correct |
45 |
Correct |
29 ms |
1008 KB |
Output is correct |
46 |
Correct |
48 ms |
816 KB |
Output is correct |
47 |
Correct |
11 ms |
600 KB |
Output is correct |
48 |
Correct |
14 ms |
600 KB |
Output is correct |
49 |
Correct |
26 ms |
600 KB |
Output is correct |
50 |
Correct |
34 ms |
808 KB |
Output is correct |
51 |
Correct |
12 ms |
604 KB |
Output is correct |
52 |
Correct |
18 ms |
604 KB |
Output is correct |
53 |
Correct |
28 ms |
600 KB |
Output is correct |
54 |
Correct |
48 ms |
792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Execution timed out |
2041 ms |
26144 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 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 |
2041 ms |
26144 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 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 |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
42 ms |
608 KB |
Output is correct |
31 |
Correct |
34 ms |
724 KB |
Output is correct |
32 |
Correct |
17 ms |
600 KB |
Output is correct |
33 |
Correct |
11 ms |
344 KB |
Output is correct |
34 |
Correct |
39 ms |
808 KB |
Output is correct |
35 |
Correct |
51 ms |
808 KB |
Output is correct |
36 |
Correct |
10 ms |
604 KB |
Output is correct |
37 |
Correct |
26 ms |
828 KB |
Output is correct |
38 |
Correct |
2 ms |
604 KB |
Output is correct |
39 |
Correct |
22 ms |
604 KB |
Output is correct |
40 |
Correct |
15 ms |
656 KB |
Output is correct |
41 |
Correct |
16 ms |
600 KB |
Output is correct |
42 |
Correct |
14 ms |
604 KB |
Output is correct |
43 |
Correct |
10 ms |
588 KB |
Output is correct |
44 |
Correct |
18 ms |
600 KB |
Output is correct |
45 |
Correct |
29 ms |
1008 KB |
Output is correct |
46 |
Correct |
48 ms |
816 KB |
Output is correct |
47 |
Correct |
11 ms |
600 KB |
Output is correct |
48 |
Correct |
14 ms |
600 KB |
Output is correct |
49 |
Correct |
26 ms |
600 KB |
Output is correct |
50 |
Correct |
34 ms |
808 KB |
Output is correct |
51 |
Correct |
12 ms |
604 KB |
Output is correct |
52 |
Correct |
18 ms |
604 KB |
Output is correct |
53 |
Correct |
28 ms |
600 KB |
Output is correct |
54 |
Correct |
48 ms |
792 KB |
Output is correct |
55 |
Execution timed out |
2063 ms |
25144 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 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 |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
42 ms |
608 KB |
Output is correct |
31 |
Correct |
34 ms |
724 KB |
Output is correct |
32 |
Correct |
17 ms |
600 KB |
Output is correct |
33 |
Correct |
11 ms |
344 KB |
Output is correct |
34 |
Correct |
39 ms |
808 KB |
Output is correct |
35 |
Correct |
51 ms |
808 KB |
Output is correct |
36 |
Correct |
10 ms |
604 KB |
Output is correct |
37 |
Correct |
26 ms |
828 KB |
Output is correct |
38 |
Correct |
2 ms |
604 KB |
Output is correct |
39 |
Correct |
22 ms |
604 KB |
Output is correct |
40 |
Correct |
15 ms |
656 KB |
Output is correct |
41 |
Correct |
16 ms |
600 KB |
Output is correct |
42 |
Correct |
14 ms |
604 KB |
Output is correct |
43 |
Correct |
10 ms |
588 KB |
Output is correct |
44 |
Correct |
18 ms |
600 KB |
Output is correct |
45 |
Correct |
29 ms |
1008 KB |
Output is correct |
46 |
Correct |
48 ms |
816 KB |
Output is correct |
47 |
Correct |
11 ms |
600 KB |
Output is correct |
48 |
Correct |
14 ms |
600 KB |
Output is correct |
49 |
Correct |
26 ms |
600 KB |
Output is correct |
50 |
Correct |
34 ms |
808 KB |
Output is correct |
51 |
Correct |
12 ms |
604 KB |
Output is correct |
52 |
Correct |
18 ms |
604 KB |
Output is correct |
53 |
Correct |
28 ms |
600 KB |
Output is correct |
54 |
Correct |
48 ms |
792 KB |
Output is correct |
55 |
Execution timed out |
2041 ms |
26144 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |