//#pragma GCC optimize ("O2")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define task "task"
#define task "task"
#define prll pair<ll,ll>
#define pb push_back
#define ld long double
const ll MIN=-1e18,MAX=1e18,MOD=1e9+7,nmax=5e5+5;
int a[nmax],f[nmax];
vector<int> key[nmax];
int rr[nmax],lll[nmax],exl[nmax],exr[nmax];
void dnc(int l,int r)
{
if(l==r)
{
exl[l]=exr[l]=l;
return;
}
int m=(l+r)/2;
dnc(l,m);
dnc(m+1,r);
int cl=m,cr=m;
for(int i=m;i>=l;i--)
{
if(exr[i]==m)
{
cl=min(cl,exl[i]);
while(1)
{
if(cr<r and rr[cr]>=cl) cr++;
else if(cl>l and lll[cl]<=cr) cl--;
else break;
}
exr[i]=cr,exl[i]=cl;
}
}
cl=m+1,cr=m+1;
for(int i=m+1;i<=r;i++)
{
if(exl[i]==m+1)
{
cr=max(cr,exr[i]);
while(1)
{
if(cl>l and lll[cl]<=cr) cl--;
else if(cr<r and rr[cr]>=cl) cr++;
else break;
}
exr[i]=cr,exl[i]=cl;
}
}
}
int main(){
//freopen (task".inp", "r", stdin);
//freopen (task".out", "w", stdout);
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n;
cin>>n;
for(int i=1;i<n;i++)
{
cin>>a[i];
}
for(int i=1;i<=n;i++)
{
int x,y;
cin>>x;
for(int j=1;j<=x;j++)
{
cin>>y;
key[i].push_back(y);
}
}
// cout<<endl;
for(int i=1;i<=n;i++)
{
for(auto j:key[i])
{
f[j]=i;
}
rr[i]=f[a[i]];
}
for(int i=0;i<=n;i++) f[i]=n+1;
for(int i=n;i>=1;i--)
{
for(auto j:key[i])
{
f[j]=i;
}
lll[i]=f[a[i-1]];
}
// for(int i=1;i<=n;i++)
// {
// cout<<rr[i]<<" "<<lll[i]<<endl;
// }
// cout<<endl;
dnc(1,n);
// for(int i=1;i<=n;i++)
// {
// cout<<exl[i]<<" "<<exr[i]<<endl;
// }
int q;
cin>>q;
for(int i=1;i<=q;i++)
{
int x,y;
cin>>x>>y;
if(exl[x]<=y and exr[x]>=y) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
/*
5
1 2 3 4
2 2 3
1 1
1 1
1 3
1 4
4
2 4
4 2
1 5
5 3
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
22872 KB |
Output is correct |
2 |
Correct |
5 ms |
23128 KB |
Output is correct |
3 |
Correct |
6 ms |
23128 KB |
Output is correct |
4 |
Correct |
6 ms |
23132 KB |
Output is correct |
5 |
Correct |
5 ms |
22972 KB |
Output is correct |
6 |
Correct |
5 ms |
23128 KB |
Output is correct |
7 |
Correct |
5 ms |
22876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
22872 KB |
Output is correct |
2 |
Correct |
5 ms |
23128 KB |
Output is correct |
3 |
Correct |
6 ms |
23128 KB |
Output is correct |
4 |
Correct |
6 ms |
23132 KB |
Output is correct |
5 |
Correct |
5 ms |
22972 KB |
Output is correct |
6 |
Correct |
5 ms |
23128 KB |
Output is correct |
7 |
Correct |
5 ms |
22876 KB |
Output is correct |
8 |
Correct |
86 ms |
24656 KB |
Output is correct |
9 |
Correct |
82 ms |
24400 KB |
Output is correct |
10 |
Correct |
80 ms |
24632 KB |
Output is correct |
11 |
Correct |
79 ms |
24912 KB |
Output is correct |
12 |
Correct |
75 ms |
24656 KB |
Output is correct |
13 |
Correct |
78 ms |
24660 KB |
Output is correct |
14 |
Correct |
78 ms |
24656 KB |
Output is correct |
15 |
Correct |
78 ms |
24696 KB |
Output is correct |
16 |
Correct |
76 ms |
24912 KB |
Output is correct |
17 |
Correct |
87 ms |
24612 KB |
Output is correct |
18 |
Correct |
80 ms |
25040 KB |
Output is correct |
19 |
Correct |
78 ms |
24740 KB |
Output is correct |
20 |
Correct |
76 ms |
24912 KB |
Output is correct |
21 |
Correct |
76 ms |
24912 KB |
Output is correct |
22 |
Correct |
78 ms |
24856 KB |
Output is correct |
23 |
Correct |
86 ms |
24504 KB |
Output is correct |
24 |
Correct |
78 ms |
24664 KB |
Output is correct |
25 |
Correct |
78 ms |
24444 KB |
Output is correct |
26 |
Correct |
91 ms |
24656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
28496 KB |
Output is correct |
2 |
Correct |
140 ms |
28416 KB |
Output is correct |
3 |
Correct |
132 ms |
28496 KB |
Output is correct |
4 |
Correct |
149 ms |
28576 KB |
Output is correct |
5 |
Correct |
137 ms |
28496 KB |
Output is correct |
6 |
Correct |
118 ms |
28052 KB |
Output is correct |
7 |
Correct |
116 ms |
28188 KB |
Output is correct |
8 |
Correct |
110 ms |
28240 KB |
Output is correct |
9 |
Correct |
111 ms |
28244 KB |
Output is correct |
10 |
Correct |
111 ms |
28240 KB |
Output is correct |
11 |
Correct |
110 ms |
28240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
22872 KB |
Output is correct |
2 |
Correct |
5 ms |
23128 KB |
Output is correct |
3 |
Correct |
6 ms |
23128 KB |
Output is correct |
4 |
Correct |
6 ms |
23132 KB |
Output is correct |
5 |
Correct |
5 ms |
22972 KB |
Output is correct |
6 |
Correct |
5 ms |
23128 KB |
Output is correct |
7 |
Correct |
5 ms |
22876 KB |
Output is correct |
8 |
Correct |
86 ms |
24656 KB |
Output is correct |
9 |
Correct |
82 ms |
24400 KB |
Output is correct |
10 |
Correct |
80 ms |
24632 KB |
Output is correct |
11 |
Correct |
79 ms |
24912 KB |
Output is correct |
12 |
Correct |
75 ms |
24656 KB |
Output is correct |
13 |
Correct |
78 ms |
24660 KB |
Output is correct |
14 |
Correct |
78 ms |
24656 KB |
Output is correct |
15 |
Correct |
78 ms |
24696 KB |
Output is correct |
16 |
Correct |
76 ms |
24912 KB |
Output is correct |
17 |
Correct |
87 ms |
24612 KB |
Output is correct |
18 |
Correct |
80 ms |
25040 KB |
Output is correct |
19 |
Correct |
78 ms |
24740 KB |
Output is correct |
20 |
Correct |
76 ms |
24912 KB |
Output is correct |
21 |
Correct |
76 ms |
24912 KB |
Output is correct |
22 |
Correct |
78 ms |
24856 KB |
Output is correct |
23 |
Correct |
86 ms |
24504 KB |
Output is correct |
24 |
Correct |
78 ms |
24664 KB |
Output is correct |
25 |
Correct |
78 ms |
24444 KB |
Output is correct |
26 |
Correct |
91 ms |
24656 KB |
Output is correct |
27 |
Correct |
134 ms |
28496 KB |
Output is correct |
28 |
Correct |
140 ms |
28416 KB |
Output is correct |
29 |
Correct |
132 ms |
28496 KB |
Output is correct |
30 |
Correct |
149 ms |
28576 KB |
Output is correct |
31 |
Correct |
137 ms |
28496 KB |
Output is correct |
32 |
Correct |
118 ms |
28052 KB |
Output is correct |
33 |
Correct |
116 ms |
28188 KB |
Output is correct |
34 |
Correct |
110 ms |
28240 KB |
Output is correct |
35 |
Correct |
111 ms |
28244 KB |
Output is correct |
36 |
Correct |
111 ms |
28240 KB |
Output is correct |
37 |
Correct |
110 ms |
28240 KB |
Output is correct |
38 |
Correct |
186 ms |
37456 KB |
Output is correct |
39 |
Correct |
185 ms |
41040 KB |
Output is correct |
40 |
Correct |
175 ms |
34092 KB |
Output is correct |
41 |
Correct |
223 ms |
41324 KB |
Output is correct |
42 |
Correct |
114 ms |
27984 KB |
Output is correct |
43 |
Correct |
116 ms |
27984 KB |
Output is correct |
44 |
Correct |
158 ms |
31056 KB |
Output is correct |
45 |
Correct |
152 ms |
31180 KB |
Output is correct |
46 |
Correct |
155 ms |
31056 KB |
Output is correct |
47 |
Correct |
117 ms |
27984 KB |
Output is correct |
48 |
Correct |
114 ms |
27984 KB |
Output is correct |
49 |
Correct |
162 ms |
31132 KB |
Output is correct |
50 |
Correct |
154 ms |
31056 KB |
Output is correct |
51 |
Correct |
160 ms |
31204 KB |
Output is correct |
52 |
Correct |
141 ms |
30860 KB |
Output is correct |
53 |
Correct |
167 ms |
34128 KB |
Output is correct |
54 |
Correct |
180 ms |
37548 KB |
Output is correct |
55 |
Correct |
160 ms |
34048 KB |
Output is correct |