Submission #834319

#TimeUsernameProblemLanguageResultExecution timeMemory
834319makanhuliaBall Machine (BOI13_ballmachine)C++17
8.69 / 100
1099 ms28108 KiB
#include <bits/stdc++.h>
#define inf INT_MAX
#define longlonginf LONG_LONG_MAX
#define mod  998244353
#define MAXN 100005
#define pii pair<ll,ll>
#define ll long long
#define deb(x) cerr<<"[ "<<#x<<" = "<<x<<" ]";
#define yes() cout<<"YES\n";
#define no() cout<<"NO\n";
using namespace std;

ll n,m,cur,q,x;
ll h;
ll ans = 0;
string subtask;
ll t;
ll par[MAXN][20];
vector<vector<ll>> child(MAXN);
ll root;
ll v[4*MAXN];
ll k;
bool vis[4*MAXN];

void build(ll x){
  if(child[x].size() == 0 ){
    v[x] = x;
    return;
  }
  for(int i = 0 ; i < (int) child[x].size() ; i++){
    build(child[x][i]);
  }
  v[x] = x;
  for(int i = 0 ; i < (int) child[x].size() ; i++){
    v[x] = min(v[x],child[x][i]);
  }
}

bool by_v(int a,int b){
  return v[a] < v[b];
}

void add(ll x){
  //cout<<k<<" "<<"traversing ; "<<x<<"\n";
  if( child[x].size() == 0 ) {
    //cout<<"visiting : "<<x<<"\n";
    vis[x] = 1;
    ans = x;
    return;
  }
  int cnt = 0;
  for(int i = 0 ; i < (int) child[x].size() ; i++){
    if( vis[child[x][i]] == 0 ){
      return add(child[x][i]);
      break;
    }
    else cnt++;
  }
  if( cnt == (int) child[x].size() ){
    //cout<<"visiting : "<<x<<"\n";
    vis[x] = 1;
    ans = x;
    return;
  }
}

void remove(int x,ll d){
  ll l = 0,r = n;
  ll u;
  ll tu;
  while( l <= r ){
    ll m = (l+r)/2;
    u = x;
    for(int i = 0; i <= 18 ; i++){
      if( ((1<<i) & m) != 0 ) u = par[u][i]; 
    }
    //cout<<"! "<<m<<" "<<u<<"\n";
    if( vis[u] == 0 ){
      tu = m;
      r = m-1;
    }
    else{
      l = m+1;
    }
  }
  tu--;
  //deb(tu);
  u = x;
  for(int i = 0; i <= 18 ; i++){
    if( ((1<<i) & tu) != 0 ) u = par[u][i]; 
  }
  //cout<<u<<"!\n";
  vis[u] = 0;
  cout<<tu<<"\n";
}

void solve(){
  cin>>n>>q;
  for(int i = 1 ; i <= n ; i++){
    cin>>x;
    if( x == 0 ) root = i;
    par[i][0] = x;
    child[x].push_back(i);
  }
  //init
  build(root);
  for(int i = 0 ; i <= n ; i++) par[root][i] = 0;
  for(int i = 1 ; i <= n ; i++) sort(child[i].begin(),child[i].end(),by_v);
  for(int i = 1 ; i <= 18 ; i++){
    for(int j = 1 ; j <= n ; j++){
      par[j][i] = par[par[j][i-1]][i-1];
    }
  }
  memset(vis,0,sizeof(vis));
  //queries
  while(q--){
    cin>>x;
    if( x == 1 ){
      cin>>k;
      while(k--) {
        add(1);
      }
      cout<<ans<<"\n";
    }
    else{
      cin>>k;
      remove(k,0);
    }
  }
}


int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  //cin>>T;
  for(int i = 0 ; i < T ; i++){
    //cout<<"Case #"<<i+1<<": ";
    solve();
  }
  return 0;
}

/*
  you thought u declared it huh?
  not i but x
  logical operator
  wrong example/proof
  thoroughly
  wrong variables
  thinking it wrong
  bruh just try some test case
  capitals ;-;
  wrong data structure lol
  count memory usement
  corner case
  oversized array
  orders
  statements
  size initializer
  while con
  map -> array
  wrong digits??
  swapped variables??
  check if theres any variabled
  that got declared twice
  find some pattern
  name collision
  constraints??!
  mod !!
  resets
*/

Compilation message (stderr)

ballmachine.cpp: In function 'void remove(int, long long int)':
ballmachine.cpp:86:5: warning: 'tu' may be used uninitialized in this function [-Wmaybe-uninitialized]
   86 |   tu--;
      |   ~~^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...