#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
const int SZ = 1 << 19;
int C[SZ], L[SZ], R[SZ];
pii E[SZ];
vector<int> K[SZ];
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int N, Q;
cin >> N;
for (int i = 1; i < N; ++i) cin >> C[i];
for (int i = 1; i <= N; ++i) {
int M;
cin >> M;
K[i].resize(M);
for (int& n : K[i]) cin >> n;
}
vector<int> in(N + 1);
for (int i = 1; i < N; ++i) {
for (int k : K[i]) in[k] = i;
L[i] = in[C[i]];
}
fill(in.begin(), in.end(), N + 1);
for (int i = N - 1; i > 0; --i) {
for (int k : K[i + 1]) in[k] = i + 1;
R[i] = in[C[i]];
}
R[0] = R[N] = N + 1, L[0] = L[N] = 0;
// for (int i = 0; i <= N; ++i) {
// cout << "Door: " << L[i] << ' ' << R[i] << '\n';
// }
// cout << '\n';
fill(E, E + SZ, pii{1, -N});
for (int i = 1; i <= N; ++i) {
vector<int> go;
for (int j = L[i]; j < i; ++j) {
if (i < R[j]) go.push_back(j);
}
if (go.empty()) continue;
int a = go.back(); go.pop_back();
for (int p = a + 1; p <= i; ++p) E[p] = max(E[p], pii{a + 1, -i});
if (go.empty()) continue;
a = go.back(); go.pop_back();
for (int p = a + 1; p <= i; ++p) E[p] = max(E[p], pii{a + 1, -i});
}
for (int i = 0; i < N; ++i) {
vector<int> go;
for (int j = R[i] - 1; j > i; --j) {
if (L[j] <= i) go.push_back(j);
}
if (go.empty()) continue;
int a = go.back(); go.pop_back();
for (int p = i + 1; p <= a; ++p) E[p] = max(E[p], pii{i + 1, -a});
if (go.empty()) continue;
a = go.back(); go.pop_back();
for (int p = i + 1; p <= a; ++p) E[p] = max(E[p], pii{i + 1, -a});
}
// cout << '\n';
// for (int i = 1; i <= N; ++i) {
// cout << "Bottleneck: " << E[i].first << ' ' << -E[i].second << '\n';
// }
cin >> Q;
while (Q--) {
int x, y;
cin >> x >> y;
bool can = E[x].first <= y && y <= -E[x].second;
cout << (can ? "YES" : "NO") << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
21080 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
21080 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
135 ms |
33736 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
21080 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |