답안 #784904

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
784904 2023-07-16T17:51:28 Z kshitij_sodani Long Mansion (JOI17_long_mansion) C++14
100 / 100
543 ms 68676 KB
#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[4*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 8 ms 12244 KB Output is correct
2 Correct 9 ms 12372 KB Output is correct
3 Correct 10 ms 12628 KB Output is correct
4 Correct 9 ms 12308 KB Output is correct
5 Correct 8 ms 12244 KB Output is correct
6 Correct 9 ms 12308 KB Output is correct
7 Correct 9 ms 12304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12244 KB Output is correct
2 Correct 9 ms 12372 KB Output is correct
3 Correct 10 ms 12628 KB Output is correct
4 Correct 9 ms 12308 KB Output is correct
5 Correct 8 ms 12244 KB Output is correct
6 Correct 9 ms 12308 KB Output is correct
7 Correct 9 ms 12304 KB Output is correct
8 Correct 84 ms 13764 KB Output is correct
9 Correct 85 ms 13764 KB Output is correct
10 Correct 86 ms 14028 KB Output is correct
11 Correct 87 ms 14408 KB Output is correct
12 Correct 79 ms 13884 KB Output is correct
13 Correct 83 ms 14016 KB Output is correct
14 Correct 83 ms 13992 KB Output is correct
15 Correct 82 ms 14052 KB Output is correct
16 Correct 79 ms 14224 KB Output is correct
17 Correct 83 ms 13900 KB Output is correct
18 Correct 84 ms 13968 KB Output is correct
19 Correct 95 ms 13980 KB Output is correct
20 Correct 88 ms 14248 KB Output is correct
21 Correct 79 ms 14228 KB Output is correct
22 Correct 85 ms 14048 KB Output is correct
23 Correct 84 ms 13824 KB Output is correct
24 Correct 85 ms 13976 KB Output is correct
25 Correct 89 ms 13824 KB Output is correct
26 Correct 83 ms 13708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 200 ms 23884 KB Output is correct
2 Correct 204 ms 23868 KB Output is correct
3 Correct 190 ms 23884 KB Output is correct
4 Correct 200 ms 23976 KB Output is correct
5 Correct 200 ms 23912 KB Output is correct
6 Correct 195 ms 23372 KB Output is correct
7 Correct 158 ms 23628 KB Output is correct
8 Correct 160 ms 23636 KB Output is correct
9 Correct 159 ms 23568 KB Output is correct
10 Correct 157 ms 23624 KB Output is correct
11 Correct 149 ms 23580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12244 KB Output is correct
2 Correct 9 ms 12372 KB Output is correct
3 Correct 10 ms 12628 KB Output is correct
4 Correct 9 ms 12308 KB Output is correct
5 Correct 8 ms 12244 KB Output is correct
6 Correct 9 ms 12308 KB Output is correct
7 Correct 9 ms 12304 KB Output is correct
8 Correct 84 ms 13764 KB Output is correct
9 Correct 85 ms 13764 KB Output is correct
10 Correct 86 ms 14028 KB Output is correct
11 Correct 87 ms 14408 KB Output is correct
12 Correct 79 ms 13884 KB Output is correct
13 Correct 83 ms 14016 KB Output is correct
14 Correct 83 ms 13992 KB Output is correct
15 Correct 82 ms 14052 KB Output is correct
16 Correct 79 ms 14224 KB Output is correct
17 Correct 83 ms 13900 KB Output is correct
18 Correct 84 ms 13968 KB Output is correct
19 Correct 95 ms 13980 KB Output is correct
20 Correct 88 ms 14248 KB Output is correct
21 Correct 79 ms 14228 KB Output is correct
22 Correct 85 ms 14048 KB Output is correct
23 Correct 84 ms 13824 KB Output is correct
24 Correct 85 ms 13976 KB Output is correct
25 Correct 89 ms 13824 KB Output is correct
26 Correct 83 ms 13708 KB Output is correct
27 Correct 200 ms 23884 KB Output is correct
28 Correct 204 ms 23868 KB Output is correct
29 Correct 190 ms 23884 KB Output is correct
30 Correct 200 ms 23976 KB Output is correct
31 Correct 200 ms 23912 KB Output is correct
32 Correct 195 ms 23372 KB Output is correct
33 Correct 158 ms 23628 KB Output is correct
34 Correct 160 ms 23636 KB Output is correct
35 Correct 159 ms 23568 KB Output is correct
36 Correct 157 ms 23624 KB Output is correct
37 Correct 149 ms 23580 KB Output is correct
38 Correct 411 ms 51832 KB Output is correct
39 Correct 480 ms 68676 KB Output is correct
40 Correct 358 ms 56936 KB Output is correct
41 Correct 484 ms 67148 KB Output is correct
42 Correct 222 ms 30852 KB Output is correct
43 Correct 213 ms 30908 KB Output is correct
44 Correct 365 ms 43056 KB Output is correct
45 Correct 369 ms 43100 KB Output is correct
46 Correct 374 ms 43268 KB Output is correct
47 Correct 204 ms 30924 KB Output is correct
48 Correct 214 ms 30700 KB Output is correct
49 Correct 381 ms 43032 KB Output is correct
50 Correct 417 ms 43108 KB Output is correct
51 Correct 403 ms 43432 KB Output is correct
52 Correct 320 ms 41932 KB Output is correct
53 Correct 451 ms 57320 KB Output is correct
54 Correct 543 ms 64460 KB Output is correct
55 Correct 433 ms 57548 KB Output is correct