#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;
allp.push_back(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 |
2 ms |
504 KB |
Output is correct |
4 |
Correct |
9 ms |
1320 KB |
Output is correct |
5 |
Correct |
6 ms |
592 KB |
Output is correct |
6 |
Correct |
3 ms |
592 KB |
Output is correct |
7 |
Correct |
13 ms |
1808 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
9 ms |
1424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1016 KB |
Output is correct |
2 |
Correct |
6 ms |
848 KB |
Output is correct |
3 |
Correct |
7 ms |
848 KB |
Output is correct |
4 |
Correct |
8 ms |
944 KB |
Output is correct |
5 |
Correct |
345 ms |
36368 KB |
Output is correct |
6 |
Correct |
238 ms |
35912 KB |
Output is correct |
7 |
Correct |
165 ms |
20804 KB |
Output is correct |
8 |
Correct |
166 ms |
20760 KB |
Output is correct |
9 |
Correct |
8 ms |
1104 KB |
Output is correct |
10 |
Correct |
7 ms |
848 KB |
Output is correct |
11 |
Correct |
7 ms |
2040 KB |
Output is correct |
12 |
Correct |
279 ms |
35912 KB |
Output is correct |
# |
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 |
2 ms |
504 KB |
Output is correct |
4 |
Correct |
9 ms |
1320 KB |
Output is correct |
5 |
Correct |
6 ms |
592 KB |
Output is correct |
6 |
Correct |
3 ms |
592 KB |
Output is correct |
7 |
Correct |
13 ms |
1808 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
9 ms |
1424 KB |
Output is correct |
13 |
Correct |
6 ms |
1016 KB |
Output is correct |
14 |
Correct |
6 ms |
848 KB |
Output is correct |
15 |
Correct |
7 ms |
848 KB |
Output is correct |
16 |
Correct |
8 ms |
944 KB |
Output is correct |
17 |
Correct |
345 ms |
36368 KB |
Output is correct |
18 |
Correct |
238 ms |
35912 KB |
Output is correct |
19 |
Correct |
165 ms |
20804 KB |
Output is correct |
20 |
Correct |
166 ms |
20760 KB |
Output is correct |
21 |
Correct |
8 ms |
1104 KB |
Output is correct |
22 |
Correct |
7 ms |
848 KB |
Output is correct |
23 |
Correct |
7 ms |
2040 KB |
Output is correct |
24 |
Correct |
279 ms |
35912 KB |
Output is correct |
25 |
Correct |
7 ms |
848 KB |
Output is correct |
26 |
Correct |
6 ms |
996 KB |
Output is correct |
27 |
Correct |
8 ms |
1104 KB |
Output is correct |
28 |
Correct |
7 ms |
984 KB |
Output is correct |
29 |
Correct |
257 ms |
35884 KB |
Output is correct |
30 |
Correct |
286 ms |
36168 KB |
Output is correct |
31 |
Correct |
200 ms |
20916 KB |
Output is correct |
32 |
Correct |
176 ms |
20556 KB |
Output is correct |
33 |
Correct |
8 ms |
1104 KB |
Output is correct |
34 |
Correct |
6 ms |
1052 KB |
Output is correct |
35 |
Correct |
8 ms |
1872 KB |
Output is correct |
36 |
Correct |
416 ms |
44852 KB |
Output is correct |