#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) {
if (pval[v]==x) return true;
for (int i=1;i<=n;i++) {
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);
}
}
}
int now=v;
while (now!=-1) {
for (int y:fac) {
// cout<<now<<" "<<tx<<" "<<y<<" "<<s<<" "<<pval[now]-tx/y+s<<" now tx y s blahblah\n";
if (pre[now].find(pval[now]-tx/y+s)!=pre[now].end()) return true;
}
now=fa[now];
}
return false;
}
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;
pre[i].insert(pval[i]);
}
while (m--) {
int p,l;
cin>>p>>l;
l++;
bool flag = can(p,k-l,child,fa,pre,pval,n,s);
if (flag) cout<<"YES\n";
else cout<<"NO\n";
}
return 0;
}
/*
6 3 10
2
0 0
0 0
1 0
1 0
2 0
2 0
3 1
3 2
3 3
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
33 ms |
892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |