#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int N;
int C[500005];
vector<vector<int>> A;
struct SegTree {
vector<int> seg;
int MAX;
SegTree(int N) {
int i;
for(i=1;i<2*N;i*=2) {}
seg.resize(i);
MAX = i;
for(i=0;i<MAX;i++) {
seg[i] = INF;
}
}
void cons() {
for(int i = MAX/2-1;i>=1;i--) {
seg[i] = min(seg[2*i], seg[2*i+1]);
}
}
void update(int n) {
n += MAX/2;
n /= 2;
while(n) {
seg[n] = min(seg[2*n], seg[2*n+1]);
n /= 2;
}
}
void Edit(int n, int a) {
seg[n+MAX/2] = a;
update(n);
}
int sum(int s, int e, int n, int ns, int ne) {
if(e<=ns||ne<=s) return INF;
if(s<=ns&&ne<=e) return seg[n];
int mid = ns + ne >> 1;
return min(sum(s, e, 2*n, ns, mid), sum(s, e, 2*n+1, mid, ne));
}
int get_right(int s, int e, int k, int n, int ns, int ne) {
if(e<=ns||ne<=s||seg[n]>=k) return -1;
if(n>=MAX/2) return ns;
int mid = (ns + ne) / 2;
int c = get_right(s, e, k, 2*n, ns, mid);
if(c != -1) return c;
return get_right(s, e, k, 2*n+1, mid, ne);
}
};
int L[500005];
int R[500005];
int prv[500005];
int nxt[500005];
int D[500005];
void init(vector<int> _C, vector<vector<int>> _A) {
N = _A.size();
A = _A;
int i, j;
for(i=0;i<N-1;i++) C[i] = _C[i] - 1;
for(i=0;i<N;i++) {
for(j=0;j<A[i].size();j++) A[i][j]--;
}
for(i=0;i<N;i++) D[i] = -1;
for(i=0;i<N;i++) {
prv[i] = (i == 0 ? -1 : D[C[i-1]]);
for(int n : A[i]) D[n] = i;
}
for(i=0;i<N;i++) D[i] = N;
for(i=N-1;i>=0;i--) {
nxt[i] = (i == N-1 ? N : D[C[i]]);
for(int n : A[i]) D[n] = i;
}
SegTree tree(N+5);
int MAX = tree.MAX;
for(i=0;i<N;i++) {
tree.seg[i+MAX/2] = prv[i];
//tree.Edit(i, prv[i]);
}
tree.cons();
for(i=0;i<N;i++) {
L[i] = i;
R[i] = i;
int pL = L[i], pR = R[i];
while(true) {
if(R[i] < N-1) R[i] = tree.get_right(R[i]+1, N, L[i], 1, 0, MAX/2) - 1;
if(R[i]==-2) R[i] = N-1;
if(L[i] >= 1 && nxt[L[i]-1] <= R[i]) {
L[i] = L[L[i]-1];
}
if(pL==L[i]&&pR==R[i]) break;
pL= L[i];
pR = R[i];
}
//cout << i << " : " << L[i] <<' ' << R[i] << '\n';
}
}
bool isPos(int a, int b) {
a--;
b--;
return L[a]<=b&&b<=R[a];
}
signed main() {
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N;
cin >> N;
int i, j;
vector<int> c(N-1);
for(i=0;i<N-1;i++) cin >> c[i];
vector<vector<int>> a(N);
for(i=0;i<N;i++) {
int sz;
cin >> sz;
a[i].resize(sz);
for(j=0;j<sz;j++) cin >> a[i][j];
}
init(c, a);
int Q;
cin >> Q;
while(Q--) {
int x, y;
cin >> x >> y;
cout << (isPos(x, y) ? "YES\n": "NO\n");
}
}
Compilation message
long_mansion.cpp: In member function 'int SegTree::sum(int, int, int, int, int)':
long_mansion.cpp:39:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
39 | int mid = ns + ne >> 1;
| ~~~^~~~
long_mansion.cpp: In function 'void init(std::vector<int>, std::vector<std::vector<int> >)':
long_mansion.cpp:62:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for(j=0;j<A[i].size();j++) A[i][j]--;
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
724 KB |
Output is correct |
2 |
Correct |
3 ms |
948 KB |
Output is correct |
3 |
Correct |
4 ms |
1620 KB |
Output is correct |
4 |
Correct |
3 ms |
852 KB |
Output is correct |
5 |
Correct |
2 ms |
732 KB |
Output is correct |
6 |
Correct |
3 ms |
852 KB |
Output is correct |
7 |
Correct |
2 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
724 KB |
Output is correct |
2 |
Correct |
3 ms |
948 KB |
Output is correct |
3 |
Correct |
4 ms |
1620 KB |
Output is correct |
4 |
Correct |
3 ms |
852 KB |
Output is correct |
5 |
Correct |
2 ms |
732 KB |
Output is correct |
6 |
Correct |
3 ms |
852 KB |
Output is correct |
7 |
Correct |
2 ms |
852 KB |
Output is correct |
8 |
Correct |
89 ms |
6636 KB |
Output is correct |
9 |
Correct |
88 ms |
6592 KB |
Output is correct |
10 |
Correct |
89 ms |
7080 KB |
Output is correct |
11 |
Correct |
86 ms |
7780 KB |
Output is correct |
12 |
Correct |
76 ms |
6008 KB |
Output is correct |
13 |
Correct |
100 ms |
6856 KB |
Output is correct |
14 |
Correct |
83 ms |
6836 KB |
Output is correct |
15 |
Correct |
82 ms |
6860 KB |
Output is correct |
16 |
Correct |
101 ms |
6612 KB |
Output is correct |
17 |
Correct |
79 ms |
6764 KB |
Output is correct |
18 |
Correct |
80 ms |
6824 KB |
Output is correct |
19 |
Correct |
105 ms |
6888 KB |
Output is correct |
20 |
Correct |
84 ms |
6732 KB |
Output is correct |
21 |
Correct |
82 ms |
6712 KB |
Output is correct |
22 |
Correct |
94 ms |
6732 KB |
Output is correct |
23 |
Correct |
84 ms |
6604 KB |
Output is correct |
24 |
Correct |
84 ms |
6680 KB |
Output is correct |
25 |
Correct |
92 ms |
6636 KB |
Output is correct |
26 |
Correct |
81 ms |
6536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
167 ms |
21436 KB |
Output is correct |
2 |
Correct |
178 ms |
26284 KB |
Output is correct |
3 |
Correct |
155 ms |
26008 KB |
Output is correct |
4 |
Correct |
166 ms |
26508 KB |
Output is correct |
5 |
Correct |
167 ms |
26440 KB |
Output is correct |
6 |
Correct |
145 ms |
25424 KB |
Output is correct |
7 |
Correct |
150 ms |
25256 KB |
Output is correct |
8 |
Correct |
169 ms |
25236 KB |
Output is correct |
9 |
Correct |
138 ms |
25192 KB |
Output is correct |
10 |
Correct |
148 ms |
25132 KB |
Output is correct |
11 |
Correct |
174 ms |
25116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
724 KB |
Output is correct |
2 |
Correct |
3 ms |
948 KB |
Output is correct |
3 |
Correct |
4 ms |
1620 KB |
Output is correct |
4 |
Correct |
3 ms |
852 KB |
Output is correct |
5 |
Correct |
2 ms |
732 KB |
Output is correct |
6 |
Correct |
3 ms |
852 KB |
Output is correct |
7 |
Correct |
2 ms |
852 KB |
Output is correct |
8 |
Correct |
89 ms |
6636 KB |
Output is correct |
9 |
Correct |
88 ms |
6592 KB |
Output is correct |
10 |
Correct |
89 ms |
7080 KB |
Output is correct |
11 |
Correct |
86 ms |
7780 KB |
Output is correct |
12 |
Correct |
76 ms |
6008 KB |
Output is correct |
13 |
Correct |
100 ms |
6856 KB |
Output is correct |
14 |
Correct |
83 ms |
6836 KB |
Output is correct |
15 |
Correct |
82 ms |
6860 KB |
Output is correct |
16 |
Correct |
101 ms |
6612 KB |
Output is correct |
17 |
Correct |
79 ms |
6764 KB |
Output is correct |
18 |
Correct |
80 ms |
6824 KB |
Output is correct |
19 |
Correct |
105 ms |
6888 KB |
Output is correct |
20 |
Correct |
84 ms |
6732 KB |
Output is correct |
21 |
Correct |
82 ms |
6712 KB |
Output is correct |
22 |
Correct |
94 ms |
6732 KB |
Output is correct |
23 |
Correct |
84 ms |
6604 KB |
Output is correct |
24 |
Correct |
84 ms |
6680 KB |
Output is correct |
25 |
Correct |
92 ms |
6636 KB |
Output is correct |
26 |
Correct |
81 ms |
6536 KB |
Output is correct |
27 |
Correct |
167 ms |
21436 KB |
Output is correct |
28 |
Correct |
178 ms |
26284 KB |
Output is correct |
29 |
Correct |
155 ms |
26008 KB |
Output is correct |
30 |
Correct |
166 ms |
26508 KB |
Output is correct |
31 |
Correct |
167 ms |
26440 KB |
Output is correct |
32 |
Correct |
145 ms |
25424 KB |
Output is correct |
33 |
Correct |
150 ms |
25256 KB |
Output is correct |
34 |
Correct |
169 ms |
25236 KB |
Output is correct |
35 |
Correct |
138 ms |
25192 KB |
Output is correct |
36 |
Correct |
148 ms |
25132 KB |
Output is correct |
37 |
Correct |
174 ms |
25116 KB |
Output is correct |
38 |
Correct |
311 ms |
87960 KB |
Output is correct |
39 |
Correct |
324 ms |
107204 KB |
Output is correct |
40 |
Correct |
279 ms |
67364 KB |
Output is correct |
41 |
Correct |
334 ms |
105572 KB |
Output is correct |
42 |
Correct |
158 ms |
26384 KB |
Output is correct |
43 |
Correct |
151 ms |
26488 KB |
Output is correct |
44 |
Correct |
216 ms |
45724 KB |
Output is correct |
45 |
Correct |
232 ms |
45948 KB |
Output is correct |
46 |
Correct |
231 ms |
46100 KB |
Output is correct |
47 |
Correct |
162 ms |
26436 KB |
Output is correct |
48 |
Correct |
148 ms |
26220 KB |
Output is correct |
49 |
Correct |
244 ms |
45756 KB |
Output is correct |
50 |
Correct |
217 ms |
45872 KB |
Output is correct |
51 |
Correct |
231 ms |
46228 KB |
Output is correct |
52 |
Correct |
231 ms |
44760 KB |
Output is correct |
53 |
Correct |
274 ms |
68000 KB |
Output is correct |
54 |
Correct |
322 ms |
88992 KB |
Output is correct |
55 |
Correct |
263 ms |
68164 KB |
Output is correct |