#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10, LOG = 20;
int n, q;
int need[N];
int c[N];
vector<int> keys[N];
int L[N], R[N];
int prv[N], nxt[N];
int lst[N];
int st_prv[N][LOG], st_nxt[N][LOG];
int get_prv(int l, int r) {
if(l > r) return INT_MAX;
int len = r - l + 1;
int bit = 31 - __builtin_clz(len);
return min(st_prv[l][bit], st_prv[r - (1 << bit) + 1][bit]);
}
int get_nxt(int l, int r) {
if(l > r) return 0;
int len = r - l + 1;
int bit = 31 - __builtin_clz(len);
return max(st_nxt[l][bit], st_nxt[r - (1 << bit) + 1][bit]);
}
int find_left(int start, int target) {
int l = 1, r = start;
int ans = r;
while(l <= r) {
int mid = (l + r) / 2;
if(get_nxt(mid, start - 1) <= target) {
ans = mid;
r = mid - 1;
} else l = mid + 1;
}
return ans;
}
int find_right(int target, int start) {
int l = start, r = n;
int ans = l;
while(l <= r) {
int mid = (l + r) / 2;
if(get_prv(start, mid - 1) >= target) {
ans = mid;
l = mid + 1;
} else r = mid - 1;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 1; i < n; i++) cin >> need[i];
for(int i = 1; i <= n; i++) {
cin >> c[i];
keys[i].resize(c[i]);
for(int j = 0; j < c[i]; j++) cin >> keys[i][j];
}
for(int i = 1; i < n; i++) {
for(int j : keys[i]) lst[j] = i;
prv[i] = lst[need[i]];
}
for(int i = 0; i < N; i++) lst[i] = n + 1;
for(int i = n; i > 1; i--) {
for(int j : keys[i]) lst[j] = i;
nxt[i - 1] = lst[need[i - 1]];
}
for(int i = 1; i < n; i++) {
st_prv[i][0] = prv[i];
st_nxt[i][0] = nxt[i];
}
for(int j = 1; j < LOG; j++) {
for(int i = 1; i + (1 << (j - 1)) < n; i++) {
st_prv[i][j] = min(st_prv[i][j - 1], st_prv[i + (1 << (j - 1))][j - 1]);
st_nxt[i][j] = max(st_nxt[i][j - 1], st_nxt[i + (1 << (j - 1))][j - 1]);
}
}
L[1] = 1;
R[1] = find_right(1, 1);
for(int i = 2; i <= n; i++) {
L[i] = R[i] = i;
if(R[i - 1] == i - 1) {
R[i] = find_right(i, i);
while(true) {
int temp = find_left(L[i], R[i]);
if(temp == L[i]) break;
L[i] = temp;
temp = find_right(L[i], R[i]);
if(temp == R[i]) break;
R[i] = temp;
}
} else {
R[i] = find_right(i, i);
L[i] = find_left(i, R[i]);
if(L[i] < i) {
L[i] = L[i - 1];
R[i] = R[i - 1];
}
}
}
int q;
cin >> q;
while(q--) {
int l, r;
cin >> l >> r;
cout << ((L[l] <= r && r <= R[l]) ? "YES\n" : "NO\n");
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
29272 KB |
Output is correct |
2 |
Correct |
7 ms |
31572 KB |
Output is correct |
3 |
Correct |
7 ms |
31580 KB |
Output is correct |
4 |
Correct |
6 ms |
29312 KB |
Output is correct |
5 |
Correct |
6 ms |
29428 KB |
Output is correct |
6 |
Correct |
6 ms |
29276 KB |
Output is correct |
7 |
Correct |
6 ms |
29272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
29272 KB |
Output is correct |
2 |
Correct |
7 ms |
31572 KB |
Output is correct |
3 |
Correct |
7 ms |
31580 KB |
Output is correct |
4 |
Correct |
6 ms |
29312 KB |
Output is correct |
5 |
Correct |
6 ms |
29428 KB |
Output is correct |
6 |
Correct |
6 ms |
29276 KB |
Output is correct |
7 |
Correct |
6 ms |
29272 KB |
Output is correct |
8 |
Correct |
74 ms |
35156 KB |
Output is correct |
9 |
Correct |
75 ms |
35224 KB |
Output is correct |
10 |
Correct |
71 ms |
37504 KB |
Output is correct |
11 |
Correct |
71 ms |
37960 KB |
Output is correct |
12 |
Correct |
65 ms |
34804 KB |
Output is correct |
13 |
Correct |
68 ms |
35412 KB |
Output is correct |
14 |
Correct |
72 ms |
35484 KB |
Output is correct |
15 |
Correct |
85 ms |
35408 KB |
Output is correct |
16 |
Correct |
67 ms |
35228 KB |
Output is correct |
17 |
Correct |
77 ms |
35424 KB |
Output is correct |
18 |
Correct |
68 ms |
35316 KB |
Output is correct |
19 |
Correct |
67 ms |
35412 KB |
Output is correct |
20 |
Correct |
70 ms |
35308 KB |
Output is correct |
21 |
Correct |
85 ms |
35156 KB |
Output is correct |
22 |
Correct |
68 ms |
35180 KB |
Output is correct |
23 |
Correct |
75 ms |
35152 KB |
Output is correct |
24 |
Correct |
69 ms |
35028 KB |
Output is correct |
25 |
Correct |
75 ms |
35412 KB |
Output is correct |
26 |
Correct |
85 ms |
35260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
135 ms |
55632 KB |
Output is correct |
2 |
Correct |
130 ms |
55736 KB |
Output is correct |
3 |
Correct |
127 ms |
55380 KB |
Output is correct |
4 |
Correct |
137 ms |
55808 KB |
Output is correct |
5 |
Correct |
149 ms |
55628 KB |
Output is correct |
6 |
Correct |
122 ms |
54868 KB |
Output is correct |
7 |
Correct |
114 ms |
54864 KB |
Output is correct |
8 |
Correct |
112 ms |
54636 KB |
Output is correct |
9 |
Correct |
118 ms |
54952 KB |
Output is correct |
10 |
Correct |
130 ms |
54868 KB |
Output is correct |
11 |
Correct |
111 ms |
54612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
29272 KB |
Output is correct |
2 |
Correct |
7 ms |
31572 KB |
Output is correct |
3 |
Correct |
7 ms |
31580 KB |
Output is correct |
4 |
Correct |
6 ms |
29312 KB |
Output is correct |
5 |
Correct |
6 ms |
29428 KB |
Output is correct |
6 |
Correct |
6 ms |
29276 KB |
Output is correct |
7 |
Correct |
6 ms |
29272 KB |
Output is correct |
8 |
Correct |
74 ms |
35156 KB |
Output is correct |
9 |
Correct |
75 ms |
35224 KB |
Output is correct |
10 |
Correct |
71 ms |
37504 KB |
Output is correct |
11 |
Correct |
71 ms |
37960 KB |
Output is correct |
12 |
Correct |
65 ms |
34804 KB |
Output is correct |
13 |
Correct |
68 ms |
35412 KB |
Output is correct |
14 |
Correct |
72 ms |
35484 KB |
Output is correct |
15 |
Correct |
85 ms |
35408 KB |
Output is correct |
16 |
Correct |
67 ms |
35228 KB |
Output is correct |
17 |
Correct |
77 ms |
35424 KB |
Output is correct |
18 |
Correct |
68 ms |
35316 KB |
Output is correct |
19 |
Correct |
67 ms |
35412 KB |
Output is correct |
20 |
Correct |
70 ms |
35308 KB |
Output is correct |
21 |
Correct |
85 ms |
35156 KB |
Output is correct |
22 |
Correct |
68 ms |
35180 KB |
Output is correct |
23 |
Correct |
75 ms |
35152 KB |
Output is correct |
24 |
Correct |
69 ms |
35028 KB |
Output is correct |
25 |
Correct |
75 ms |
35412 KB |
Output is correct |
26 |
Correct |
85 ms |
35260 KB |
Output is correct |
27 |
Correct |
135 ms |
55632 KB |
Output is correct |
28 |
Correct |
130 ms |
55736 KB |
Output is correct |
29 |
Correct |
127 ms |
55380 KB |
Output is correct |
30 |
Correct |
137 ms |
55808 KB |
Output is correct |
31 |
Correct |
149 ms |
55628 KB |
Output is correct |
32 |
Correct |
122 ms |
54868 KB |
Output is correct |
33 |
Correct |
114 ms |
54864 KB |
Output is correct |
34 |
Correct |
112 ms |
54636 KB |
Output is correct |
35 |
Correct |
118 ms |
54952 KB |
Output is correct |
36 |
Correct |
130 ms |
54868 KB |
Output is correct |
37 |
Correct |
111 ms |
54612 KB |
Output is correct |
38 |
Correct |
341 ms |
119084 KB |
Output is correct |
39 |
Correct |
365 ms |
132656 KB |
Output is correct |
40 |
Correct |
256 ms |
98064 KB |
Output is correct |
41 |
Correct |
410 ms |
130936 KB |
Output is correct |
42 |
Correct |
120 ms |
55916 KB |
Output is correct |
43 |
Correct |
143 ms |
55884 KB |
Output is correct |
44 |
Correct |
204 ms |
78128 KB |
Output is correct |
45 |
Correct |
228 ms |
78256 KB |
Output is correct |
46 |
Correct |
212 ms |
78340 KB |
Output is correct |
47 |
Correct |
121 ms |
55888 KB |
Output is correct |
48 |
Correct |
125 ms |
55636 KB |
Output is correct |
49 |
Correct |
214 ms |
78208 KB |
Output is correct |
50 |
Correct |
207 ms |
78260 KB |
Output is correct |
51 |
Correct |
218 ms |
78808 KB |
Output is correct |
52 |
Correct |
193 ms |
77140 KB |
Output is correct |
53 |
Correct |
290 ms |
98616 KB |
Output is correct |
54 |
Correct |
1442 ms |
120256 KB |
Output is correct |
55 |
Correct |
291 ms |
98604 KB |
Output is correct |