Submission #731764

#TimeUsernameProblemLanguageResultExecution timeMemory
731764n0sk1llBall Machine (BOI13_ballmachine)C++17
100 / 100
160 ms25164 KiB
#include <bits/stdc++.h>

#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define mp make_pair
#define xx first
#define yy second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(x) x.begin(),x.end()
#define ff(i,a,b) for (int i = a; i < b; i++)
#define fff(i,a,b) for (int i = a; i <= b; i++)
#define bff(i,a,b) for (int i = b-1; i >= a; i--)
#define bfff(i,a,b) for (int i = b; i >= a; i--)

using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int,int> typedef pii;
pair<li,li> typedef pli;
pair<ld,ld> typedef pld;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
//const int mod = 998244353;
const int mod = 1000000007;







//Note to self: Check for overflow

int rootcina=-1;

vector<vector<pii>> g(100005);
int mk[100005]; //minimalni node u subtree-u
int tin[100005];
int kad[100005];
int up[20][100005];
int VREME=0;

void cringedfs(int p, int q)
{
    mk[p]=p;
    ff(i,0,(int)g[p].size()) if (g[p][i].yy!=q)
    {
        cringedfs(g[p][i].yy,p);
        mk[p]=min(mk[p],mk[g[p][i].yy]);
        g[p][i].xx=mk[g[p][i].yy];
    }
}

void dfs(int p, int q)
{
    for (auto [_,it] : g[p]) if (it!=q) dfs(it,p);
    tin[p]=++VREME,kad[VREME]=p,up[0][p]=q;
}

void build(int n)
{
    ff(k,1,20) fff(i,1,n) up[k][i]=up[k-1][up[k-1][i]];
}

bool ball[100005];
priority_queue<int,vector<int>,greater<int>> nonballs; //zprv ubacujemo vremena u ovaj set

int main()
{
    FAST;

    int n,q; cin>>n>>q;
    fff(i,1,n)
    {
        int p; cin>>p;
        if (p) g[p].pb({-1,i});
        else rootcina=i;
    }

    cringedfs(rootcina,0);
    fff(i,1,n) sort(all(g[i]));

    dfs(rootcina,0);
    build(n);

    fff(i,1,n) nonballs.push(tin[i]);

    while (q--)
    {
        int t,x; cin>>t>>x;
        if (t==1)
        {
            int lst;
            while (x--)
            {
                auto v=nonballs.top();
                nonballs.pop();
                lst=kad[v];
                ball[lst]=true;
            }
            cout<<lst<<"\n";
        }
        else
        {
            int kolko=0;
            bff(k,0,20) if (ball[up[k][x]]) x=up[k][x],kolko+=(1<<k);
            ball[x]=false,nonballs.push(tin[x]);
            cout<<kolko<<"\n";
        }
    }
}

//Note to self: Check for overflow

Compilation message (stderr)

ballmachine.cpp: In function 'int main()':
ballmachine.cpp:104:24: warning: 'lst' may be used uninitialized in this function [-Wmaybe-uninitialized]
  104 |             cout<<lst<<"\n";
      |                        ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...