Repository navigation
Expand file tree
/
Copy pathlong_mansion.cpp
More file actions
77 lines (68 loc) · 1.54 KB
/
Copy pathlong_mansion.cpp
File metadata and controls
77 lines (68 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
same sol idea : https://github.com/nikolapesic2802/Programming-Practice/blob/master/Long%20Mansion/main.cpp
*/
#include <bits/stdc++.h>
using namespace std;
#define MAX_N 500005
int N;
int C[MAX_N];
int B ,K;
vector <int> Keys[MAX_N];
int Q;
int X ,Y;
int last[MAX_N];
int nl[MAX_N] ,nr[MAX_N];
int st[MAX_N] ,en[MAX_N];
int main()
{
scanf("%d",&N);
for(int i=0; i<N-1; i++)
scanf("%d",&C[i]);
for(int i=0; i<N; i++)
{
scanf("%d",&B);
while(B--){
scanf("%d",&K);
Keys[i].push_back(K);
}
}
memset(last ,-1 ,sizeof last);
for(int i=0; i<N-1; i++){
for(int j : Keys[i])
last[j] = i;
nl[i] = last[C[i]];
}
memset(last ,-1 ,sizeof last);
for(int i=N-2; ~i; i--){
for(int j : Keys[i+1])
last[j] = i+1;
nr[i] = last[C[i]];
}
auto can_left = [&](int i){
return (st[i] > 0 && ~nr[st[i]-1] && nr[st[i]-1] <= en[i]);
};
auto can_right = [&](int i){
return (en[i] < N-1 && ~nl[en[i]] && st[i] <= nl[en[i]]);
};
for(int i=0; i<N; i++)
{
st[i] = en[i] = i;
while(true)
{
if(can_left(i)){
en[i] = max(en[i] ,en[st[i]-1]);
st[i] = st[st[i]-1];
}
else if(can_right(i))
en[i]++;
else
break;
}
}
scanf("%d",&Q);
while(Q--)
{
scanf("%d%d",&X,&Y); X-- ,Y--;
printf(st[X]<=Y&&Y<=en[X] ? "YES\n" : "NO\n");
}
}