#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
const int MAXN = 3007 ;
const int maxval = 2e6 + 7 ;
int n , q , targ , skiplen ;
int a[ MAXN ] ;
vector < int > v[ MAXN ] ;
vector < pair < int , int > > qlist[ MAXN ] ;
bool ans[ MAXN ] ;
int per[ maxval ] , drop[ 2 * maxval ] ;
vector < int > stk ;
int dep[ MAXN ] ;
void add ( int coef ) {
int x = stk.back ( ) ;
for ( int i = 0 ; i <= n ; ++ i ) {
int w = dep[ i ] + skiplen + 1 - dep[ x ] ;
drop[ maxval + w ] += coef ;
}
}
void add_subtree ( int x , int root , int coef ) {
int hh = dep[ x ] - dep[ root ] + skiplen + 1 ;
if ( hh <= targ ) {
per[ hh ] += coef ;
}
for ( auto y : v[ x ] ) {
add_subtree ( y , root , coef ) ;
}
}
void init ( int x ) {
for ( auto y : v[ x ] ) {
dep[ y ] = dep[ x ] + a[ y ] + 1 ;
init ( y ) ;
}
}
void dfs ( int x ) {
stk.push_back ( x ) ;
add ( 1 ) ;
add_subtree ( x , x , 1 ) ;
for ( auto y : v[ x ] ) {
dfs ( y ) ;
}
for ( auto [ len , id ] : qlist[ x ] ) {
int tot = dep[ x ] + len ;
if ( tot <= targ ) {
int diff = targ - tot ;
for ( int j = 1 ; j * j <= diff ; ++ j ) {
if ( ( diff % j ) == 0 ) {
if ( per[ j ] > 0 || per[ diff / j ] ) {
ans[ id ] = true ;
break ;
}
}
}
}
if ( drop[ maxval + targ - tot ] > 0 ) {
ans[ id ] = true ;
}
}
add_subtree ( x , x , -1 ) ;
add ( -1 ) ;
stk.pop_back ( ) ;
}
void solve ( ) {
cin >> n >> q >> targ ;
cin >> skiplen ;
for ( int i = 1 , x ; i <= n ; ++ i ) {
cin >> x >> a[ i ] ;
v[ x ].push_back ( i ) ;
}
for ( int i = 1 , wh , x ; i <= q ; ++ i ) {
cin >> wh >> x ;
qlist[ wh ].push_back ( { x , i } ) ;
}
dep[ 0 ] = 1 ;
drop[ maxval ] = 1 ;
init ( 0 ) ;
dfs ( 0 ) ;
for ( int i = 1 ; i <= q ; ++ i ) {
if ( ans[ i ] == true ) { cout << "YES\n" ; }
else { cout << "NO\n" ; }
}
}
int main ( ) {
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int t = 1 ; // cin >> t ;
while ( t -- ) { solve ( ) ; }
return 0 ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
3 ms |
1620 KB |
Output is correct |
4 |
Correct |
4 ms |
1620 KB |
Output is correct |
5 |
Correct |
12 ms |
11988 KB |
Output is correct |
6 |
Correct |
10 ms |
10808 KB |
Output is correct |
7 |
Correct |
19 ms |
11604 KB |
Output is correct |
8 |
Correct |
10 ms |
11368 KB |
Output is correct |
9 |
Correct |
10 ms |
11448 KB |
Output is correct |
10 |
Correct |
1 ms |
484 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
4 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
12136 KB |
Output is correct |
2 |
Correct |
99 ms |
12080 KB |
Output is correct |
3 |
Correct |
111 ms |
12316 KB |
Output is correct |
4 |
Correct |
124 ms |
12144 KB |
Output is correct |
5 |
Correct |
255 ms |
12400 KB |
Output is correct |
6 |
Correct |
245 ms |
12584 KB |
Output is correct |
7 |
Correct |
169 ms |
12200 KB |
Output is correct |
8 |
Correct |
196 ms |
12496 KB |
Output is correct |
9 |
Correct |
102 ms |
11784 KB |
Output is correct |
10 |
Correct |
97 ms |
11252 KB |
Output is correct |
11 |
Correct |
55 ms |
928 KB |
Output is correct |
12 |
Correct |
142 ms |
4984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
3 ms |
1620 KB |
Output is correct |
4 |
Correct |
4 ms |
1620 KB |
Output is correct |
5 |
Correct |
12 ms |
11988 KB |
Output is correct |
6 |
Correct |
10 ms |
10808 KB |
Output is correct |
7 |
Correct |
19 ms |
11604 KB |
Output is correct |
8 |
Correct |
10 ms |
11368 KB |
Output is correct |
9 |
Correct |
10 ms |
11448 KB |
Output is correct |
10 |
Correct |
1 ms |
484 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
4 ms |
1364 KB |
Output is correct |
13 |
Correct |
96 ms |
12136 KB |
Output is correct |
14 |
Correct |
99 ms |
12080 KB |
Output is correct |
15 |
Correct |
111 ms |
12316 KB |
Output is correct |
16 |
Correct |
124 ms |
12144 KB |
Output is correct |
17 |
Correct |
255 ms |
12400 KB |
Output is correct |
18 |
Correct |
245 ms |
12584 KB |
Output is correct |
19 |
Correct |
169 ms |
12200 KB |
Output is correct |
20 |
Correct |
196 ms |
12496 KB |
Output is correct |
21 |
Correct |
102 ms |
11784 KB |
Output is correct |
22 |
Correct |
97 ms |
11252 KB |
Output is correct |
23 |
Correct |
55 ms |
928 KB |
Output is correct |
24 |
Correct |
142 ms |
4984 KB |
Output is correct |
25 |
Correct |
99 ms |
11888 KB |
Output is correct |
26 |
Correct |
106 ms |
12032 KB |
Output is correct |
27 |
Correct |
106 ms |
12100 KB |
Output is correct |
28 |
Correct |
103 ms |
12260 KB |
Output is correct |
29 |
Correct |
243 ms |
12544 KB |
Output is correct |
30 |
Correct |
238 ms |
12344 KB |
Output is correct |
31 |
Correct |
195 ms |
12308 KB |
Output is correct |
32 |
Correct |
168 ms |
12236 KB |
Output is correct |
33 |
Correct |
96 ms |
11308 KB |
Output is correct |
34 |
Correct |
83 ms |
11248 KB |
Output is correct |
35 |
Correct |
39 ms |
784 KB |
Output is correct |
36 |
Correct |
161 ms |
2852 KB |
Output is correct |