//Challenge: Accepted
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 500005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
vector<int> pos[maxn];
int c[maxn];
pii ans[maxn];
pii merge(pii x, pii y){return {min(x.ff, y.ff), max(x.ss, y.ss)};}
bool vis[maxn];
struct DSU{
pii ran[maxn];
int par[maxn];
void init(int n) {
for (int i = 1;i <= n;i++) par[i] = i, ran[i] = {i, i};
}
int find(int a) {
return a == par[a] ? a : (par[a] = find(par[a]));
}
pii getran(int a) {
return ran[find(a)];
}
void Union(int a, int b) {
a = find(a), b = find(b);
ran[b] = merge(ran[b], ran[a]);
par[a] = b;
}
} d;
bool key(int x, pii p) {
int il = lower_bound(pos[x].begin(), pos[x].end(), p.ff) - pos[x].begin();
int ir = upper_bound(pos[x].begin(), pos[x].end(), p.ss) - pos[x].begin();
return ir > il;
}
int tot;
bool solve(int n, int par) {
while (true) {
n = d.find(n);
pii r = d.getran(n);
auto upd = [&] (int x) {
if (d.find(par) == d.find(x)) return 1;
if (solve(x, n)) {
d.Union(x, n);
} else {
d.ran[n] = merge(d.ran[n], d.ran[x]);
}
return 0;
};
if (r.ff > 1 && key(c[r.ff-1], r)) {
if (upd(r.ff-1)) return 1;
} else if (r.ss < tot && key(c[r.ss], r)) {
if (upd(r.ss+1)) return 1;
} else {
break;
}
}
return 0;
}
int main() {
io
int n;
cin >> n;
tot = n;
for (int i = 1;i <= n - 1;i++) cin >> c[i];
for (int i = 1;i <= n;i++) {
int bi;
cin >> bi;
for (int j = 0;j < bi;j++) {
int x;cin>> x;
pos[x].push_back(i);
}
}
d.init(n);
for (int i = 1;i <= n;i++) {
if (vis[d.find(i)]) continue;
solve(i, 0);
vis[d.find(i)] = 1;
}
for (int i = 1;i <= n;i++) {
ans[i] = d.getran(i);
}
int q;
cin >> q;
while (q--) {
int x, y;
cin >> x >> y;
cout << (y <= ans[x].ss && y >= ans[x].ff ? "YES" : "NO") << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16076 KB |
Output is correct |
2 |
Correct |
12 ms |
16112 KB |
Output is correct |
3 |
Correct |
13 ms |
16212 KB |
Output is correct |
4 |
Correct |
11 ms |
16084 KB |
Output is correct |
5 |
Correct |
12 ms |
16084 KB |
Output is correct |
6 |
Correct |
12 ms |
16176 KB |
Output is correct |
7 |
Correct |
11 ms |
16176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16076 KB |
Output is correct |
2 |
Correct |
12 ms |
16112 KB |
Output is correct |
3 |
Correct |
13 ms |
16212 KB |
Output is correct |
4 |
Correct |
11 ms |
16084 KB |
Output is correct |
5 |
Correct |
12 ms |
16084 KB |
Output is correct |
6 |
Correct |
12 ms |
16176 KB |
Output is correct |
7 |
Correct |
11 ms |
16176 KB |
Output is correct |
8 |
Correct |
121 ms |
18844 KB |
Output is correct |
9 |
Correct |
148 ms |
18940 KB |
Output is correct |
10 |
Correct |
107 ms |
19012 KB |
Output is correct |
11 |
Correct |
112 ms |
19084 KB |
Output is correct |
12 |
Correct |
114 ms |
19060 KB |
Output is correct |
13 |
Correct |
102 ms |
19012 KB |
Output is correct |
14 |
Correct |
109 ms |
19044 KB |
Output is correct |
15 |
Correct |
107 ms |
19176 KB |
Output is correct |
16 |
Correct |
102 ms |
19344 KB |
Output is correct |
17 |
Correct |
138 ms |
19068 KB |
Output is correct |
18 |
Correct |
110 ms |
19068 KB |
Output is correct |
19 |
Correct |
100 ms |
19068 KB |
Output is correct |
20 |
Correct |
117 ms |
19240 KB |
Output is correct |
21 |
Correct |
105 ms |
19240 KB |
Output is correct |
22 |
Correct |
120 ms |
19124 KB |
Output is correct |
23 |
Correct |
118 ms |
18892 KB |
Output is correct |
24 |
Correct |
110 ms |
18916 KB |
Output is correct |
25 |
Correct |
107 ms |
18892 KB |
Output is correct |
26 |
Correct |
111 ms |
18880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
218 ms |
22912 KB |
Output is correct |
2 |
Correct |
306 ms |
22500 KB |
Output is correct |
3 |
Correct |
199 ms |
22712 KB |
Output is correct |
4 |
Correct |
237 ms |
22804 KB |
Output is correct |
5 |
Correct |
246 ms |
22900 KB |
Output is correct |
6 |
Correct |
180 ms |
21912 KB |
Output is correct |
7 |
Correct |
191 ms |
22044 KB |
Output is correct |
8 |
Correct |
158 ms |
22100 KB |
Output is correct |
9 |
Correct |
182 ms |
22208 KB |
Output is correct |
10 |
Correct |
157 ms |
22148 KB |
Output is correct |
11 |
Correct |
147 ms |
22036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16076 KB |
Output is correct |
2 |
Correct |
12 ms |
16112 KB |
Output is correct |
3 |
Correct |
13 ms |
16212 KB |
Output is correct |
4 |
Correct |
11 ms |
16084 KB |
Output is correct |
5 |
Correct |
12 ms |
16084 KB |
Output is correct |
6 |
Correct |
12 ms |
16176 KB |
Output is correct |
7 |
Correct |
11 ms |
16176 KB |
Output is correct |
8 |
Correct |
121 ms |
18844 KB |
Output is correct |
9 |
Correct |
148 ms |
18940 KB |
Output is correct |
10 |
Correct |
107 ms |
19012 KB |
Output is correct |
11 |
Correct |
112 ms |
19084 KB |
Output is correct |
12 |
Correct |
114 ms |
19060 KB |
Output is correct |
13 |
Correct |
102 ms |
19012 KB |
Output is correct |
14 |
Correct |
109 ms |
19044 KB |
Output is correct |
15 |
Correct |
107 ms |
19176 KB |
Output is correct |
16 |
Correct |
102 ms |
19344 KB |
Output is correct |
17 |
Correct |
138 ms |
19068 KB |
Output is correct |
18 |
Correct |
110 ms |
19068 KB |
Output is correct |
19 |
Correct |
100 ms |
19068 KB |
Output is correct |
20 |
Correct |
117 ms |
19240 KB |
Output is correct |
21 |
Correct |
105 ms |
19240 KB |
Output is correct |
22 |
Correct |
120 ms |
19124 KB |
Output is correct |
23 |
Correct |
118 ms |
18892 KB |
Output is correct |
24 |
Correct |
110 ms |
18916 KB |
Output is correct |
25 |
Correct |
107 ms |
18892 KB |
Output is correct |
26 |
Correct |
111 ms |
18880 KB |
Output is correct |
27 |
Correct |
218 ms |
22912 KB |
Output is correct |
28 |
Correct |
306 ms |
22500 KB |
Output is correct |
29 |
Correct |
199 ms |
22712 KB |
Output is correct |
30 |
Correct |
237 ms |
22804 KB |
Output is correct |
31 |
Correct |
246 ms |
22900 KB |
Output is correct |
32 |
Correct |
180 ms |
21912 KB |
Output is correct |
33 |
Correct |
191 ms |
22044 KB |
Output is correct |
34 |
Correct |
158 ms |
22100 KB |
Output is correct |
35 |
Correct |
182 ms |
22208 KB |
Output is correct |
36 |
Correct |
157 ms |
22148 KB |
Output is correct |
37 |
Correct |
147 ms |
22036 KB |
Output is correct |
38 |
Correct |
318 ms |
28524 KB |
Output is correct |
39 |
Correct |
276 ms |
30076 KB |
Output is correct |
40 |
Correct |
241 ms |
26988 KB |
Output is correct |
41 |
Correct |
440 ms |
29516 KB |
Output is correct |
42 |
Correct |
170 ms |
22380 KB |
Output is correct |
43 |
Correct |
168 ms |
22140 KB |
Output is correct |
44 |
Correct |
270 ms |
25884 KB |
Output is correct |
45 |
Correct |
237 ms |
25800 KB |
Output is correct |
46 |
Correct |
260 ms |
25908 KB |
Output is correct |
47 |
Correct |
167 ms |
22184 KB |
Output is correct |
48 |
Correct |
190 ms |
21848 KB |
Output is correct |
49 |
Correct |
210 ms |
24956 KB |
Output is correct |
50 |
Correct |
303 ms |
25324 KB |
Output is correct |
51 |
Correct |
245 ms |
25960 KB |
Output is correct |
52 |
Correct |
237 ms |
27408 KB |
Output is correct |
53 |
Correct |
229 ms |
31848 KB |
Output is correct |
54 |
Correct |
296 ms |
36568 KB |
Output is correct |
55 |
Correct |
236 ms |
31308 KB |
Output is correct |