/* Bismillahir Rahmanir Rahim */
#include <bits/stdc++.h>
using namespace std;
const int N = 500005;
int n, lf[N], rf[N], C[N];
int past[N], t[N*4];
int L[N], R[N];
vector<int>A[N];
void build(int l, int r, int id){
if(l == r){
t[id] = lf[l];
return;
}
int mid = (l + r) / 2;
build(l, mid, id + id);
build(mid + 1, r, id + id + 1);
t[id] = min(t[id + id], t[id + id + 1]);
}
int get(int l, int r, int id, int val){
if(t[id] >= val) return n + 1;
if(l == r) return l;
int mid = (l + r) / 2;
if(t[id + id] < val) return get(l, mid, id + id, val);
else return get(mid + 1, r, id + id + 1, val);
}
void update(int l, int r, int id, int pos){
if(l == r){
t[id] = n + 1;
return;
}
int mid = (l + r) / 2;
if(pos <= mid) update(l, mid, id + id, pos);
else update(mid + 1, r, id + id + 1, pos);
t[id] = min(t[id + id], t[id + id + 1]);
}
void init(){
for(int i = 0; i < N; i++) past[i] = 0;
for(int i = 1; i <= n; i++){
lf[i] = past[ C[i] ];
for(auto u : A[i]){
past[u] = i;
}
}
for(int i = 0; i < N; i++) past[i] = n + 1;
for(int i = n; i >= 1; i--){
for(auto u : A[i]){
past[u] = i;
}
rf[i] = past[ C[i] ];
}
build(1, n, 1);
}
void solve(){
stack<int>S;
for(int i = 1; i <= n; i++){
L[i] = R[i] = i;
update(1, n, 1, i);
while(1){
R[i] = get(1, n, 1, L[i]) - 1;
if(L[i] == 1) break;
if(rf[ L[i] ] > R[i]) break;
L[i] = S.top(); S.pop();
}
S.push(L[i]);
}
}
int main(){
int q, x, y;
scanf("%d", &n);
for(int i = 2; i <= n; i++) scanf("%d", &C[i]);
for(int i = 1; i <= n; i++){
scanf("%d", &x);
while(x--){
scanf("%d", &y);
A[i].push_back(y);
}
}
init();
solve();
scanf("%d", &q);
while(q--){
scanf("%d %d", &x, &y);
if(L[x] <= y && y <= R[x]) printf("YES\n");
else printf("NO\n");
}
return 0;
}
Compilation message
long_mansion.cpp: In function 'int main()':
long_mansion.cpp:79:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
^
long_mansion.cpp:80:48: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i = 2; i <= n; i++) scanf("%d", &C[i]);
^
long_mansion.cpp:82:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &x);
^
long_mansion.cpp:84:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &y);
^
long_mansion.cpp:90:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q);
^
long_mansion.cpp:92:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &x, &y);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
33404 KB |
Output is correct |
2 |
Correct |
9 ms |
33404 KB |
Output is correct |
3 |
Correct |
13 ms |
33404 KB |
Output is correct |
4 |
Correct |
6 ms |
33404 KB |
Output is correct |
5 |
Correct |
6 ms |
33404 KB |
Output is correct |
6 |
Correct |
3 ms |
33404 KB |
Output is correct |
7 |
Correct |
3 ms |
33404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
33404 KB |
Output is correct |
2 |
Correct |
9 ms |
33404 KB |
Output is correct |
3 |
Correct |
13 ms |
33404 KB |
Output is correct |
4 |
Correct |
6 ms |
33404 KB |
Output is correct |
5 |
Correct |
6 ms |
33404 KB |
Output is correct |
6 |
Correct |
3 ms |
33404 KB |
Output is correct |
7 |
Correct |
3 ms |
33404 KB |
Output is correct |
8 |
Correct |
173 ms |
33404 KB |
Output is correct |
9 |
Correct |
179 ms |
33404 KB |
Output is correct |
10 |
Correct |
179 ms |
33404 KB |
Output is correct |
11 |
Correct |
159 ms |
33404 KB |
Output is correct |
12 |
Correct |
163 ms |
33272 KB |
Output is correct |
13 |
Correct |
193 ms |
33404 KB |
Output is correct |
14 |
Correct |
149 ms |
33404 KB |
Output is correct |
15 |
Correct |
153 ms |
33404 KB |
Output is correct |
16 |
Correct |
166 ms |
33404 KB |
Output is correct |
17 |
Correct |
173 ms |
33404 KB |
Output is correct |
18 |
Correct |
179 ms |
33404 KB |
Output is correct |
19 |
Correct |
179 ms |
33404 KB |
Output is correct |
20 |
Correct |
193 ms |
33404 KB |
Output is correct |
21 |
Correct |
149 ms |
33404 KB |
Output is correct |
22 |
Correct |
149 ms |
33404 KB |
Output is correct |
23 |
Correct |
176 ms |
33404 KB |
Output is correct |
24 |
Correct |
179 ms |
33404 KB |
Output is correct |
25 |
Correct |
166 ms |
33404 KB |
Output is correct |
26 |
Correct |
143 ms |
33404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
326 ms |
37232 KB |
Output is correct |
2 |
Correct |
383 ms |
37100 KB |
Output is correct |
3 |
Correct |
333 ms |
36968 KB |
Output is correct |
4 |
Correct |
309 ms |
37100 KB |
Output is correct |
5 |
Correct |
346 ms |
37100 KB |
Output is correct |
6 |
Correct |
279 ms |
36440 KB |
Output is correct |
7 |
Correct |
289 ms |
36440 KB |
Output is correct |
8 |
Correct |
229 ms |
36440 KB |
Output is correct |
9 |
Correct |
289 ms |
36440 KB |
Output is correct |
10 |
Correct |
249 ms |
36440 KB |
Output is correct |
11 |
Correct |
309 ms |
36440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
33404 KB |
Output is correct |
2 |
Correct |
9 ms |
33404 KB |
Output is correct |
3 |
Correct |
13 ms |
33404 KB |
Output is correct |
4 |
Correct |
6 ms |
33404 KB |
Output is correct |
5 |
Correct |
6 ms |
33404 KB |
Output is correct |
6 |
Correct |
3 ms |
33404 KB |
Output is correct |
7 |
Correct |
3 ms |
33404 KB |
Output is correct |
8 |
Correct |
173 ms |
33404 KB |
Output is correct |
9 |
Correct |
179 ms |
33404 KB |
Output is correct |
10 |
Correct |
179 ms |
33404 KB |
Output is correct |
11 |
Correct |
159 ms |
33404 KB |
Output is correct |
12 |
Correct |
163 ms |
33272 KB |
Output is correct |
13 |
Correct |
193 ms |
33404 KB |
Output is correct |
14 |
Correct |
149 ms |
33404 KB |
Output is correct |
15 |
Correct |
153 ms |
33404 KB |
Output is correct |
16 |
Correct |
166 ms |
33404 KB |
Output is correct |
17 |
Correct |
173 ms |
33404 KB |
Output is correct |
18 |
Correct |
179 ms |
33404 KB |
Output is correct |
19 |
Correct |
179 ms |
33404 KB |
Output is correct |
20 |
Correct |
193 ms |
33404 KB |
Output is correct |
21 |
Correct |
149 ms |
33404 KB |
Output is correct |
22 |
Correct |
149 ms |
33404 KB |
Output is correct |
23 |
Correct |
176 ms |
33404 KB |
Output is correct |
24 |
Correct |
179 ms |
33404 KB |
Output is correct |
25 |
Correct |
166 ms |
33404 KB |
Output is correct |
26 |
Correct |
143 ms |
33404 KB |
Output is correct |
27 |
Correct |
326 ms |
37232 KB |
Output is correct |
28 |
Correct |
383 ms |
37100 KB |
Output is correct |
29 |
Correct |
333 ms |
36968 KB |
Output is correct |
30 |
Correct |
309 ms |
37100 KB |
Output is correct |
31 |
Correct |
346 ms |
37100 KB |
Output is correct |
32 |
Correct |
279 ms |
36440 KB |
Output is correct |
33 |
Correct |
289 ms |
36440 KB |
Output is correct |
34 |
Correct |
229 ms |
36440 KB |
Output is correct |
35 |
Correct |
289 ms |
36440 KB |
Output is correct |
36 |
Correct |
249 ms |
36440 KB |
Output is correct |
37 |
Correct |
309 ms |
36440 KB |
Output is correct |
38 |
Correct |
533 ms |
47396 KB |
Output is correct |
39 |
Correct |
576 ms |
50988 KB |
Output is correct |
40 |
Correct |
403 ms |
43964 KB |
Output is correct |
41 |
Correct |
573 ms |
48848 KB |
Output is correct |
42 |
Correct |
266 ms |
36440 KB |
Output is correct |
43 |
Correct |
253 ms |
36440 KB |
Output is correct |
44 |
Correct |
389 ms |
39608 KB |
Output is correct |
45 |
Correct |
389 ms |
39608 KB |
Output is correct |
46 |
Correct |
409 ms |
39608 KB |
Output is correct |
47 |
Correct |
299 ms |
36440 KB |
Output is correct |
48 |
Correct |
273 ms |
36440 KB |
Output is correct |
49 |
Correct |
363 ms |
39476 KB |
Output is correct |
50 |
Correct |
419 ms |
39476 KB |
Output is correct |
51 |
Correct |
426 ms |
39476 KB |
Output is correct |
52 |
Correct |
389 ms |
39968 KB |
Output is correct |
53 |
Correct |
476 ms |
43172 KB |
Output is correct |
54 |
Correct |
556 ms |
46604 KB |
Output is correct |
55 |
Correct |
473 ms |
43304 KB |
Output is correct |