/**
* 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])) {
L[i] = L[L[i] - 1];
R[i] = max(R[i], Query(1, 0, n - 1, L[i], i));
} 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
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 |
1 |
Correct |
3 ms |
860 KB |
Output is correct |
2 |
Correct |
4 ms |
860 KB |
Output is correct |
3 |
Correct |
11 ms |
1168 KB |
Output is correct |
4 |
Correct |
3 ms |
860 KB |
Output is correct |
5 |
Correct |
9 ms |
852 KB |
Output is correct |
6 |
Correct |
4 ms |
868 KB |
Output is correct |
7 |
Correct |
3 ms |
720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
860 KB |
Output is correct |
2 |
Correct |
4 ms |
860 KB |
Output is correct |
3 |
Correct |
11 ms |
1168 KB |
Output is correct |
4 |
Correct |
3 ms |
860 KB |
Output is correct |
5 |
Correct |
9 ms |
852 KB |
Output is correct |
6 |
Correct |
4 ms |
868 KB |
Output is correct |
7 |
Correct |
3 ms |
720 KB |
Output is correct |
8 |
Correct |
76 ms |
4692 KB |
Output is correct |
9 |
Correct |
78 ms |
4700 KB |
Output is correct |
10 |
Correct |
80 ms |
4980 KB |
Output is correct |
11 |
Correct |
78 ms |
5204 KB |
Output is correct |
12 |
Correct |
66 ms |
4436 KB |
Output is correct |
13 |
Correct |
74 ms |
4888 KB |
Output is correct |
14 |
Correct |
72 ms |
4944 KB |
Output is correct |
15 |
Correct |
73 ms |
4820 KB |
Output is correct |
16 |
Correct |
75 ms |
5024 KB |
Output is correct |
17 |
Correct |
69 ms |
4908 KB |
Output is correct |
18 |
Correct |
77 ms |
4948 KB |
Output is correct |
19 |
Correct |
71 ms |
4948 KB |
Output is correct |
20 |
Correct |
83 ms |
5204 KB |
Output is correct |
21 |
Correct |
75 ms |
4948 KB |
Output is correct |
22 |
Correct |
74 ms |
4688 KB |
Output is correct |
23 |
Correct |
77 ms |
4604 KB |
Output is correct |
24 |
Correct |
72 ms |
4516 KB |
Output is correct |
25 |
Correct |
78 ms |
4696 KB |
Output is correct |
26 |
Correct |
71 ms |
4460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3009 ms |
29524 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
860 KB |
Output is correct |
2 |
Correct |
4 ms |
860 KB |
Output is correct |
3 |
Correct |
11 ms |
1168 KB |
Output is correct |
4 |
Correct |
3 ms |
860 KB |
Output is correct |
5 |
Correct |
9 ms |
852 KB |
Output is correct |
6 |
Correct |
4 ms |
868 KB |
Output is correct |
7 |
Correct |
3 ms |
720 KB |
Output is correct |
8 |
Correct |
76 ms |
4692 KB |
Output is correct |
9 |
Correct |
78 ms |
4700 KB |
Output is correct |
10 |
Correct |
80 ms |
4980 KB |
Output is correct |
11 |
Correct |
78 ms |
5204 KB |
Output is correct |
12 |
Correct |
66 ms |
4436 KB |
Output is correct |
13 |
Correct |
74 ms |
4888 KB |
Output is correct |
14 |
Correct |
72 ms |
4944 KB |
Output is correct |
15 |
Correct |
73 ms |
4820 KB |
Output is correct |
16 |
Correct |
75 ms |
5024 KB |
Output is correct |
17 |
Correct |
69 ms |
4908 KB |
Output is correct |
18 |
Correct |
77 ms |
4948 KB |
Output is correct |
19 |
Correct |
71 ms |
4948 KB |
Output is correct |
20 |
Correct |
83 ms |
5204 KB |
Output is correct |
21 |
Correct |
75 ms |
4948 KB |
Output is correct |
22 |
Correct |
74 ms |
4688 KB |
Output is correct |
23 |
Correct |
77 ms |
4604 KB |
Output is correct |
24 |
Correct |
72 ms |
4516 KB |
Output is correct |
25 |
Correct |
78 ms |
4696 KB |
Output is correct |
26 |
Correct |
71 ms |
4460 KB |
Output is correct |
27 |
Execution timed out |
3009 ms |
29524 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |