Submission #723996

#TimeUsernameProblemLanguageResultExecution timeMemory
723996NothingXDLong Mansion (JOI17_long_mansion)C++17
100 / 100
430 ms46476 KiB
/*

   Wei Wai Wei Wai
   Zumigat nan damu dimi kwayt rayt

*/

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef complex<ld> point;
/*typedef __uint128_t L;
  struct FastMod {
  ull b, m;
  FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
  ull reduce(ull a) {
  ull q = (ull)((L(m) * a) >> 64);
  ull r = a - q * b; // can be proven that 0 <= r < 2*b
  return r >= b ? r - b : r;
  }
  };
  FastMod FM(2);
  */
void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	cerr << " " << H;
	debug_out(T...);
}

#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
#define F first
#define S second

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 5e5 + 10;

int n, q, L[maxn], R[maxn], a[maxn];
vector<int> val[maxn];
bool mark[maxn];

void solve(int l, int r){
	if (l + 1 == r){
		L[l] = R[l] = l;
		return;
	}
	int mid = (l + r) >> 1;
	solve(l, mid);
	solve(mid, r);
	int ptl = mid, ptr = mid-1;
	for (int i = mid; i < r; i++){
		if (L[i] != mid) continue;
		while(ptr < R[i]){
			ptr++;
			for (auto x: val[ptr]) mark[x] = true;
		}
		while((ptl > l && mark[a[ptl-1]]) || (ptr + 1 < r && mark[a[ptr]])){
			if (ptl > l && mark[a[ptl-1]]){
				ptl--;
				for (auto x: val[ptl]) mark[x] = true;
			}
			if (ptr + 1 < r && mark[a[ptr]]){
				ptr++;
				for (auto x: val[ptr]) mark[x] = true;
			}
		}
		L[i] = ptl, R[i] = ptr;
	}
	for (int i = l; i < r; i++) for (auto x: val[i]) mark[x] = false;
	ptl = mid, ptr = mid-1;
	for (int i = mid-1; i >= l; i--){
		if (R[i] != mid-1) continue;
		while(ptl > L[i]){
			ptl--;
			for (auto x: val[ptl]) mark[x] = true;
		}
		while((ptl > l && mark[a[ptl-1]]) || (ptr + 1 < r && mark[a[ptr]])){
			if (ptl > l && mark[a[ptl-1]]){
				ptl--;
				for (auto x: val[ptl]) mark[x] = true;
			}
			if (ptr + 1 < r && mark[a[ptr]]){
				ptr++;
				for (auto x: val[ptr]) mark[x] = true;
			}
		}
		L[i] = ptl, R[i] = ptr;
	}
	for (int i = l; i < r; i++) for (auto x: val[i]) mark[x] = false;
}

int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);

	cin >> n;
	for (int i = 1; i < n; i++) cin >> a[i];
	for (int i = 1; i <= n; i++){
		int k; cin >> k;
		for (int j = 0; j < k; j++){
			int x; cin >> x;
			val[i].push_back(x);
		}
	}

	solve(1, n+1);
	cin >> q;
	while(q--){
		int u, v; cin >> u >> v;
		cout << (L[u] <= v && v <= R[u]? "YES\n": "NO\n");
	}

	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...