#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] = n;
/* 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 |
Incorrect |
32 ms |
29308 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
29308 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
400 ms |
38648 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
29308 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |