Submission #726736

#TimeUsernameProblemLanguageResultExecution timeMemory
726736TB_Ball Machine (BOI13_ballmachine)C++17
8.57 / 100
1097 ms49544 KiB
#include <bits/stdc++.h>
using namespace std;

// #undef _GLIBCXX_DEBUG                // disable run-time bound checking, etc
// #pragma GCC optimize("Ofast,inline") // Ofast = O3,fast-math,allow-store-data-races,no-protect-parens
// #pragma GCC optimize ("unroll-loops")

// #pragma GCC target("bmi,bmi2,lzcnt,popcnt")                      // bit manipulation
// #pragma GCC target("movbe")                                      // byte swap
// #pragma GCC target("aes,pclmul,rdrnd")                           // encryption
// #pragma GCC target("avx,avx2,f16c,fma,sse3,ssse3,sse4.1,sse4.2") // SIMD

// #include <bits/extc++.h>
// using namespace __gnu_pbds;
// template<class T>using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
// template<class T>using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define ll long long
#define INF (ll)1e9+7
#define fo(i, n) for(int i=0;i<(int)n;i++)
#define Fo(i, k, n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define deb(x) cout << #x << " = " << x << endl;
#define deb2(x, y) cout << #x << " = " << x << ", " << #y << " = " << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define LSOne(S) ((S) & (-S))
#define sp(x, y) fixed<<setprecision(y)<<x
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define clr(x) memset(x, 0, sizeof(x))
#define tr(it, x) for(auto it = x.begin(); it != x.end(); it++)
#define trr(it, x) for(auto it = x.rbegin(); it != x.rend(); it+)
inline int readint(){ int v = 0; char c; while((c = getchar()) != EOF && c != ' ' && c != '\n'){ v *= 10; v += c - '0'; } return v; }
inline int readintsigned() { int v = 0; int sign = 1; char c = getchar(); if (c == '-') { sign = -1; } else { v += c - '0'; } while ((c = getchar()) != EOF && c != ' ' && c != '\n') { v *= 10; v += c - '0'; } return v * sign; }
inline string readstring() { string s; char c; while ((c = getchar()) != EOF && c != '\n' && c != ' ') { s.push_back(c); } return s; }
template <class result_t=std::chrono::milliseconds,class clock_t=std::chrono::steady_clock,class duration_t = std::chrono::milliseconds>
auto since(std::chrono::time_point<clock_t, duration_t> const& start){return std::chrono::duration_cast<result_t>(clock_t::now() - start);}
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<vpii> vvpii;
typedef vector<vpl> vvpl;

vvpii adj(100001);
vector<set<pii>> adj2(100001);
map<int, pii> rem;
vi amount(100001, 0);

int dfsSort(int u, int last){
    int best = u;
    for(auto v : adj[u]){
        if(last == v.S) continue;
        v.F = dfsSort(v.S, u);
        best = max(best, v.F);
        adj2[u].insert(v);
    }
    sort(all(adj[u]));
    return best;
}

int dfsDis(int u, int last){
    int best = u;
    for(auto v : adj[u]){
        if(last == v.S) continue;
        v.F = dfsSort(v.S, u);
        best = max(best, v.F);
        adj2[u].insert(v);
    }
    sort(all(adj[u]));
    return best;
}

int dfs(int pos, int &balls){
    if(amount[pos] || !balls) return -1;
    int ans = -1;
    vpii toRemove;
    for(auto v : adj2[pos]){
        if(!balls) break;
        int val = dfs(v.S, balls);
        if(val != -1) ans = val;
        if(amount[v.S]) toRemove.pb(v);
    }
    fo(i, toRemove.size()){
        adj2[pos].erase(toRemove[i]);
        rem[toRemove[i].S] = {toRemove[i].F, pos};
    }
    if(balls){
        balls--;
        amount[pos] = 1;
        ans = pos;
    }
    return ans;
}

int main() {
    // cout << fixed << setprecision(20);
    // auto start = std::chrono::steady_clock::now(); // since(start).count()
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit); // remove if to endof file

    int n, q, to, typ;
    cin >> n >> q;
    int root = -1;
    vvi bl(n, vi(30, -1));
    fo(i, n){
        cin >> to;
        bl[i][0] = to-1;
        if(!to) {
            adj[i].pb({INF, i});
            root = i;
            bl[i][0] = i;
        }
        else adj[to-1].pb({0, i});
    }
    dfsSort(root, root);

    fo(i, 25){
        fo(j, n){
            bl[j][i+1] = bl[bl[j][i]][i];
        }
    }

    fo(i, q){
        cin >> typ >> to;
        if(typ == 1){
            cout << dfs(root, to)+1 << "\n";
        }else{
            int current = to-1;
            int ans = 0;
            bool isRoot = false;
            for(int bit = 23; bit>=0; bit--){
                // deb(bl[current][bit]);
                if(amount[bl[current][bit]]) {
                    if(bl[current][bit] == root) {
                        isRoot = true;
                        continue;
                    }
                    current = bl[current][bit];
                    ans += (1<<bit);
                }
            }
            // deb(current);
            if(isRoot) current = bl[current][0];
            amount[current] = 0;
            if(rem.count(current)){
                adj2[rem[current].S].insert({rem[current].F, current});
                rem.erase(current);
            }
            cout << ans+isRoot << "\n";
        }
        // fo(j, n) deb(amount[j]);
    }


    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...