#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 |
604 KB |
Output is correct |
2 |
Correct |
4 ms |
860 KB |
Output is correct |
3 |
Correct |
5 ms |
1028 KB |
Output is correct |
4 |
Correct |
3 ms |
604 KB |
Output is correct |
5 |
Correct |
3 ms |
604 KB |
Output is correct |
6 |
Correct |
4 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
604 KB |
Output is correct |
2 |
Correct |
4 ms |
860 KB |
Output is correct |
3 |
Correct |
5 ms |
1028 KB |
Output is correct |
4 |
Correct |
3 ms |
604 KB |
Output is correct |
5 |
Correct |
3 ms |
604 KB |
Output is correct |
6 |
Correct |
4 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
79 ms |
2144 KB |
Output is correct |
9 |
Correct |
79 ms |
2112 KB |
Output is correct |
10 |
Correct |
79 ms |
2524 KB |
Output is correct |
11 |
Correct |
81 ms |
2696 KB |
Output is correct |
12 |
Correct |
91 ms |
2308 KB |
Output is correct |
13 |
Correct |
76 ms |
2388 KB |
Output is correct |
14 |
Correct |
82 ms |
2524 KB |
Output is correct |
15 |
Correct |
77 ms |
2544 KB |
Output is correct |
16 |
Correct |
77 ms |
2720 KB |
Output is correct |
17 |
Correct |
76 ms |
2388 KB |
Output is correct |
18 |
Correct |
78 ms |
2384 KB |
Output is correct |
19 |
Correct |
78 ms |
2428 KB |
Output is correct |
20 |
Correct |
76 ms |
2588 KB |
Output is correct |
21 |
Correct |
82 ms |
2636 KB |
Output is correct |
22 |
Correct |
76 ms |
2416 KB |
Output is correct |
23 |
Correct |
77 ms |
2132 KB |
Output is correct |
24 |
Correct |
77 ms |
2132 KB |
Output is correct |
25 |
Correct |
77 ms |
2292 KB |
Output is correct |
26 |
Correct |
76 ms |
2204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
255 ms |
24624 KB |
Output is correct |
2 |
Correct |
279 ms |
25760 KB |
Output is correct |
3 |
Correct |
255 ms |
25036 KB |
Output is correct |
4 |
Correct |
249 ms |
24704 KB |
Output is correct |
5 |
Correct |
254 ms |
24460 KB |
Output is correct |
6 |
Correct |
215 ms |
18800 KB |
Output is correct |
7 |
Correct |
217 ms |
15096 KB |
Output is correct |
8 |
Correct |
228 ms |
19036 KB |
Output is correct |
9 |
Correct |
217 ms |
15284 KB |
Output is correct |
10 |
Correct |
212 ms |
15128 KB |
Output is correct |
11 |
Correct |
215 ms |
18772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
604 KB |
Output is correct |
2 |
Correct |
4 ms |
860 KB |
Output is correct |
3 |
Correct |
5 ms |
1028 KB |
Output is correct |
4 |
Correct |
3 ms |
604 KB |
Output is correct |
5 |
Correct |
3 ms |
604 KB |
Output is correct |
6 |
Correct |
4 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
79 ms |
2144 KB |
Output is correct |
9 |
Correct |
79 ms |
2112 KB |
Output is correct |
10 |
Correct |
79 ms |
2524 KB |
Output is correct |
11 |
Correct |
81 ms |
2696 KB |
Output is correct |
12 |
Correct |
91 ms |
2308 KB |
Output is correct |
13 |
Correct |
76 ms |
2388 KB |
Output is correct |
14 |
Correct |
82 ms |
2524 KB |
Output is correct |
15 |
Correct |
77 ms |
2544 KB |
Output is correct |
16 |
Correct |
77 ms |
2720 KB |
Output is correct |
17 |
Correct |
76 ms |
2388 KB |
Output is correct |
18 |
Correct |
78 ms |
2384 KB |
Output is correct |
19 |
Correct |
78 ms |
2428 KB |
Output is correct |
20 |
Correct |
76 ms |
2588 KB |
Output is correct |
21 |
Correct |
82 ms |
2636 KB |
Output is correct |
22 |
Correct |
76 ms |
2416 KB |
Output is correct |
23 |
Correct |
77 ms |
2132 KB |
Output is correct |
24 |
Correct |
77 ms |
2132 KB |
Output is correct |
25 |
Correct |
77 ms |
2292 KB |
Output is correct |
26 |
Correct |
76 ms |
2204 KB |
Output is correct |
27 |
Correct |
255 ms |
24624 KB |
Output is correct |
28 |
Correct |
279 ms |
25760 KB |
Output is correct |
29 |
Correct |
255 ms |
25036 KB |
Output is correct |
30 |
Correct |
249 ms |
24704 KB |
Output is correct |
31 |
Correct |
254 ms |
24460 KB |
Output is correct |
32 |
Correct |
215 ms |
18800 KB |
Output is correct |
33 |
Correct |
217 ms |
15096 KB |
Output is correct |
34 |
Correct |
228 ms |
19036 KB |
Output is correct |
35 |
Correct |
217 ms |
15284 KB |
Output is correct |
36 |
Correct |
212 ms |
15128 KB |
Output is correct |
37 |
Correct |
215 ms |
18772 KB |
Output is correct |
38 |
Correct |
270 ms |
42392 KB |
Output is correct |
39 |
Correct |
265 ms |
51524 KB |
Output is correct |
40 |
Correct |
228 ms |
32944 KB |
Output is correct |
41 |
Correct |
643 ms |
66560 KB |
Output is correct |
42 |
Correct |
154 ms |
13140 KB |
Output is correct |
43 |
Correct |
160 ms |
12724 KB |
Output is correct |
44 |
Correct |
271 ms |
24364 KB |
Output is correct |
45 |
Correct |
270 ms |
24404 KB |
Output is correct |
46 |
Correct |
308 ms |
24916 KB |
Output is correct |
47 |
Correct |
212 ms |
14644 KB |
Output is correct |
48 |
Correct |
200 ms |
14956 KB |
Output is correct |
49 |
Correct |
353 ms |
24004 KB |
Output is correct |
50 |
Correct |
367 ms |
26420 KB |
Output is correct |
51 |
Correct |
373 ms |
27416 KB |
Output is correct |
52 |
Correct |
205 ms |
31464 KB |
Output is correct |
53 |
Correct |
274 ms |
44044 KB |
Output is correct |
54 |
Correct |
267 ms |
55496 KB |
Output is correct |
55 |
Correct |
242 ms |
44152 KB |
Output is correct |