Submission #1047965

#TimeUsernameProblemLanguageResultExecution timeMemory
1047965ortsacBall Machine (BOI13_ballmachine)C++17
16.11 / 100
1099 ms32856 KiB
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e5 + 10;
const int MAXLOG = 18;
int cont = 0, n, q;
int dad[MAXN];
int binl[MAXLOG][MAXN];
vector<int> adj[MAXN];
int minin[MAXN];
int ocupado[MAXN];
int h[MAXN];

void dfs(int node) {
    if (adj[node].size() == 0) minin[node] = node;
    else minin[node] = 0x3f3f3f3f;
    for (auto u : adj[node]) {
        h[u] = h[node] + 1;
        dfs(u);
        minin[node] = min(minin[node], minin[u]);
    }
}

int calch(int x) {
    if (!ocupado[x]) return -1;
    for (int i = MAXLOG - 1; i >= 0; i--) {
        int maybe = binl[i][x];
        if (ocupado[maybe]) x = maybe; 
    }
    return x;
}

bool comp(int a, int b) {
    return minin[a] < minin[b];
}

vector<int> filhos[MAXN];
int order[MAXN];

int t = 0;

void go(int node) {
    for (auto u : filhos[node]) {
        go(u);
    }
    order[node] = ++t;
}

struct Node {
    int po;
};

bool operator <(const Node &a, const Node &b) {
    return order[a.po] < order[b.po];
}

Node gen(int x) {
    Node k;
    k.po = x;
    return k;
}

set<Node> ops;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> q;
    int root;
    for (int i = 1; i <= n; i++) {
        cin >> dad[i];
        adj[dad[i]].push_back(i);
        filhos[dad[i]].push_back(i);
        if (dad[i] == 0) root = i;
    }
    for (int i = 1; i <= n; i++) {
        sort(filhos[i].begin(), filhos[i].end(), comp);
    }
    dfs(root);
    go(root);
    for (int i = 1; i <= n; i++) ops.insert(gen(i));
    // lets calc binlift lesgooo
    for (int i = 1; i <= n; i++) {
        binl[0][i] = dad[i];
    }
    for (int j = 1; j < MAXLOG; j++) {
        for (int i = 1; i <= n; i++) {
            binl[j][i] = binl[j - 1][binl[j - 1][i]];
        }
    }
    // done bitches
    while (q--) {
        int t, x;
        cin >> t >> x;
        if (t == 2) {
            // beautiful
            int k = calch(x);
            cout << h[x] - h[k] << "\n";
            ocupado[k] = 0;
            ops.insert(gen(k));
            continue;
        }
        // insert x into 1
        int lst;
        while (x--) {
            int add = (*ops.begin()).po;
            ops.erase(ops.begin());
            lst = add;
            ocupado[add] = 1;
        }
        cout << lst << "\n";
    }
}

Compilation message (stderr)

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