#include <bits/stdc++.h>
#pragma GCC optimize ("O2")
#pragma GCC optimize ("unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;
const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 1000010, LOG=20;
int n, m, k, s, u, v, x, y, t, a, b;
ll L[MAXN], P[MAXN];
int cnt[MAXN];
bool ans[MAXN];
int X[MAXN], Y[MAXN];
bool mark[MAXN];
vector<int> G[MAXN];
vector<pll> query[MAXN];
vector<int> D;
void shit(int x){
D.clear();
for (int i=1; i*i<=x; i++) if (x%i==0){
D.pb(i);
D.pb(x/i);
}
}
void dfs2(int node, int par, int val){
ll x=L[node]-L[par]+s;
if (x<MAXN) cnt[x]+=val;
for (int v:G[node]) dfs2(v, par, val);
}
void dfs1(int node){
dfs2(node, node, +1);
for (pll p:query[node]){
shit(p.first);
for (int d:D) if (cnt[d]>0){
ans[p.second]=1;
break ;
}
}
for (int v:G[node]) dfs1(v);
dfs2(node, node, -1);
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
//for (int i=1; i<MAXN; i++) for (int j=i; j<MAXN; j+=i) D[j].pb(i);
cin>>n>>m>>k>>s;
s++;
mark[0]=1;
for (int i=1; i<=n; i++){
cin>>P[i]>>L[i];
G[P[i]].pb(i);
L[i]+=L[P[i]]+1;
if (L[i]<MAXN) mark[L[i]]=1;
}
for (int i=1; i<=m; i++){
cin>>x>>y;
X[i]=x;Y[i]=++y;
y+=L[x];
y=k-y;
if (y<0) continue ;
if (y==0) ans[i]=1;
// tool | k - (L[x]+y)
for (int tmp=X[i]; !ans[i]; tmp=P[tmp]){
if (mark[k-(s+Y[i]+L[X[i]]-L[tmp])]) ans[i]=1;
if (!tmp) break ;
}
if (ans[i]) continue ;
query[x].pb({y, i});
}
dfs1(0);
for (int i=1; i<=m; i++){
if (ans[i]) cout<<"YES\n";
else cout<<"NO\n";
}
return 0;
}
/*
2 4 22
2
0 1
1 5
2 13
2 10
1 4
0 7
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
47352 KB |
Output is correct |
2 |
Correct |
46 ms |
47452 KB |
Output is correct |
3 |
Correct |
47 ms |
47992 KB |
Output is correct |
4 |
Correct |
49 ms |
47864 KB |
Output is correct |
5 |
Correct |
51 ms |
51576 KB |
Output is correct |
6 |
Correct |
50 ms |
50168 KB |
Output is correct |
7 |
Correct |
53 ms |
51576 KB |
Output is correct |
8 |
Correct |
49 ms |
51192 KB |
Output is correct |
9 |
Correct |
49 ms |
51320 KB |
Output is correct |
10 |
Correct |
45 ms |
47352 KB |
Output is correct |
11 |
Correct |
46 ms |
47480 KB |
Output is correct |
12 |
Correct |
48 ms |
47864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
52088 KB |
Output is correct |
2 |
Correct |
54 ms |
52088 KB |
Output is correct |
3 |
Correct |
54 ms |
52188 KB |
Output is correct |
4 |
Correct |
55 ms |
52216 KB |
Output is correct |
5 |
Correct |
180 ms |
52208 KB |
Output is correct |
6 |
Correct |
179 ms |
52516 KB |
Output is correct |
7 |
Correct |
132 ms |
52108 KB |
Output is correct |
8 |
Correct |
127 ms |
52472 KB |
Output is correct |
9 |
Correct |
55 ms |
51668 KB |
Output is correct |
10 |
Correct |
54 ms |
51192 KB |
Output is correct |
11 |
Correct |
61 ms |
47584 KB |
Output is correct |
12 |
Correct |
154 ms |
49376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
47352 KB |
Output is correct |
2 |
Correct |
46 ms |
47452 KB |
Output is correct |
3 |
Correct |
47 ms |
47992 KB |
Output is correct |
4 |
Correct |
49 ms |
47864 KB |
Output is correct |
5 |
Correct |
51 ms |
51576 KB |
Output is correct |
6 |
Correct |
50 ms |
50168 KB |
Output is correct |
7 |
Correct |
53 ms |
51576 KB |
Output is correct |
8 |
Correct |
49 ms |
51192 KB |
Output is correct |
9 |
Correct |
49 ms |
51320 KB |
Output is correct |
10 |
Correct |
45 ms |
47352 KB |
Output is correct |
11 |
Correct |
46 ms |
47480 KB |
Output is correct |
12 |
Correct |
48 ms |
47864 KB |
Output is correct |
13 |
Correct |
54 ms |
52088 KB |
Output is correct |
14 |
Correct |
54 ms |
52088 KB |
Output is correct |
15 |
Correct |
54 ms |
52188 KB |
Output is correct |
16 |
Correct |
55 ms |
52216 KB |
Output is correct |
17 |
Correct |
180 ms |
52208 KB |
Output is correct |
18 |
Correct |
179 ms |
52516 KB |
Output is correct |
19 |
Correct |
132 ms |
52108 KB |
Output is correct |
20 |
Correct |
127 ms |
52472 KB |
Output is correct |
21 |
Correct |
55 ms |
51668 KB |
Output is correct |
22 |
Correct |
54 ms |
51192 KB |
Output is correct |
23 |
Correct |
61 ms |
47584 KB |
Output is correct |
24 |
Correct |
154 ms |
49376 KB |
Output is correct |
25 |
Correct |
54 ms |
51960 KB |
Output is correct |
26 |
Correct |
55 ms |
52128 KB |
Output is correct |
27 |
Correct |
54 ms |
52088 KB |
Output is correct |
28 |
Correct |
54 ms |
52216 KB |
Output is correct |
29 |
Correct |
174 ms |
52472 KB |
Output is correct |
30 |
Correct |
178 ms |
52304 KB |
Output is correct |
31 |
Correct |
125 ms |
52316 KB |
Output is correct |
32 |
Correct |
126 ms |
52264 KB |
Output is correct |
33 |
Correct |
60 ms |
51320 KB |
Output is correct |
34 |
Correct |
53 ms |
51192 KB |
Output is correct |
35 |
Correct |
50 ms |
47556 KB |
Output is correct |
36 |
Correct |
188 ms |
48504 KB |
Output is correct |