제출 #666890

#제출 시각아이디문제언어결과실행 시간메모리
666890Hacv16Ball Machine (BOI13_ballmachine)C++17
0 / 100
1091 ms468 KiB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MAX = 450;
const int LOG = 21;
const int INF = 0x3f3f3f3f;

int n, q, filled[MAX], root, t, order[MAX], dp[MAX][LOG], d[MAX];
vector<int> adj[MAX];
set<pair<int, int>> s;

void dfs(int u, int p, int dist){
    d[u] = dist;

    for(auto v : adj[u]){
        if(v == p) continue;
        dfs(v, u, dist + 1);
    }

    order[u] = ++t;
    s.insert({order[u], u});
}

ll query(ll u){
    int d1 = d[u];

    for(int i = LOG - 1; i >= 0; i--){
        int j = dp[u][i];
        if(filled[j]) u = j;
    }

    return d1 - d[u];
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> q;

    memset(dp, -1, sizeof(dp));

    for(int i = 1; i <= n; i++){
        int x; cin >> x;

        if(x != 0){
            adj[x].push_back(i);
            adj[i].push_back(x);
            dp[i][0] = x;

        }else{ root = i; }
    }

    for(int i = 1; i <= n; i++)
        sort(adj[i].begin(), adj[i].end());

    dfs(root, -1, 0);

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

    while(q--){
        int op, k; cin >> op >> k;

        if(op == 1){
            int last = -1;

            while(k--){
                pair<int, int> b = *(s.begin());
                int cur = b.second;
                filled[cur] = true;
                last = cur;

                s.erase(s.begin());
            }

            cout << last << '\n';

        }else{
            s.insert({order[k], k});
            filled[k] = false;

            cout << query(k) << '\n';
        }
    }

    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...