제출 #1171821

#제출 시각아이디문제언어결과실행 시간메모리
1171821NonozeFile Paths (BOI15_fil)C++20
100 / 100
452 ms3060 KiB
/*
*	Author: Nonoze
*	Created: Monday 10/03/2025
*/
#include <bits/stdc++.h>
using namespace std;

#ifndef DEBUG
	#define dbg(...)
#endif

// #define cout cerr << "OUT: "
#define endl '\n'
#define endlfl '\n' << flush
#define quit(x) return (void)(cout << x << endl)

template<typename T> void read(T& x) { cin >> x;}
template<typename T1, typename T2> void read(pair<T1, T2>& p) { read(p.first), read(p.second);}
template<typename T> void read(vector<T>& v) { for (auto& x : v) read(x); }
template<typename T1, typename T2> void read(T1& x, T2& y) { read(x), read(y); }
template<typename T1, typename T2, typename T3> void read(T1& x, T2& y, T3& z) { read(x), read(y), read(z); }
template<typename T1, typename T2, typename T3, typename T4> void read(T1& x, T2& y, T3& z, T4& zz) { read(x), read(y), read(z), read(zz); }
template<typename T> void print(vector<T>& v) { for (auto& x : v) cout << x << ' '; cout << endl; }

#define sz(x) (int)(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(v) sort(all(v)), v.erase(unique(all(v)), (v).end())
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define fi first
#define se second
#define cmin(a, b) a = min(a, b)
#define cmax(a, b) a = max(a, b)
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define QYES quit("YES")
#define QNO quit("NO")

// #define int long long
#define double long double
const int inf = numeric_limits<int>::max() / 4;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7, LOG=20;



void solve();

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int tt=1;
	// cin >> tt;
	while(tt--) solve();
	return 0;
}




int n, k, m, q;
vector<int> a, par, w;
vector<unordered_set<int>> vec;


void solve() {
	read(n, m, k);
	int s; read(s); s++;
	a.clear(), a.resize(n+1); a[0]=1;
	par.clear(), par.resize(n+1); par[0]=-1;
	w.clear(), w.resize(n+1);
	for (int i=0; i<n; i++) {
		int p, l; read(p, l);
		a[i+1]=a[p]+l+1;
		par[i+1]=p, w[i+1]=l+1;
	}
	n++;
	vec.clear(), vec.resize(n);
	vector<pair<int, int>> queries; queries.resize(m);
	set<int> maybe;
	for (int T=0; T<m; T++) {
		int p, l; read(p, l);
		queries[T]={p, l};
		int val=k-a[p]-l;
		for (int i=1; i*i<=val; i++) {
			if (val%i==0) {
				if (i-s>=0) maybe.insert(i-s);
				if (i*i!=val && val/i-s>=0) maybe.insert(val/i-s);
			}
		}
	}
	for (int i=0; i<n; i++) {
		int j=i;
		while (j!=-1) {
			if (maybe.count(a[i]-a[j])) vec[j].insert(a[i]-a[j]);
			j=par[j];
		}
	}
	// for (int i=1; i<n; i++) vec[i].insert(all(vec[par[i]]));
	vector<bool> answers;
	for (int T=0; T<m; T++) {
		auto [p, l]=queries[T];
		if (a[p]+l==k) {
			answers.pb(1);
			continue;
		}
		unordered_set<int> st;
		int val=a[p]+l;

		int act=p;
		while (act!=-1) st.insert(val-a[act]), act=par[act];
		bool ok=0;
		for (int i=0; i<n; i++) if (st.count(k-a[i]-s)) ok=1;
		if (ok) {
			answers.pb(1);
			continue;
		} else if (val>=k) {
			answers.pb(0);
			continue;
		}
		val=k-val;
		unordered_set<int> vals;
		for (int i=1; i*i<=val; i++) {
			if (val%i==0) {
				if (i-s>=0) vals.insert(i-s);
				if (i*i!=val && val/i-s>=0) vals.insert(val/i-s);
			}
		}
		act=p;
		while (act!=-1) {
			if (sz(vals)<sz(vec[act])) {
				for (auto x: vals) {
					if (vec[act].count(x)) {
						ok=1;
						break;
					}
				}
			} else {
				for (auto x: vec[act]) {
					if (vals.count(x)) {
						ok=1;
						break;
					}
				}
			}
			if (ok) break;
			act=par[act];
		}
		if (ok) answers.pb(1);
		else answers.pb(0);
	}
	for (auto x: answers) cout << (x ? "YES" : "NO") << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...