#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5.1e5;
int N;
int C[MAXN];
vector<int> A[MAXN];
int last[MAXN];
int prv[MAXN]; // previous position with corresponding key
int nxt[MAXN]; // next
int lo[MAXN];
int hi[MAXN];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> N;
for (int i = 1; i <= N-1; i++) {
cin >> C[i];
}
for (int i = 1; i <= N; i++) {
int B; cin >> B;
A[i].reserve(B);
for (int j = 0; j < B; j++) {
int v; cin >> v;
A[i].push_back(v);
}
}
for (int i = 1; i <= N-1; i++) {
for (int v : A[i]) {
last[v] = i;
}
prv[i] = last[C[i]];
}
fill(last + 1, last + N + 1, N+1);
for (int i = N; i >= 2; i--) {
for (int v : A[i]) {
last[v] = i;
}
nxt[i-1] = last[C[i-1]];
}
for (int i = 1; i <= N; i++) {
int l = i;
int r = i;
while (true) {
if (l > 1 && nxt[l-1] <= r) {
r = max(r, hi[l-1]);
l = lo[l-1];
continue;
}
if (r+1 <= N && prv[r] >= l) {
r++;
continue;
}
break;
}
lo[i] = l;
hi[i] = r;
}
int Q; cin >> Q;
while (Q--) {
int x, y; cin >> x >> y;
if (lo[x] <= y && y <= hi[x]) {
cout << "YES" << '\n';
} else {
cout << "NO" << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
12536 KB |
Output is correct |
2 |
Correct |
15 ms |
12536 KB |
Output is correct |
3 |
Correct |
15 ms |
12664 KB |
Output is correct |
4 |
Correct |
15 ms |
12536 KB |
Output is correct |
5 |
Correct |
15 ms |
12536 KB |
Output is correct |
6 |
Correct |
15 ms |
12536 KB |
Output is correct |
7 |
Correct |
14 ms |
12536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
12536 KB |
Output is correct |
2 |
Correct |
15 ms |
12536 KB |
Output is correct |
3 |
Correct |
15 ms |
12664 KB |
Output is correct |
4 |
Correct |
15 ms |
12536 KB |
Output is correct |
5 |
Correct |
15 ms |
12536 KB |
Output is correct |
6 |
Correct |
15 ms |
12536 KB |
Output is correct |
7 |
Correct |
14 ms |
12536 KB |
Output is correct |
8 |
Correct |
147 ms |
17656 KB |
Output is correct |
9 |
Correct |
146 ms |
17528 KB |
Output is correct |
10 |
Correct |
146 ms |
17772 KB |
Output is correct |
11 |
Correct |
144 ms |
17952 KB |
Output is correct |
12 |
Correct |
131 ms |
17760 KB |
Output is correct |
13 |
Correct |
141 ms |
17784 KB |
Output is correct |
14 |
Correct |
141 ms |
17784 KB |
Output is correct |
15 |
Correct |
140 ms |
17912 KB |
Output is correct |
16 |
Correct |
138 ms |
18040 KB |
Output is correct |
17 |
Correct |
140 ms |
17768 KB |
Output is correct |
18 |
Correct |
143 ms |
17784 KB |
Output is correct |
19 |
Correct |
138 ms |
17784 KB |
Output is correct |
20 |
Correct |
138 ms |
18128 KB |
Output is correct |
21 |
Correct |
140 ms |
18040 KB |
Output is correct |
22 |
Correct |
140 ms |
17884 KB |
Output is correct |
23 |
Correct |
142 ms |
17724 KB |
Output is correct |
24 |
Correct |
138 ms |
17656 KB |
Output is correct |
25 |
Correct |
139 ms |
17788 KB |
Output is correct |
26 |
Correct |
137 ms |
17656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
223 ms |
24888 KB |
Output is correct |
2 |
Correct |
227 ms |
24844 KB |
Output is correct |
3 |
Correct |
214 ms |
24572 KB |
Output is correct |
4 |
Correct |
223 ms |
25080 KB |
Output is correct |
5 |
Correct |
225 ms |
24696 KB |
Output is correct |
6 |
Correct |
201 ms |
23348 KB |
Output is correct |
7 |
Correct |
188 ms |
23032 KB |
Output is correct |
8 |
Correct |
193 ms |
23004 KB |
Output is correct |
9 |
Correct |
190 ms |
22972 KB |
Output is correct |
10 |
Correct |
185 ms |
22904 KB |
Output is correct |
11 |
Correct |
191 ms |
22980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
12536 KB |
Output is correct |
2 |
Correct |
15 ms |
12536 KB |
Output is correct |
3 |
Correct |
15 ms |
12664 KB |
Output is correct |
4 |
Correct |
15 ms |
12536 KB |
Output is correct |
5 |
Correct |
15 ms |
12536 KB |
Output is correct |
6 |
Correct |
15 ms |
12536 KB |
Output is correct |
7 |
Correct |
14 ms |
12536 KB |
Output is correct |
8 |
Correct |
147 ms |
17656 KB |
Output is correct |
9 |
Correct |
146 ms |
17528 KB |
Output is correct |
10 |
Correct |
146 ms |
17772 KB |
Output is correct |
11 |
Correct |
144 ms |
17952 KB |
Output is correct |
12 |
Correct |
131 ms |
17760 KB |
Output is correct |
13 |
Correct |
141 ms |
17784 KB |
Output is correct |
14 |
Correct |
141 ms |
17784 KB |
Output is correct |
15 |
Correct |
140 ms |
17912 KB |
Output is correct |
16 |
Correct |
138 ms |
18040 KB |
Output is correct |
17 |
Correct |
140 ms |
17768 KB |
Output is correct |
18 |
Correct |
143 ms |
17784 KB |
Output is correct |
19 |
Correct |
138 ms |
17784 KB |
Output is correct |
20 |
Correct |
138 ms |
18128 KB |
Output is correct |
21 |
Correct |
140 ms |
18040 KB |
Output is correct |
22 |
Correct |
140 ms |
17884 KB |
Output is correct |
23 |
Correct |
142 ms |
17724 KB |
Output is correct |
24 |
Correct |
138 ms |
17656 KB |
Output is correct |
25 |
Correct |
139 ms |
17788 KB |
Output is correct |
26 |
Correct |
137 ms |
17656 KB |
Output is correct |
27 |
Correct |
223 ms |
24888 KB |
Output is correct |
28 |
Correct |
227 ms |
24844 KB |
Output is correct |
29 |
Correct |
214 ms |
24572 KB |
Output is correct |
30 |
Correct |
223 ms |
25080 KB |
Output is correct |
31 |
Correct |
225 ms |
24696 KB |
Output is correct |
32 |
Correct |
201 ms |
23348 KB |
Output is correct |
33 |
Correct |
188 ms |
23032 KB |
Output is correct |
34 |
Correct |
193 ms |
23004 KB |
Output is correct |
35 |
Correct |
190 ms |
22972 KB |
Output is correct |
36 |
Correct |
185 ms |
22904 KB |
Output is correct |
37 |
Correct |
191 ms |
22980 KB |
Output is correct |
38 |
Correct |
335 ms |
41204 KB |
Output is correct |
39 |
Correct |
346 ms |
46844 KB |
Output is correct |
40 |
Correct |
308 ms |
35344 KB |
Output is correct |
41 |
Correct |
340 ms |
46548 KB |
Output is correct |
42 |
Correct |
201 ms |
25080 KB |
Output is correct |
43 |
Correct |
202 ms |
25148 KB |
Output is correct |
44 |
Correct |
267 ms |
30072 KB |
Output is correct |
45 |
Correct |
270 ms |
30072 KB |
Output is correct |
46 |
Correct |
277 ms |
29912 KB |
Output is correct |
47 |
Correct |
206 ms |
25392 KB |
Output is correct |
48 |
Correct |
202 ms |
25464 KB |
Output is correct |
49 |
Correct |
266 ms |
30200 KB |
Output is correct |
50 |
Correct |
272 ms |
30584 KB |
Output is correct |
51 |
Correct |
284 ms |
30260 KB |
Output is correct |
52 |
Correct |
252 ms |
30508 KB |
Output is correct |
53 |
Correct |
296 ms |
35704 KB |
Output is correct |
54 |
Correct |
331 ms |
40696 KB |
Output is correct |
55 |
Correct |
309 ms |
35168 KB |
Output is correct |