#include <bits/stdc++.h>
using namespace std;
#define a first
#define b second
#define pb push_back
typedef long long llo;
#define endl '\n'
int n;
int it[500001];
int cur[500001];
int le[500001];
int re[500001];
vector<int> pre[500001];
int tree[4*500001][2];
pair<int,int> tree2[500001];
void build(int no,int l,int r){
if(l==r){
tree[no][0]=le[l];
tree[no][1]=re[l];
tree2[no]={l,l};
}
else{
int mid=(l+r)/2;
build(no*2+1,l,mid);
build(no*2+2,mid+1,r);
tree[no][0]=min(tree[no*2+1][0],tree[no*2+2][0]);
tree[no][1]=max(tree[no*2+1][1],tree[no*2+2][1]);
tree2[no].a=tree2[no*2+1].a;
tree2[no].b=tree2[no*2+2].b;
}
}
pair<int,int> query(int no,int l,int r,int aa,int bb){
if(r<aa or l>bb or aa>bb){
return {n,0};
}
if(aa<=l and r<=bb){
return tree2[no];
}
int mid=(l+r)/2;
pair<int,int> x=query(no*2+1,l,mid,aa,bb);
pair<int,int> y=query(no*2+2,mid+1,r,aa,bb);
return {min(x.a,y.a),max(x.b,y.b)};
}
void update(int no,int l,int r,int i,pair<int,int> x){
if(l==r){
tree2[no]=x;
}
else{
int mid=(l+r)/2;
if(i<=mid){
update(no*2+1,l,mid,i,x);
}
else{
update(no*2+2,mid+1,r,i,x);
}
tree2[no].a=min(tree2[no*2+1].a,tree2[no*2+2].a);
tree2[no].b=max(tree2[no*2+1].b,tree2[no*2+2].b);
}
}
int query2(int no,int l,int r,int i,int x){
if(r<i){
return r+1;
}
if(l==r and tree[no][0]<x){
return l;
}
if(tree[no][0]>=x){
return r+1;
}
int mid=(l+r)/2;
int xx=query2(no*2+1,l,mid,i,x);
if(xx==mid+1){
return query2(no*2+2,mid+1,r,i,x);
}
return xx;
}
int query3(int no,int l,int r,int i,int x){
//return largest index<=i with r value >x
if(l>i){
return l-1;
}
if(l==r and tree[no][1]>x){
return l;
}
if(tree[no][1]<=x){
return l-1;
}
int mid=(l+r)/2;
int xx=query3(no*2+2,mid+1,r,i,x);
if(xx==mid){
return query3(no*2+1,l,mid,i,x);
}
return xx;
}
pair<int,int> ans[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];
it[i]--;
}
for(int i=0;i<n;i++){
int x;
cin>>x;
for(int j=0;j<x;j++){
int y;
cin>>y;
y--;
pre[i].pb(y);
}
}
for(int i=0;i<n;i++){
cur[i]=-1;
}
le[0]=-1;
for(int i=0;i<n;i++){
if(i>0){
le[i]=cur[it[i-1]];
}
for(auto j:pre[i]){
cur[j]=i;
}
}
re[n-1]=n+1;
for(int i=0;i<n;i++){
cur[i]=n+1;
}
for(int i=n-1;i>=0;i--){
if(i<n-1){
re[i]=cur[it[i]];
}
for(auto j:pre[i]){
cur[j]=i;
}
}
build(0,0,n-1);
/* for(int i=0;i<n;i++){
cout<<re[i]<<",";
}
cout<<endl;*/
for(int i=0;i<n;i++){
pair<int,int> cur={i,i};
while(true){
pair<int,int> cur2=cur;
if(cur.b<n-1){
int ind=query2(0,0,n-1,cur.b+1,cur.a);
ind--;
if(ind>cur.b){
cur.b=ind;
}
cur=query(0,0,n-1,cur.a,cur.b);
}
if(cur.a>0){
int ind=query3(0,0,n-1,cur.a-1,cur.b);
/*if(i==1){
cout<<cur.a<<":"<<cur.b<<endl;
cout<<ind<<"::"<<endl;
}*/
ind++;
if(ind<cur.a){
cur.a=ind;
}
cur=query(0,0,n-1,cur.a,cur.b);
}
/*if(i==3){
cout<<cur.a<<"::"<<cur.b<<endl;
}*/
if(cur2.a==cur.a and cur2.b==cur.b){
break;
}
}
ans[i]=cur;
update(0,0,n-1,i,cur);
//cout<<cur.a<<":"<<cur.b<<endl;
}
int q;
cin>>q;
while(q--){
int x,y;
cin>>x>>y;
x--;
y--;
if(ans[x].a<=y and ans[x].b>=y){
cout<<"YES"<<endl;
}
else{
cout<<"NO"<<endl;
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12344 KB |
Output is correct |
2 |
Correct |
10 ms |
12408 KB |
Output is correct |
3 |
Correct |
10 ms |
12628 KB |
Output is correct |
4 |
Correct |
9 ms |
12352 KB |
Output is correct |
5 |
Correct |
8 ms |
12244 KB |
Output is correct |
6 |
Correct |
9 ms |
12344 KB |
Output is correct |
7 |
Correct |
9 ms |
12296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12344 KB |
Output is correct |
2 |
Correct |
10 ms |
12408 KB |
Output is correct |
3 |
Correct |
10 ms |
12628 KB |
Output is correct |
4 |
Correct |
9 ms |
12352 KB |
Output is correct |
5 |
Correct |
8 ms |
12244 KB |
Output is correct |
6 |
Correct |
9 ms |
12344 KB |
Output is correct |
7 |
Correct |
9 ms |
12296 KB |
Output is correct |
8 |
Correct |
91 ms |
18252 KB |
Output is correct |
9 |
Correct |
90 ms |
18160 KB |
Output is correct |
10 |
Correct |
86 ms |
18588 KB |
Output is correct |
11 |
Correct |
88 ms |
19020 KB |
Output is correct |
12 |
Correct |
84 ms |
17676 KB |
Output is correct |
13 |
Correct |
88 ms |
18308 KB |
Output is correct |
14 |
Correct |
85 ms |
18316 KB |
Output is correct |
15 |
Correct |
89 ms |
18312 KB |
Output is correct |
16 |
Correct |
80 ms |
18112 KB |
Output is correct |
17 |
Correct |
99 ms |
18440 KB |
Output is correct |
18 |
Correct |
85 ms |
18364 KB |
Output is correct |
19 |
Correct |
85 ms |
18352 KB |
Output is correct |
20 |
Correct |
102 ms |
18328 KB |
Output is correct |
21 |
Correct |
81 ms |
18188 KB |
Output is correct |
22 |
Correct |
84 ms |
18228 KB |
Output is correct |
23 |
Correct |
84 ms |
18132 KB |
Output is correct |
24 |
Correct |
89 ms |
18104 KB |
Output is correct |
25 |
Correct |
91 ms |
18124 KB |
Output is correct |
26 |
Correct |
89 ms |
18096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
244 ms |
31236 KB |
Output is correct |
2 |
Correct |
208 ms |
31152 KB |
Output is correct |
3 |
Correct |
198 ms |
30840 KB |
Output is correct |
4 |
Correct |
202 ms |
31180 KB |
Output is correct |
5 |
Correct |
207 ms |
31148 KB |
Output is correct |
6 |
Correct |
218 ms |
29892 KB |
Output is correct |
7 |
Correct |
167 ms |
29728 KB |
Output is correct |
8 |
Correct |
161 ms |
29708 KB |
Output is correct |
9 |
Correct |
168 ms |
29612 KB |
Output is correct |
10 |
Correct |
172 ms |
29692 KB |
Output is correct |
11 |
Correct |
180 ms |
29656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12344 KB |
Output is correct |
2 |
Correct |
10 ms |
12408 KB |
Output is correct |
3 |
Correct |
10 ms |
12628 KB |
Output is correct |
4 |
Correct |
9 ms |
12352 KB |
Output is correct |
5 |
Correct |
8 ms |
12244 KB |
Output is correct |
6 |
Correct |
9 ms |
12344 KB |
Output is correct |
7 |
Correct |
9 ms |
12296 KB |
Output is correct |
8 |
Correct |
91 ms |
18252 KB |
Output is correct |
9 |
Correct |
90 ms |
18160 KB |
Output is correct |
10 |
Correct |
86 ms |
18588 KB |
Output is correct |
11 |
Correct |
88 ms |
19020 KB |
Output is correct |
12 |
Correct |
84 ms |
17676 KB |
Output is correct |
13 |
Correct |
88 ms |
18308 KB |
Output is correct |
14 |
Correct |
85 ms |
18316 KB |
Output is correct |
15 |
Correct |
89 ms |
18312 KB |
Output is correct |
16 |
Correct |
80 ms |
18112 KB |
Output is correct |
17 |
Correct |
99 ms |
18440 KB |
Output is correct |
18 |
Correct |
85 ms |
18364 KB |
Output is correct |
19 |
Correct |
85 ms |
18352 KB |
Output is correct |
20 |
Correct |
102 ms |
18328 KB |
Output is correct |
21 |
Correct |
81 ms |
18188 KB |
Output is correct |
22 |
Correct |
84 ms |
18228 KB |
Output is correct |
23 |
Correct |
84 ms |
18132 KB |
Output is correct |
24 |
Correct |
89 ms |
18104 KB |
Output is correct |
25 |
Correct |
91 ms |
18124 KB |
Output is correct |
26 |
Correct |
89 ms |
18096 KB |
Output is correct |
27 |
Correct |
244 ms |
31236 KB |
Output is correct |
28 |
Correct |
208 ms |
31152 KB |
Output is correct |
29 |
Correct |
198 ms |
30840 KB |
Output is correct |
30 |
Correct |
202 ms |
31180 KB |
Output is correct |
31 |
Correct |
207 ms |
31148 KB |
Output is correct |
32 |
Correct |
218 ms |
29892 KB |
Output is correct |
33 |
Correct |
167 ms |
29728 KB |
Output is correct |
34 |
Correct |
161 ms |
29708 KB |
Output is correct |
35 |
Correct |
168 ms |
29612 KB |
Output is correct |
36 |
Correct |
172 ms |
29692 KB |
Output is correct |
37 |
Correct |
180 ms |
29656 KB |
Output is correct |
38 |
Incorrect |
784 ms |
59776 KB |
Output isn't correct |
39 |
Halted |
0 ms |
0 KB |
- |