This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* author: wxhtzdy
* created: 16.09.2023 20:57:23
**/
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> c(n);
for (int i = 0; i + 1 < n; i++) {
cin >> c[i];
--c[i];
}
vector<set<int>> pos(n);
for (int i = 0; i < n; i++) {
int k;
cin >> k;
for (int j = 0; j < k; j++) {
int a;
cin >> a;
--a;
pos[a].insert(i);
}
}
auto Have = [&](int L, int R, int k) {
auto it = pos[k].lower_bound(L);
return (it != pos[k].end() && *it <= R);
};
vector<int> L(n), R(n);
vector<int> mx(4 * n);
function<void(int, int, int, int)> Modify = [&](int x, int l, int r, int p) {
if (l == r) {
mx[l] = R[p];
return;
}
int mid = l + r >> 1;
if (p <= mid) {
Modify(x * 2, l, mid, p);
} else {
Modify(x * 2 + 1, mid + 1, r, p);
}
mx[x] = max(mx[x * 2], mx[x * 2 + 1]);
};
function<int(int, int, int, int, int)> Query = [&](int x, int l, int r, int ll, int rr) {
if (l > r || l > rr || r < ll) {
return 0;
}
if (ll <= l && r <= rr) {
return mx[x];
}
int mid = l + r >> 1;
return max(Query(x * 2, l, mid, ll, rr), Query(x * 2 + 1, mid + 1, r, ll, rr));
};
for (int i = 0; i < n; i++) {
L[i] = R[i] = i;
while (true) {
if (L[i] > 0 && Have(L[i], R[i], c[L[i] - 1])) {
R[i] = max(R[i], R[L[i] - 1]);
L[i] = L[L[i] - 1];
} else if (R[i] + 1 < n && Have(L[i], R[i], c[R[i]])) {
R[i] += 1;
} else {
break;
}
}
Modify(1, 0, n - 1, i);
}
int q;
cin >> q;
while (q--) {
int x, y;
cin >> x >> y;
--x; --y;
cout << (L[x] <= y && y <= R[x] ? "YES" : "NO") << '\n';
}
return 0;
}
Compilation message (stderr)
long_mansion.cpp: In lambda function:
long_mansion.cpp:41:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
41 | int mid = l + r >> 1;
| ~~^~~
long_mansion.cpp: In lambda function:
long_mansion.cpp:56:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int mid = l + r >> 1;
| ~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |