#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> 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;
vector<int> keys(sz);
for(int j = 0; j < sz; j++) {
cin >> keys[j];
--keys[j];
ls[keys[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");
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
16888 KB |
Output is correct |
2 |
Correct |
28 ms |
17128 KB |
Output is correct |
3 |
Correct |
24 ms |
17268 KB |
Output is correct |
4 |
Correct |
28 ms |
17408 KB |
Output is correct |
5 |
Correct |
26 ms |
17472 KB |
Output is correct |
6 |
Correct |
24 ms |
17524 KB |
Output is correct |
7 |
Correct |
21 ms |
17600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
16888 KB |
Output is correct |
2 |
Correct |
28 ms |
17128 KB |
Output is correct |
3 |
Correct |
24 ms |
17268 KB |
Output is correct |
4 |
Correct |
28 ms |
17408 KB |
Output is correct |
5 |
Correct |
26 ms |
17472 KB |
Output is correct |
6 |
Correct |
24 ms |
17524 KB |
Output is correct |
7 |
Correct |
21 ms |
17600 KB |
Output is correct |
8 |
Correct |
208 ms |
23460 KB |
Output is correct |
9 |
Correct |
275 ms |
27804 KB |
Output is correct |
10 |
Correct |
208 ms |
28228 KB |
Output is correct |
11 |
Correct |
220 ms |
28328 KB |
Output is correct |
12 |
Correct |
214 ms |
28328 KB |
Output is correct |
13 |
Correct |
172 ms |
28328 KB |
Output is correct |
14 |
Correct |
230 ms |
28328 KB |
Output is correct |
15 |
Correct |
172 ms |
28328 KB |
Output is correct |
16 |
Correct |
198 ms |
28360 KB |
Output is correct |
17 |
Correct |
185 ms |
28360 KB |
Output is correct |
18 |
Correct |
235 ms |
28360 KB |
Output is correct |
19 |
Correct |
257 ms |
28360 KB |
Output is correct |
20 |
Correct |
184 ms |
28360 KB |
Output is correct |
21 |
Correct |
170 ms |
28360 KB |
Output is correct |
22 |
Correct |
176 ms |
28360 KB |
Output is correct |
23 |
Correct |
165 ms |
28360 KB |
Output is correct |
24 |
Correct |
160 ms |
28360 KB |
Output is correct |
25 |
Correct |
198 ms |
28360 KB |
Output is correct |
26 |
Correct |
170 ms |
28360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
341 ms |
33060 KB |
Output is correct |
2 |
Correct |
326 ms |
33060 KB |
Output is correct |
3 |
Correct |
321 ms |
33072 KB |
Output is correct |
4 |
Correct |
382 ms |
33072 KB |
Output is correct |
5 |
Correct |
396 ms |
33072 KB |
Output is correct |
6 |
Correct |
273 ms |
33072 KB |
Output is correct |
7 |
Correct |
284 ms |
33072 KB |
Output is correct |
8 |
Correct |
295 ms |
33072 KB |
Output is correct |
9 |
Correct |
286 ms |
33072 KB |
Output is correct |
10 |
Correct |
318 ms |
33072 KB |
Output is correct |
11 |
Correct |
274 ms |
33072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
16888 KB |
Output is correct |
2 |
Correct |
28 ms |
17128 KB |
Output is correct |
3 |
Correct |
24 ms |
17268 KB |
Output is correct |
4 |
Correct |
28 ms |
17408 KB |
Output is correct |
5 |
Correct |
26 ms |
17472 KB |
Output is correct |
6 |
Correct |
24 ms |
17524 KB |
Output is correct |
7 |
Correct |
21 ms |
17600 KB |
Output is correct |
8 |
Correct |
208 ms |
23460 KB |
Output is correct |
9 |
Correct |
275 ms |
27804 KB |
Output is correct |
10 |
Correct |
208 ms |
28228 KB |
Output is correct |
11 |
Correct |
220 ms |
28328 KB |
Output is correct |
12 |
Correct |
214 ms |
28328 KB |
Output is correct |
13 |
Correct |
172 ms |
28328 KB |
Output is correct |
14 |
Correct |
230 ms |
28328 KB |
Output is correct |
15 |
Correct |
172 ms |
28328 KB |
Output is correct |
16 |
Correct |
198 ms |
28360 KB |
Output is correct |
17 |
Correct |
185 ms |
28360 KB |
Output is correct |
18 |
Correct |
235 ms |
28360 KB |
Output is correct |
19 |
Correct |
257 ms |
28360 KB |
Output is correct |
20 |
Correct |
184 ms |
28360 KB |
Output is correct |
21 |
Correct |
170 ms |
28360 KB |
Output is correct |
22 |
Correct |
176 ms |
28360 KB |
Output is correct |
23 |
Correct |
165 ms |
28360 KB |
Output is correct |
24 |
Correct |
160 ms |
28360 KB |
Output is correct |
25 |
Correct |
198 ms |
28360 KB |
Output is correct |
26 |
Correct |
170 ms |
28360 KB |
Output is correct |
27 |
Correct |
341 ms |
33060 KB |
Output is correct |
28 |
Correct |
326 ms |
33060 KB |
Output is correct |
29 |
Correct |
321 ms |
33072 KB |
Output is correct |
30 |
Correct |
382 ms |
33072 KB |
Output is correct |
31 |
Correct |
396 ms |
33072 KB |
Output is correct |
32 |
Correct |
273 ms |
33072 KB |
Output is correct |
33 |
Correct |
284 ms |
33072 KB |
Output is correct |
34 |
Correct |
295 ms |
33072 KB |
Output is correct |
35 |
Correct |
286 ms |
33072 KB |
Output is correct |
36 |
Correct |
318 ms |
33072 KB |
Output is correct |
37 |
Correct |
274 ms |
33072 KB |
Output is correct |
38 |
Correct |
679 ms |
41848 KB |
Output is correct |
39 |
Correct |
601 ms |
44620 KB |
Output is correct |
40 |
Correct |
502 ms |
44620 KB |
Output is correct |
41 |
Correct |
779 ms |
44620 KB |
Output is correct |
42 |
Correct |
311 ms |
44620 KB |
Output is correct |
43 |
Correct |
332 ms |
44620 KB |
Output is correct |
44 |
Correct |
396 ms |
44620 KB |
Output is correct |
45 |
Correct |
507 ms |
44620 KB |
Output is correct |
46 |
Correct |
491 ms |
47736 KB |
Output is correct |
47 |
Correct |
312 ms |
50464 KB |
Output is correct |
48 |
Correct |
395 ms |
57440 KB |
Output is correct |
49 |
Correct |
534 ms |
71840 KB |
Output is correct |
50 |
Correct |
465 ms |
82420 KB |
Output is correct |
51 |
Correct |
511 ms |
93568 KB |
Output is correct |
52 |
Execution timed out |
3058 ms |
93568 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |