//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl '\n'
int it[500001];
set<int> ss[500001];
int n;
pair<int,int> ans[500001];
vector<int> pre[500001];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
for(int i=0;i<n-1;i++){
cin>>it[i];
}
for(int i=0;i<n;i++){
int x;
cin>>x;
for(int j=0;j<x;j++){
int aa;
cin>>aa;
ss[aa].insert(i);
}
}
set<int> xx;
for(int i=1;i<n;i++){
auto j=ss[it[i-1]].upper_bound(i-1);
if(j==ss[it[i-1]].begin()){
xx.insert(i);
//cout<<i<<",,";
}
else{
j--;
pre[*j].pb(i);
}
}
//cout<<endl;
for(int i=0;i<n;i++){
auto j=xx.upper_bound(i);
if(j==xx.end()){
ans[i]={i,n-1};
}
else{
ans[i]={i,(*j)-1};
}
//cout<<ans[i].a<<":"<<ans[i].b<<endl;
if(i>0){
pair<int,int> cur=ans[i];
while(cur.a>0){
auto ac=ss[it[cur.a-1]].lower_bound(cur.a);
if(ac==ss[it[cur.a-1]].end()){
break;
}
/*if(i==3){
cout<<(*ac)<<endl;
}*/
if((*ac)>cur.b){
break;
}
pair<int,int> cur2=ans[cur.a-1];
ans[i].a=min(ans[i].a,cur2.a);
ans[i].b=max(ans[i].b,cur2.a);
// cout<<cur2.a<<"::"<<cur2.b<<endl;
/* if(cur2.b>=i){
break;
}*/
int yy=ans[i].b;
for(int ii=yy+1;ii<n;ii++){
auto j=ss[it[ii-1]].lower_bound(ans[i].a);
/*if(i==1){
cout<<ii<<"<<<<<"<<endl;
}*/
if(j==ss[it[ii-1]].end()){
break;
}
if((*j)<=ans[i].b){
ans[i].b+=1;
continue;
}
break;
}
cur=ans[i];
continue;
}
}
//cout<<ans[i].a<<","<<ans[i].b<<endl;
for(auto j:pre[i]){
xx.insert(j);
}
}
int q;
cin>>q;
while(q--){
int aa,bb;
cin>>aa>>bb;
aa--;
bb--;
if(bb>=ans[aa].a and bb<=ans[aa].b){
cout<<"YES"<<endl;
}
else{
cout<<"NO"<<endl;
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
35948 KB |
Output is correct |
2 |
Correct |
43 ms |
36076 KB |
Output is correct |
3 |
Correct |
85 ms |
36332 KB |
Output is correct |
4 |
Correct |
31 ms |
36076 KB |
Output is correct |
5 |
Correct |
33 ms |
36076 KB |
Output is correct |
6 |
Correct |
31 ms |
36080 KB |
Output is correct |
7 |
Correct |
28 ms |
35948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
35948 KB |
Output is correct |
2 |
Correct |
43 ms |
36076 KB |
Output is correct |
3 |
Correct |
85 ms |
36332 KB |
Output is correct |
4 |
Correct |
31 ms |
36076 KB |
Output is correct |
5 |
Correct |
33 ms |
36076 KB |
Output is correct |
6 |
Correct |
31 ms |
36080 KB |
Output is correct |
7 |
Correct |
28 ms |
35948 KB |
Output is correct |
8 |
Correct |
159 ms |
41836 KB |
Output is correct |
9 |
Correct |
159 ms |
41836 KB |
Output is correct |
10 |
Correct |
174 ms |
42220 KB |
Output is correct |
11 |
Correct |
228 ms |
42732 KB |
Output is correct |
12 |
Correct |
152 ms |
41452 KB |
Output is correct |
13 |
Correct |
153 ms |
42092 KB |
Output is correct |
14 |
Correct |
152 ms |
42092 KB |
Output is correct |
15 |
Correct |
167 ms |
42092 KB |
Output is correct |
16 |
Correct |
159 ms |
41836 KB |
Output is correct |
17 |
Correct |
159 ms |
42092 KB |
Output is correct |
18 |
Correct |
152 ms |
42092 KB |
Output is correct |
19 |
Correct |
152 ms |
42092 KB |
Output is correct |
20 |
Correct |
169 ms |
42092 KB |
Output is correct |
21 |
Correct |
160 ms |
41964 KB |
Output is correct |
22 |
Correct |
169 ms |
41964 KB |
Output is correct |
23 |
Correct |
158 ms |
41708 KB |
Output is correct |
24 |
Correct |
156 ms |
41708 KB |
Output is correct |
25 |
Correct |
158 ms |
41796 KB |
Output is correct |
26 |
Correct |
160 ms |
41708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3064 ms |
59420 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
35948 KB |
Output is correct |
2 |
Correct |
43 ms |
36076 KB |
Output is correct |
3 |
Correct |
85 ms |
36332 KB |
Output is correct |
4 |
Correct |
31 ms |
36076 KB |
Output is correct |
5 |
Correct |
33 ms |
36076 KB |
Output is correct |
6 |
Correct |
31 ms |
36080 KB |
Output is correct |
7 |
Correct |
28 ms |
35948 KB |
Output is correct |
8 |
Correct |
159 ms |
41836 KB |
Output is correct |
9 |
Correct |
159 ms |
41836 KB |
Output is correct |
10 |
Correct |
174 ms |
42220 KB |
Output is correct |
11 |
Correct |
228 ms |
42732 KB |
Output is correct |
12 |
Correct |
152 ms |
41452 KB |
Output is correct |
13 |
Correct |
153 ms |
42092 KB |
Output is correct |
14 |
Correct |
152 ms |
42092 KB |
Output is correct |
15 |
Correct |
167 ms |
42092 KB |
Output is correct |
16 |
Correct |
159 ms |
41836 KB |
Output is correct |
17 |
Correct |
159 ms |
42092 KB |
Output is correct |
18 |
Correct |
152 ms |
42092 KB |
Output is correct |
19 |
Correct |
152 ms |
42092 KB |
Output is correct |
20 |
Correct |
169 ms |
42092 KB |
Output is correct |
21 |
Correct |
160 ms |
41964 KB |
Output is correct |
22 |
Correct |
169 ms |
41964 KB |
Output is correct |
23 |
Correct |
158 ms |
41708 KB |
Output is correct |
24 |
Correct |
156 ms |
41708 KB |
Output is correct |
25 |
Correct |
158 ms |
41796 KB |
Output is correct |
26 |
Correct |
160 ms |
41708 KB |
Output is correct |
27 |
Execution timed out |
3064 ms |
59420 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |