#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 19;
int n, q, C[N], lf_link[N], rg_link[N], lb[N], rb[N], tmin[N << 1], tmax[N << 1];
vector<int> keys[N], ls[N];
int query_min(int l, int r) {
int res = N;
for(l += N, r += N+1; l < r; l >>= 1, r >>= 1) {
if(l & 1) res = min(tmin[l++], res);
if(r & 1) res = min(tmin[--r], res);
}
return res;
}
int query_max(int l, int r) {
int res = -1;
for(l += N, r += N+1; l < r; l >>= 1, r >>= 1) {
if(l & 1) res = max(tmax[l++], res);
if(r & 1) res = max(tmax[--r], res);
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 0; i < n-1; i++) cin >> C[i], --C[i];
for(int i = 0, sz; i < n; i++) {
cin >> sz;
keys[i].resize(sz);
for(int j = 0; j < sz; j++) {
cin >> keys[i][j];
--keys[i][j];
ls[keys[i][j]].push_back(i);
}
}
rg_link[0] = n;
for(int i = 1; i < n; i++) {
auto it = lower_bound(ls[C[i-1]].begin(), ls[C[i-1]].end(), i);
if(ls[C[i-1]].empty() || it == ls[C[i-1]].end()) rg_link[i] = n;
else rg_link[i] = *it;
}
lf_link[n-1] = -1;
for(int i = 0; i < n-1; i++) {
auto it = upper_bound(ls[C[i]].begin(), ls[C[i]].end(), i);
if(ls[C[i]].empty() || it == ls[C[i]].begin()) lf_link[i] = -1;
else lf_link[i] = *prev(it);
}
/* for(int type = 0; type < n; type++) { */
/* cout << "type " << type << endl; */
/* for(int i: ls[type]) cout << i << ' '; */
/* cout << endl; */
/* } */
/* for(int i = 0; i < n; i++) cout << rg_link[i] << ' '; */
/* cout << endl; */
/* for(int i = 0; i < n; i++) cout << lf_link[i] << ' '; */
/* cout << endl; */
for(int r = 0; r < n; r++) {
int s = lf_link[r] + 1, e = r;
if(e-s+1 <= 0) {
lb[r] = n;
continue;
}
bool found = false;
for(int l = s; l <= e; l++) {
if(rg_link[l] > r) {
lb[r] = l;
found = true;
break;
}
}
if(!found) lb[r] = n;
/* while(s < e) { */
/* int mid = s+e >> 1; */
/* if(rg_link[mid] > r) e = mid; */
/* else s = mid+1; */
/* } */
/* if(rg_link[s] <= r) lb[r] = n; */
/* else lb[r] = s; */
}
for(int l = 0; l < n; l++) {
int s = l, e = rg_link[l] - 1;
if(e-s+1 <= 0) {
rb[l] = -1;
continue;
}
bool found = false;
for(int r = e; r >= s; r--) {
if(lf_link[r] < l) {
rb[l] = r;
found = true;
break;
}
}
if(!found) rb[l] = -1;
/* while(s < e) { */
/* int mid = s+e+1 >> 1; */
/* if(lf_link[mid] < l) s = mid; */
/* else e = mid-1; */
/* } */
/* if(lf_link[s] >= l) rb[l] = -1; */
/* else rb[l] = s; */
}
/* for(int i = 0; i < n; i++) cout << lb[i] << ' '; */
/* cout << endl; */
/* for(int i = 0; i < n; i++) cout << rb[i] << ' '; */
/* cout << endl; */
for(int i = 0; i < n; i++) tmin[N+i] = lb[i];
for(int i = 0; i < n; i++) tmax[N+i] = rb[i];
for(int i = N-1; i >= 1; i--) tmin[i] = min(tmin[i << 1], tmin[i << 1 | 1]);
for(int i = N-1; i >= 1; i--) tmax[i] = max(tmax[i << 1], tmax[i << 1 | 1]);
cin >> q;
while(q--) {
int x, y;
cin >> x >> y;
--x, --y;
if(x < y) {
int res = query_min(x, y-1);
cout << (res > x ? "YES\n" : "NO\n");
} else {
int res = query_max(y+1, x);
cout << (res < x ? "YES\n" : "NO\n");
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
29304 KB |
Output is correct |
2 |
Correct |
31 ms |
29624 KB |
Output is correct |
3 |
Correct |
41 ms |
29760 KB |
Output is correct |
4 |
Correct |
41 ms |
29760 KB |
Output is correct |
5 |
Correct |
32 ms |
29760 KB |
Output is correct |
6 |
Correct |
34 ms |
29900 KB |
Output is correct |
7 |
Correct |
38 ms |
29928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
29304 KB |
Output is correct |
2 |
Correct |
31 ms |
29624 KB |
Output is correct |
3 |
Correct |
41 ms |
29760 KB |
Output is correct |
4 |
Correct |
41 ms |
29760 KB |
Output is correct |
5 |
Correct |
32 ms |
29760 KB |
Output is correct |
6 |
Correct |
34 ms |
29900 KB |
Output is correct |
7 |
Correct |
38 ms |
29928 KB |
Output is correct |
8 |
Correct |
255 ms |
35960 KB |
Output is correct |
9 |
Correct |
218 ms |
40332 KB |
Output is correct |
10 |
Correct |
249 ms |
45184 KB |
Output is correct |
11 |
Correct |
209 ms |
50080 KB |
Output is correct |
12 |
Correct |
217 ms |
53608 KB |
Output is correct |
13 |
Correct |
216 ms |
57968 KB |
Output is correct |
14 |
Correct |
178 ms |
62336 KB |
Output is correct |
15 |
Correct |
170 ms |
66776 KB |
Output is correct |
16 |
Correct |
187 ms |
70764 KB |
Output is correct |
17 |
Correct |
266 ms |
75064 KB |
Output is correct |
18 |
Correct |
204 ms |
79412 KB |
Output is correct |
19 |
Correct |
194 ms |
83800 KB |
Output is correct |
20 |
Correct |
194 ms |
88100 KB |
Output is correct |
21 |
Correct |
169 ms |
92164 KB |
Output is correct |
22 |
Correct |
178 ms |
96216 KB |
Output is correct |
23 |
Correct |
169 ms |
100252 KB |
Output is correct |
24 |
Correct |
182 ms |
104592 KB |
Output is correct |
25 |
Correct |
195 ms |
109036 KB |
Output is correct |
26 |
Correct |
203 ms |
113048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
311 ms |
120924 KB |
Output is correct |
2 |
Correct |
419 ms |
127928 KB |
Output is correct |
3 |
Correct |
380 ms |
134824 KB |
Output is correct |
4 |
Correct |
385 ms |
142284 KB |
Output is correct |
5 |
Correct |
404 ms |
149708 KB |
Output is correct |
6 |
Correct |
260 ms |
155176 KB |
Output is correct |
7 |
Correct |
275 ms |
161468 KB |
Output is correct |
8 |
Correct |
332 ms |
167468 KB |
Output is correct |
9 |
Correct |
350 ms |
173616 KB |
Output is correct |
10 |
Correct |
339 ms |
179576 KB |
Output is correct |
11 |
Correct |
348 ms |
185732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
29304 KB |
Output is correct |
2 |
Correct |
31 ms |
29624 KB |
Output is correct |
3 |
Correct |
41 ms |
29760 KB |
Output is correct |
4 |
Correct |
41 ms |
29760 KB |
Output is correct |
5 |
Correct |
32 ms |
29760 KB |
Output is correct |
6 |
Correct |
34 ms |
29900 KB |
Output is correct |
7 |
Correct |
38 ms |
29928 KB |
Output is correct |
8 |
Correct |
255 ms |
35960 KB |
Output is correct |
9 |
Correct |
218 ms |
40332 KB |
Output is correct |
10 |
Correct |
249 ms |
45184 KB |
Output is correct |
11 |
Correct |
209 ms |
50080 KB |
Output is correct |
12 |
Correct |
217 ms |
53608 KB |
Output is correct |
13 |
Correct |
216 ms |
57968 KB |
Output is correct |
14 |
Correct |
178 ms |
62336 KB |
Output is correct |
15 |
Correct |
170 ms |
66776 KB |
Output is correct |
16 |
Correct |
187 ms |
70764 KB |
Output is correct |
17 |
Correct |
266 ms |
75064 KB |
Output is correct |
18 |
Correct |
204 ms |
79412 KB |
Output is correct |
19 |
Correct |
194 ms |
83800 KB |
Output is correct |
20 |
Correct |
194 ms |
88100 KB |
Output is correct |
21 |
Correct |
169 ms |
92164 KB |
Output is correct |
22 |
Correct |
178 ms |
96216 KB |
Output is correct |
23 |
Correct |
169 ms |
100252 KB |
Output is correct |
24 |
Correct |
182 ms |
104592 KB |
Output is correct |
25 |
Correct |
195 ms |
109036 KB |
Output is correct |
26 |
Correct |
203 ms |
113048 KB |
Output is correct |
27 |
Correct |
311 ms |
120924 KB |
Output is correct |
28 |
Correct |
419 ms |
127928 KB |
Output is correct |
29 |
Correct |
380 ms |
134824 KB |
Output is correct |
30 |
Correct |
385 ms |
142284 KB |
Output is correct |
31 |
Correct |
404 ms |
149708 KB |
Output is correct |
32 |
Correct |
260 ms |
155176 KB |
Output is correct |
33 |
Correct |
275 ms |
161468 KB |
Output is correct |
34 |
Correct |
332 ms |
167468 KB |
Output is correct |
35 |
Correct |
350 ms |
173616 KB |
Output is correct |
36 |
Correct |
339 ms |
179576 KB |
Output is correct |
37 |
Correct |
348 ms |
185732 KB |
Output is correct |
38 |
Correct |
709 ms |
216448 KB |
Output is correct |
39 |
Correct |
771 ms |
233568 KB |
Output is correct |
40 |
Correct |
621 ms |
233568 KB |
Output is correct |
41 |
Correct |
543 ms |
253048 KB |
Output is correct |
42 |
Correct |
388 ms |
253048 KB |
Output is correct |
43 |
Correct |
521 ms |
253048 KB |
Output is correct |
44 |
Correct |
596 ms |
262052 KB |
Output is correct |
45 |
Runtime error |
504 ms |
263168 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
46 |
Halted |
0 ms |
0 KB |
- |