#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]];
}
int cnt = 0;
for (int i = 1; i <= N; i++) {
int l = i;
int r = i;
while (true) {
cnt++;
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;
}
if(cnt >= ((int)1e7)) {
cout << "-1\n";
exit(0);
}
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 |
10 ms |
12416 KB |
Output is correct |
2 |
Correct |
10 ms |
12544 KB |
Output is correct |
3 |
Correct |
10 ms |
12672 KB |
Output is correct |
4 |
Correct |
10 ms |
12544 KB |
Output is correct |
5 |
Correct |
10 ms |
12544 KB |
Output is correct |
6 |
Correct |
10 ms |
12544 KB |
Output is correct |
7 |
Correct |
9 ms |
12544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
12416 KB |
Output is correct |
2 |
Correct |
10 ms |
12544 KB |
Output is correct |
3 |
Correct |
10 ms |
12672 KB |
Output is correct |
4 |
Correct |
10 ms |
12544 KB |
Output is correct |
5 |
Correct |
10 ms |
12544 KB |
Output is correct |
6 |
Correct |
10 ms |
12544 KB |
Output is correct |
7 |
Correct |
9 ms |
12544 KB |
Output is correct |
8 |
Correct |
126 ms |
14072 KB |
Output is correct |
9 |
Correct |
129 ms |
13944 KB |
Output is correct |
10 |
Correct |
129 ms |
14144 KB |
Output is correct |
11 |
Correct |
126 ms |
14456 KB |
Output is correct |
12 |
Correct |
123 ms |
14328 KB |
Output is correct |
13 |
Correct |
125 ms |
14200 KB |
Output is correct |
14 |
Correct |
123 ms |
14192 KB |
Output is correct |
15 |
Correct |
122 ms |
14268 KB |
Output is correct |
16 |
Correct |
119 ms |
14456 KB |
Output is correct |
17 |
Correct |
125 ms |
14200 KB |
Output is correct |
18 |
Correct |
125 ms |
14200 KB |
Output is correct |
19 |
Correct |
125 ms |
14328 KB |
Output is correct |
20 |
Correct |
122 ms |
14336 KB |
Output is correct |
21 |
Correct |
122 ms |
14456 KB |
Output is correct |
22 |
Correct |
121 ms |
14328 KB |
Output is correct |
23 |
Correct |
127 ms |
14076 KB |
Output is correct |
24 |
Correct |
126 ms |
14072 KB |
Output is correct |
25 |
Correct |
127 ms |
14072 KB |
Output is correct |
26 |
Correct |
125 ms |
14072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
199 ms |
19592 KB |
Output is correct |
2 |
Correct |
192 ms |
19728 KB |
Output is correct |
3 |
Correct |
185 ms |
19648 KB |
Output is correct |
4 |
Correct |
191 ms |
19704 KB |
Output is correct |
5 |
Correct |
194 ms |
19704 KB |
Output is correct |
6 |
Correct |
173 ms |
19696 KB |
Output is correct |
7 |
Correct |
171 ms |
19832 KB |
Output is correct |
8 |
Correct |
171 ms |
19832 KB |
Output is correct |
9 |
Correct |
183 ms |
19880 KB |
Output is correct |
10 |
Correct |
168 ms |
19832 KB |
Output is correct |
11 |
Correct |
173 ms |
19960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
12416 KB |
Output is correct |
2 |
Correct |
10 ms |
12544 KB |
Output is correct |
3 |
Correct |
10 ms |
12672 KB |
Output is correct |
4 |
Correct |
10 ms |
12544 KB |
Output is correct |
5 |
Correct |
10 ms |
12544 KB |
Output is correct |
6 |
Correct |
10 ms |
12544 KB |
Output is correct |
7 |
Correct |
9 ms |
12544 KB |
Output is correct |
8 |
Correct |
126 ms |
14072 KB |
Output is correct |
9 |
Correct |
129 ms |
13944 KB |
Output is correct |
10 |
Correct |
129 ms |
14144 KB |
Output is correct |
11 |
Correct |
126 ms |
14456 KB |
Output is correct |
12 |
Correct |
123 ms |
14328 KB |
Output is correct |
13 |
Correct |
125 ms |
14200 KB |
Output is correct |
14 |
Correct |
123 ms |
14192 KB |
Output is correct |
15 |
Correct |
122 ms |
14268 KB |
Output is correct |
16 |
Correct |
119 ms |
14456 KB |
Output is correct |
17 |
Correct |
125 ms |
14200 KB |
Output is correct |
18 |
Correct |
125 ms |
14200 KB |
Output is correct |
19 |
Correct |
125 ms |
14328 KB |
Output is correct |
20 |
Correct |
122 ms |
14336 KB |
Output is correct |
21 |
Correct |
122 ms |
14456 KB |
Output is correct |
22 |
Correct |
121 ms |
14328 KB |
Output is correct |
23 |
Correct |
127 ms |
14076 KB |
Output is correct |
24 |
Correct |
126 ms |
14072 KB |
Output is correct |
25 |
Correct |
127 ms |
14072 KB |
Output is correct |
26 |
Correct |
125 ms |
14072 KB |
Output is correct |
27 |
Correct |
199 ms |
19592 KB |
Output is correct |
28 |
Correct |
192 ms |
19728 KB |
Output is correct |
29 |
Correct |
185 ms |
19648 KB |
Output is correct |
30 |
Correct |
191 ms |
19704 KB |
Output is correct |
31 |
Correct |
194 ms |
19704 KB |
Output is correct |
32 |
Correct |
173 ms |
19696 KB |
Output is correct |
33 |
Correct |
171 ms |
19832 KB |
Output is correct |
34 |
Correct |
171 ms |
19832 KB |
Output is correct |
35 |
Correct |
183 ms |
19880 KB |
Output is correct |
36 |
Correct |
168 ms |
19832 KB |
Output is correct |
37 |
Correct |
173 ms |
19960 KB |
Output is correct |
38 |
Correct |
295 ms |
35836 KB |
Output is correct |
39 |
Correct |
317 ms |
41208 KB |
Output is correct |
40 |
Correct |
267 ms |
30328 KB |
Output is correct |
41 |
Correct |
305 ms |
41720 KB |
Output is correct |
42 |
Correct |
173 ms |
19576 KB |
Output is correct |
43 |
Correct |
174 ms |
19680 KB |
Output is correct |
44 |
Correct |
227 ms |
25048 KB |
Output is correct |
45 |
Correct |
231 ms |
25080 KB |
Output is correct |
46 |
Correct |
231 ms |
25208 KB |
Output is correct |
47 |
Correct |
173 ms |
19704 KB |
Output is correct |
48 |
Correct |
174 ms |
19704 KB |
Output is correct |
49 |
Correct |
233 ms |
25080 KB |
Output is correct |
50 |
Correct |
235 ms |
25208 KB |
Output is correct |
51 |
Correct |
232 ms |
25080 KB |
Output is correct |
52 |
Correct |
219 ms |
24824 KB |
Output is correct |
53 |
Correct |
268 ms |
30360 KB |
Output is correct |
54 |
Correct |
301 ms |
35832 KB |
Output is correct |
55 |
Correct |
261 ms |
30456 KB |
Output is correct |