제출 #1328322

#제출 시각아이디문제언어결과실행 시간메모리
1328322paronmanukyanBall Machine (BOI13_ballmachine)C++20
100 / 100
180 ms34140 KiB
#include <bits/stdc++.h>
#define _CRT_SECURE_NO_WARNINGS
using namespace std;

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define sort_uniq(x) sort(all(x)), uniq(x);
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define V vector
#define V2dll V<V<ll>>
#define V2dint V<V<int>>
#define V2dchar V<V<char>>
#define V2dbool V<V<bool>>
#define V3dll V<V<V<ll>>>
#define V3dint V<V<V<int>>>
#define V3dchar V<V<V<char>>>
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define eb emplace_back
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define INF INT32_MAX
#define blt __builtin_popcount
#define clr(x) x.clear()
#define ff first
#define ss second
#define popf pop_front
#define popb pop_back
#define sz(x) int(x.size())
#define rep(a, b, c, d) for (int a = b; a <= c; a += d)
#define repl(a, b, c, d) for (int a = b; a >= c; a -= d)

mt19937_64 rng(chrono::steady_clock().now().time_since_epoch().count());

const int LG = 18;
const int N = 1e5 + 5;

int root, n;
V<int> adj[N];
int up[N][LG + 1];
int subt[N], pos[N], d[N];
bool ng[N];
V<int> order;

void dfs(int node, int p) {
    subt[node] = node;
    for (auto i : adj[node]) if (i != p) {
        dfs(i, node);   // FIXED
        subt[node] = min(subt[node], subt[i]);
    }

    V<pii> cur;
    for (auto i : adj[node]) if (i != p)
        cur.pb({subt[i], i});

    sort(all(cur));

    V<int> tmp;
    for (auto [_, v] : cur)
        tmp.pb(v);

    if (node != root)
        tmp.pb(p);

    adj[node] = tmp;   
}

void dfs_order(int node, int p) {
    for (auto i : adj[node]) if (i != p)
        dfs_order(i, node);   
    order.pb(node);
}

void dfs_blift(int node, int p) {
    up[node][0] = p;
    rep(i, 1, LG, 1)
        up[node][i] = up[up[node][i - 1]][i - 1];

    for (auto i : adj[node]) if (i != p)
        d[i] = d[node] + 1, dfs_blift(i, node);   
}

int main()
{
    FASTIO
    int q; 
    cin >> n >> q;
    rep(i, 1, n, 1) {
        int x; cin >> x;
        if (x == 0)
            root = i;
        else {
            adj[i].pb(x);
            adj[x].pb(i);
        }
    }

    order = {-1};

    dfs(root, root);
    dfs_order(root, root);
    d[root] = 1;
    dfs_blift(root, root);

    rep(i, 1, n, 1)
        pos[order[i]] = i;

    set<pii> black, white;

    rep(i, 1, n, 1) {
        white.insert({pos[i], i});
        ng[i] = false;
        //cout << d[i] << " ";
    }

    while (q--) {
        int t; cin >> t;

        if (t == 1) {
            int k; cin >> k;
            while (k--) {
                auto it = white.begin();
                auto [bs, x] = *it;

                ng[x] = true;
                white.erase(it);
                black.insert({bs, x});   

                if (k == 0)
                    cout << x << "\n";
            }
        }
        else {
            int node; cin >> node;
            int ans = d[node];

            repl(i, LG, 0, 1) {
                if (ng[up[node][i]]) {
                    node = up[node][i];
                }
            }
            //cout << "!!!!!!!" << node << endl;
            ans -= d[node];
            cout << ans << "\n";

            ng[node] = false;
            white.insert({pos[node], node});
            black.erase({pos[node], node});
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...