Submission #234984

#TimeUsernameProblemLanguageResultExecution timeMemory
234984Knps4422Long Mansion (JOI17_long_mansion)C++14
0 / 100
3082 ms31152 KiB
//#pragma optimization_level 3
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordset;
*/

#define fr first
#define sc second
#define vec vector
#define pb push_back
#define pii pair<int, int>
#define forn(x,y) for(int x = 1 ; x <= y ; ++x)
#define all(x) (x).begin(),(x).end()
#define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);


using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef complex<int> point;
const int nmax = 500005;
const ll linf = 1e18;
const ll mod = 998244353;
const int inf = INT_MAX;

int n, q;
ll key[nmax];
vec < int > ks[nmax], col_p[nmax];
int cr[nmax], cl[nmax], lf[nmax], rt[nmax];
bitset <nmax> vis;
// l -> l*2 - 1
// r -> r*2 - 1




int main(){
	fast;
	cin >> n;
	forn(i,n-1)cin >> key[i];
	forn(i,n){
		int a, b;
		cin >> b;
		forn(ii,b){
			cin >> a;
			ks[i].pb(a);
			col_p[a].pb(i);
		}
		sort(all(ks[i]));
	}
	cr[0] = inf;
	cl[1] = -1;
	cr[n] = inf;
	cl[n+1] = -1;
	for(int i = 1; i <= n; i++){
		// cl[i+1] , cr[i]
		cl[i+1] = -1;
		cr[i] = inf;
		for(int j : col_p[key[i]]){
			if(j > i) cr[i] = min(cr[i],j);
			if(j < i+1) cl[i+1] = max(cl[i+1],j);
		}
	}
	for(int i = 1; i <= n ; i++){
		rt[i] = i;
		while( cl[rt[i] + 1] >= i) rt[i] ++;
		lf[i] = i;
		while( cr[lf[i] - 1] <= i)lf[i]--; 
	}
	cin >> q;
	while(q -- ){
		vis.reset();
		int x, y;
		cin >> x >> y;
		bool tr = false;
		while( y != x && !tr && y > 0 && y <= n){
			vis[y] = 1;
			if(x < y){
				if(cl[y] < 1 || cl[y] > n || vis[cl[y]]) tr = true;
				y = cl[y]; 
			}else{
				if(cr[y] < 1 || cr[y] > n || vis[cr[y]]) tr = true;
				y = cr[y];
			}
		}
		if(tr || y < 1 || y > n){
			cout << "NO\n";
		}else{
			cout << "YES\n";
		}
	}
	


	
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...