#include <bits/stdc++.h>
using i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::vector<int> a(n);
std::vector<std::vector<int>> keys(n + 1), inds(n + 1);
for (int i = 1; i < n; i++) {
std::cin >> a[i];
}
for (int i = 1; i <= n; i++) {
int m;
std::cin >> m;
keys[i].resize(m);
for (int j = 0; j < m; j++) {
std::cin >> keys[i][j];
inds[keys[i][j]].push_back(i);
}
}
auto check = [&](int l, int r, int x) {
auto it = lower_bound(inds[x].begin(), inds[x].end(), l);
return it != inds[x].end() && *it <= r;
};
std::vector<int> L(n + 1), R(n + 1), cnt(n + 1, 0);
auto solve = [&](auto self, int l, int r) -> void {
if (l > r) {
return;
}
int m = (l + r) / 2;
self(self, l, m - 1);
self(self, m + 1, r);
std::vector<int> left;
for (int i = m - 1; i >= l; i--) {
if (R[i] == m - 1 && check(L[i], R[i], a[m - 1])) {
left.push_back(i);
}
}
std::vector<int> right;
for (int i = m + 1; i <= r; i++) {
if (L[i] == m + 1 && check(L[i], R[i], a[m])) {
right.push_back(i);
}
}
std::vector<int> clr;
auto add = [&](int i) {
for (auto x : keys[i]) {
cnt[x]++;
clr.push_back(x);
}
};
auto clear = [&]() {
for (auto it : clr) {
cnt[it] = 0;
}
};
int ql = m, qr = m;
auto advance = [&]() {
while (true) {
if (ql > l && cnt[a[ql - 1]] > 0) {
ql--;
add(ql);
} else if (qr < r && cnt[a[qr]] > 0) {
qr++;
add(qr);
} else {
break;
}
}
};
add(m);
advance();
L[m] = ql;
R[m] = qr;
int last = m;
for (auto pos : left) {
for (int j = last - 1; j >= L[pos]; j--) {
add(j);
}
advance();
L[pos] = ql;
R[pos] = qr;
last = L[pos];
};
clear();
ql = m;
qr = m;
add(m);
advance();
last = m;
for (auto pos : right) {
for (int j = last + 1; j <= R[pos]; j++) {
add(j);
}
advance();
L[pos] = ql;
R[pos] = qr;
last = R[pos];
}
clear();
};
solve(solve, 1, n);
int q;
std::cin >> q;
while (q--) {
int x, y;
std::cin >> x >> y;
std::cout << (y >= L[x] && y <= R[x] ? "YES" : "NO") << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
856 KB |
Output is correct |
2 |
Correct |
4 ms |
860 KB |
Output is correct |
3 |
Correct |
5 ms |
1116 KB |
Output is correct |
4 |
Correct |
3 ms |
856 KB |
Output is correct |
5 |
Correct |
3 ms |
860 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
856 KB |
Output is correct |
2 |
Correct |
4 ms |
860 KB |
Output is correct |
3 |
Correct |
5 ms |
1116 KB |
Output is correct |
4 |
Correct |
3 ms |
856 KB |
Output is correct |
5 |
Correct |
3 ms |
860 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
81 ms |
6500 KB |
Output is correct |
9 |
Correct |
80 ms |
6484 KB |
Output is correct |
10 |
Correct |
81 ms |
6968 KB |
Output is correct |
11 |
Correct |
82 ms |
7496 KB |
Output is correct |
12 |
Correct |
73 ms |
6196 KB |
Output is correct |
13 |
Correct |
89 ms |
6740 KB |
Output is correct |
14 |
Correct |
80 ms |
6740 KB |
Output is correct |
15 |
Correct |
82 ms |
6812 KB |
Output is correct |
16 |
Correct |
77 ms |
6632 KB |
Output is correct |
17 |
Correct |
81 ms |
6608 KB |
Output is correct |
18 |
Correct |
78 ms |
6700 KB |
Output is correct |
19 |
Correct |
79 ms |
6744 KB |
Output is correct |
20 |
Correct |
78 ms |
6840 KB |
Output is correct |
21 |
Correct |
76 ms |
6732 KB |
Output is correct |
22 |
Correct |
78 ms |
6740 KB |
Output is correct |
23 |
Correct |
86 ms |
6484 KB |
Output is correct |
24 |
Correct |
85 ms |
6428 KB |
Output is correct |
25 |
Correct |
80 ms |
6740 KB |
Output is correct |
26 |
Correct |
78 ms |
6440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
247 ms |
27572 KB |
Output is correct |
2 |
Correct |
260 ms |
26436 KB |
Output is correct |
3 |
Correct |
267 ms |
26112 KB |
Output is correct |
4 |
Correct |
272 ms |
26204 KB |
Output is correct |
5 |
Correct |
252 ms |
27468 KB |
Output is correct |
6 |
Correct |
220 ms |
21464 KB |
Output is correct |
7 |
Correct |
225 ms |
19300 KB |
Output is correct |
8 |
Correct |
218 ms |
23192 KB |
Output is correct |
9 |
Correct |
226 ms |
19332 KB |
Output is correct |
10 |
Correct |
215 ms |
19208 KB |
Output is correct |
11 |
Correct |
225 ms |
23208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
856 KB |
Output is correct |
2 |
Correct |
4 ms |
860 KB |
Output is correct |
3 |
Correct |
5 ms |
1116 KB |
Output is correct |
4 |
Correct |
3 ms |
856 KB |
Output is correct |
5 |
Correct |
3 ms |
860 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
81 ms |
6500 KB |
Output is correct |
9 |
Correct |
80 ms |
6484 KB |
Output is correct |
10 |
Correct |
81 ms |
6968 KB |
Output is correct |
11 |
Correct |
82 ms |
7496 KB |
Output is correct |
12 |
Correct |
73 ms |
6196 KB |
Output is correct |
13 |
Correct |
89 ms |
6740 KB |
Output is correct |
14 |
Correct |
80 ms |
6740 KB |
Output is correct |
15 |
Correct |
82 ms |
6812 KB |
Output is correct |
16 |
Correct |
77 ms |
6632 KB |
Output is correct |
17 |
Correct |
81 ms |
6608 KB |
Output is correct |
18 |
Correct |
78 ms |
6700 KB |
Output is correct |
19 |
Correct |
79 ms |
6744 KB |
Output is correct |
20 |
Correct |
78 ms |
6840 KB |
Output is correct |
21 |
Correct |
76 ms |
6732 KB |
Output is correct |
22 |
Correct |
78 ms |
6740 KB |
Output is correct |
23 |
Correct |
86 ms |
6484 KB |
Output is correct |
24 |
Correct |
85 ms |
6428 KB |
Output is correct |
25 |
Correct |
80 ms |
6740 KB |
Output is correct |
26 |
Correct |
78 ms |
6440 KB |
Output is correct |
27 |
Correct |
247 ms |
27572 KB |
Output is correct |
28 |
Correct |
260 ms |
26436 KB |
Output is correct |
29 |
Correct |
267 ms |
26112 KB |
Output is correct |
30 |
Correct |
272 ms |
26204 KB |
Output is correct |
31 |
Correct |
252 ms |
27468 KB |
Output is correct |
32 |
Correct |
220 ms |
21464 KB |
Output is correct |
33 |
Correct |
225 ms |
19300 KB |
Output is correct |
34 |
Correct |
218 ms |
23192 KB |
Output is correct |
35 |
Correct |
226 ms |
19332 KB |
Output is correct |
36 |
Correct |
215 ms |
19208 KB |
Output is correct |
37 |
Correct |
225 ms |
23208 KB |
Output is correct |
38 |
Correct |
275 ms |
54100 KB |
Output is correct |
39 |
Correct |
281 ms |
63208 KB |
Output is correct |
40 |
Correct |
258 ms |
43436 KB |
Output is correct |
41 |
Correct |
671 ms |
70036 KB |
Output is correct |
42 |
Correct |
162 ms |
20596 KB |
Output is correct |
43 |
Correct |
170 ms |
20268 KB |
Output is correct |
44 |
Correct |
264 ms |
34388 KB |
Output is correct |
45 |
Correct |
290 ms |
34684 KB |
Output is correct |
46 |
Correct |
288 ms |
35204 KB |
Output is correct |
47 |
Correct |
226 ms |
20932 KB |
Output is correct |
48 |
Correct |
206 ms |
20424 KB |
Output is correct |
49 |
Correct |
371 ms |
33740 KB |
Output is correct |
50 |
Correct |
369 ms |
34308 KB |
Output is correct |
51 |
Correct |
379 ms |
35756 KB |
Output is correct |
52 |
Correct |
190 ms |
36156 KB |
Output is correct |
53 |
Correct |
256 ms |
52136 KB |
Output is correct |
54 |
Correct |
272 ms |
64512 KB |
Output is correct |
55 |
Correct |
251 ms |
50320 KB |
Output is correct |