This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std ;
#define maxn 200009
#define ll long long
#define fi first
#define se second
#define pb push_back
#define left id<<1
#define right id<<1|1
#define _lower(x) lower_bound(v.begin(),v.end(),x)-v.begin()+1
#define TIME (1.0*clock()/TIME_PERS_SEC)
#define re exit(0);
const int mod = 1e9+7 ;
const int INF = 1e9 ;
const int LOG = 18 ;
typedef vector<int> vi ;
typedef pair<int,int> pii ;
typedef vector<pii> vii ;
void add ( int &a , int b )
{
a += b ;
if ( a >= mod ) a -= mod ;
if ( a < 0 ) a += mod ;
}
template < typename T > void chkmin ( T &a , T b ) { if ( a > b ) a = b ;}
template < typename T > void chkmax ( T &a , T b ) { if ( a < b ) a = b ;}
void rf ()
{
freopen ("bai1.inp","r",stdin) ;
}
mt19937 rng (time(0)) ;
int _pow ( int a , int n )
{
if ( n == 0 ) return 1 ;
int res = _pow (a,n/2) ;
if ( n % 2 ) return 1ll*res*res%mod*a%mod ;
else return 1ll*res*res%mod ;
}
int n , nq , root ;
vi adjList [maxn] ;
vi child [maxn] ;
int dp [maxn] ;
int par [maxn][20] , dep [maxn] ;
void dfs ( int u )
{
dp [u] = u ;
for ( auto v : adjList [u] )
{
if ( v == par [u][0] ) continue ;
par [v][0] = u ; dep [v] = dep [u] + 1 ;
for ( int i = 1 ; i <= LOG ; i ++ ) par [v][i] = par[par[v][i-1]][i-1] ;
dfs (v) ;
chkmin (dp[u],dp[v]) ;
child [u] . pb (v) ;
}
}
bool cmp ( int u , int v )
{
return dp [u] < dp [v] ;
}
int ver1 [maxn] , ver2 [maxn] ;
void find_order ( int u )
{
static int cnt = 0 ;
for ( auto v : child [u] ) find_order (v) ;
ver1 [++cnt] = u ;
ver2 [u] = cnt ;
}
int have_ball [maxn] ;
int T [maxn*4] ;
void update ( int id , int l , int r , int pos , int w )
{
if ( l > pos || r < pos ) return ;
if ( l == r )
{
T [id] = w ;
return ;
}
int mid = (l+r)/2 ;
update (left,l,mid,pos,w) ;
update (right,mid+1,r,pos,w) ;
T [id] = min (T[left],T[right]) ;
}
int get ( int id , int l , int r )
{
if ( l == r ) return l ;
int mid = (l+r)/2 ;
if ( T [left] == 0 ) return get (left,l,mid) ;
else return get (right,mid+1,r) ;
}
int jump ( int x , int h )
{
for ( int i = LOG ; i >= 0 ; i -- ) if ( h >> i & 1 ) x = par [x][i] ;
return x ;
}
int main ()
{
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// rf () ;
cin >> n >> nq ;
for ( int i = 1 ; i <= n ; i ++ )
{
int x ; cin >> x ;
if ( x == 0 ) root = i ;
else
{
adjList [x] . pb (i) ;
adjList [i] . pb (x) ;
}
}
dfs (root) ;
for ( int i = 1 ; i <= n ; i ++ ) sort (child[i].begin(),child[i].end(),cmp) ;
find_order (root) ;
while ( nq -- )
{
int op , x ; cin >> op >> x ;
if ( op == 1 )
{
int pos ;
while ( x -- )
{
pos = get (1,1,n) ;
update (1,1,n,pos,1) ;
have_ball [ver1[pos]] = 1 ;
}
cout << ver1[pos] << "\n" ;
}
else
{
int l = 0 , r = dep [x] , res;
while ( l <= r )
{
int mid = (l+r)/2 ;
int parent = jump (x,mid) ;
if ( have_ball [parent] == true ) l = mid+1 , res = mid ;
else r = mid-1 ;
}
int parent = jump (x,res) ;
have_ball [parent] = 0 ;
update (1,1,n,ver2[parent],0) ;
cout << res << "\n" ;
}
}
}
Compilation message (stderr)
ballmachine.cpp: In function 'void rf()':
ballmachine.cpp:34:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | freopen ("bai1.inp","r",stdin) ;
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:106:45: warning: 'res' may be used uninitialized in this function [-Wmaybe-uninitialized]
106 | for ( int i = LOG ; i >= 0 ; i -- ) if ( h >> i & 1 ) x = par [x][i] ;
| ~~^~~~
ballmachine.cpp:148:30: note: 'res' was declared here
148 | int l = 0 , r = dep [x] , res;
| ^~~
ballmachine.cpp:144:25: warning: 'pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
144 | cout << ver1[pos] << "\n" ;
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |