#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 ] ;
vector < int > divs[ maxval ] ;
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 ) {
for ( auto x : divs[ targ - tot ] ) {
if ( per[ x ] > 0 ) {
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 } ) ;
}
for ( int i = 1 ; i <= targ ; ++ i ) {
for ( int j = i ; j <= targ ; j += i ) {
divs[ j ].emplace_back ( i ) ;
}
}
per[ 0 ] = 1 ;
divs[ 0 ].push_back ( 0 ) ;
dep[ 0 ] = 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 ;
}
Compilation message
fil.cpp: In function 'void dfs(int)':
fil.cpp:52:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
52 | for ( auto [ len , id ] : qlist[ x ] ) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
47444 KB |
Output is correct |
2 |
Correct |
28 ms |
48348 KB |
Output is correct |
3 |
Correct |
80 ms |
56656 KB |
Output is correct |
4 |
Correct |
78 ms |
56776 KB |
Output is correct |
5 |
Execution timed out |
1088 ms |
138216 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1042 ms |
136068 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
47444 KB |
Output is correct |
2 |
Correct |
28 ms |
48348 KB |
Output is correct |
3 |
Correct |
80 ms |
56656 KB |
Output is correct |
4 |
Correct |
78 ms |
56776 KB |
Output is correct |
5 |
Execution timed out |
1088 ms |
138216 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |