#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,vvector<ll> &cst,vector<ll> pval,int n,ll s,vector<pll> &did,vector<ll> &allp) {
if (pval[v]==x) return true;
{
int now=v;
while (now!=-1) {
int val = x+pval[now]-pval[v]-s;
auto it = lower_bound(all(allp),val);
if (it!=allp.end() and *it==val) return true;
now=fa[now];
}
}
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) {
int val = pval[now]+tx/y-s;
// cout<<now<<" "<<tx<<" "<<y<<" "<<s<<" "<<val<<" now tx y s val\n";
auto it = lower_bound(all(cst[now]),val);
if (it!=cst[now].end() and *it==val) return true;
}
now=fa[now];
}
}
return false;
}
void dfs(int v,vector<pll> &did,vvector<pll> &child,int &tm,vvector<ll> &cst,vector<ll> &pval) {
did[v].F=tm++;
for (auto [i,l]:child[v]) {
dfs(i,did,child,tm,cst,pval);
for (ll j:cst[i]) cst[v].push_back(j);
}
cst[v].push_back(pval[v]);
sort(all(cst[v]));
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);
vvector<ll> cst(n+1);
vector<ll> pval(n+1);
vector<ll> allp;
fa[0]=-1;
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;
allp.push_back(pval[i]);
}
sort(all(allp));
// 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,cst,pval);
while (m--) {
int p,l;
cin>>p>>l;
l++;
bool flag = can(p,k-l,child,fa,cst,pval,n,s,did,allp);
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 |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Incorrect |
8 ms |
1104 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
848 KB |
Output is correct |
2 |
Correct |
6 ms |
848 KB |
Output is correct |
3 |
Correct |
6 ms |
848 KB |
Output is correct |
4 |
Correct |
7 ms |
1016 KB |
Output is correct |
5 |
Incorrect |
330 ms |
36168 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Incorrect |
8 ms |
1104 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |