Submission #266330

#TimeUsernameProblemLanguageResultExecution timeMemory
266330HeheheBall Machine (BOI13_ballmachine)C++14
100 / 100
284 ms30584 KiB
#include<bits/stdc++.h> //:3
using namespace std;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define sz(x) (int)((x).size())
//#define int long long

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

const ll inf = 2e9;
const ll mod = 1e9 + 7;
const int N = 2e5 + 11;
const ll INF64 = 3e18 + 1;
const double eps = 1e-14;
const double PI = acos(-1);

//ifstream in(".in");
//ofstream out(".out");

int n, dp[N][25], root, mn[N], cnt[N], C, q, viz[N];
vector<int>v[N];

int dfs(int nod){
	int MN = inf;

	for(int it : v[nod]){
		MN = min(dfs(it), MN);
	}

	mn[nod] = min(MN, nod);
	return mn[nod];
}

void dfs2(int nod){
    for(auto it : v[nod]){
        dfs2(it);
    }
    cnt[nod] = C++;
}

void solve(){

    cin >> n >> q;

    for(int i = 1; i <= n; i++){
        cin >> dp[i][0];
        v[dp[i][0]].push_back(i);
        if(dp[i][0] == 0)root = i;
    }

    dfs(root);

    for(int i = 1; i <= n; i++){
        sort(all(v[i]), [](int l, int r){
             return (mn[l] < mn[r]);
        });
    }

    dfs2(root);

    for(int i = 1; i <= 20; i++){
        for(int j = 1; j <= n; j++){
            dp[j][i] = dp[dp[j][i - 1]][i - 1];
        }
    }

    set<pi>s;

    s.insert({inf, 0});
	for(int i = 1; i <= n; i++) s.insert({cnt[i], i});

	while(q--){
        int type, x;
        cin >> type >> x;
        if(type == 1){
            for(int i = 1; i <= x; i++){
                if(i == x)cout << (*s.begin()).ss << '\n';
                viz[(*s.begin()).ss] = 1;
                s.erase(s.begin());
            }
        }else{
            int ans = 0;
            for(int i = 20; i >= 0; i--){
                int p = dp[x][i];
                if(p && viz[p])x = p, ans += (1 << i);
            }
            cout << ans << '\n';
            s.insert({cnt[x], x});
            viz[x] = 0;
        }
	}
}

int32_t main(){
ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);

    //cout << setprecision(6) << fixed;

    int T = 1;
    //cin >> T;
    while(T--){
        solve();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...