This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0); cin.tie(0)
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx,avx2")
//#pragma GCC target("popcnt")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MAX=2e5+10,P=1e9+7;
const ll INF=0x3f3f3f3f,oo=0x3f3f3f3f3f3f3f3f;
const ldb eps=1e-6;
const ldb PI=acos(-1.0);
const int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
template<typename T>
using vvector = vector<vector<T>>;
bool can(int v,int x,vvector<pll> &child,vector<int> &fa,vector<set<ll>> &pre,vector<ll> pval,int n,ll s,vector<pll> &did) {
if (pval[v]==x) return true;
for (int i=0;i<=n;i++) {
// cout<<i<<" "<<pval[v]+s+pval[i]-x<<" i bbb\n";
if (pre[v].find(pval[v]+s+pval[i]-x)!=pre[v].end()) return true;
}
int tx=x-pval[v];
vector<int> fac;
for (int i=1;i<=sqrt(tx)+eps;i++) {
if (tx%i==0) {
fac.push_back(i);
if (i*i!=tx) {
fac.push_back(tx/i);
}
}
}
auto cont = [](pll a,pll b) {
if (a.F<b.F and a.S>b.S) return 1;
if (a.F>b.F and a.S<b.S) return -1;
return 0;
};
for (int i=0;i<=n;i++) {
if (cont(did[i],did[v])==1) {
for (int y:fac) {
if (pre[i].find(pval[i]-tx/y+s)!=pre[i].end()) return true;
}
} else if (cont(did[i],did[v])==-1) {
for (int y:fac) {
// cout<<v<<" "<<i<<" "<<tx<<" "<<y<<" "<<s<<" "<<pval[i]-tx/y+s<<" v i tx y s bluh\n";
if (pre[v].find(pval[i]-tx/y+s)!=pre[v].end()) return true;
}
}
}
return false;
}
void dfs(int v,vector<pll> &did,vvector<pll> &child,int &tm) {
did[v].F=tm++;
for (pll i:child[v]) {
dfs(i.F,did,child,tm);
}
did[v].S=tm++;
}
int main() {
speed;
ll n,m,k;
cin>>n>>m>>k;
ll s;
cin>>s;
s++;
vvector<pll> child(n+1);
vector<int> fa(n+1);
vector<set<ll>> pre(n+1);
vector<ll> pval(n+1);
fa[0]=-1;
pre[0].insert(0);
pval[0]=0;
for (int i=1;i<=n;i++) {
ll p,l;
cin>>p>>l;
l++;
child[p].emplace_back(i,l);
fa[i]=p;
pval[i]=pval[p]+l;
for (int j:pre[p]) pre[i].insert(j);
pre[i].insert(pval[i]);
}
// for (int i=0;i<=n;i++) {
// cout<<i<<" : ";
// for (int j:pre[i]) cout<<j<<" ";
// cout<<"\n";
// }
int tm=1;
vector<pll> did(n+1);
dfs(0,did,child,tm);
while (m--) {
int p,l;
cin>>p>>l;
l++;
bool flag = can(p,k-l,child,fa,pre,pval,n,s,did);
if (flag) cout<<"YES\n";
else cout<<"NO\n";
}
return 0;
}
/*
2 1 40
9
0 1
1 5
1 1
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |