제출 #1345755

#제출 시각아이디문제언어결과실행 시간메모리
1345755pcheloveksBall Machine (BOI13_ballmachine)C++20
38.53 / 100
265 ms40452 KiB
#define _CRT_SECURE_NO_WARNINGS

/*
⣿⡟⡡⠾⠛⠻⢿⣿⣿⣿⡿⠃⣿⡿⣿⠿⠛⠉⠠⠴⢶⡜⣦⡀⡈⢿⣿
⡿⢀⣰⡏⣼⠋⠁⢲⡌⢤⣠⣾⣷⡄⢄⠠⡶⣾⡀⠀⣸⡷⢸⡷⢹⠈⣿
⡇⢘⢿⣇⢻⣤⣠⡼⢃⣤⣾⣿⣿⣿⢌⣷⣅⡘⠻⠿⢛⣡⣿⠀⣾⢠⣿
⣷⠸⣮⣿⣷⣨⣥⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⢁⡼⠃⣼⣿
⡟⠛⠛⠛⣿⠛⠛⢻⡟⠛⠛⢿⡟⠛⠛⡿⢻⡿⠛⡛⢻⣿⠛⡟⠛⠛⢿
⡇⢸⣿⠀⣿⠀⠛⢻⡇⠸⠃⢸⡇⠛⢛⡇⠘⠃⢼⣷⡀⠃⣰⡇⠸⠇⢸
⡇⢸⣿⠀⣿⠀⠛⢻⡇⢰⣿⣿⡇⠛⠛⣇⢸⣧⠈⣟⠃⣠⣿⡇⢰⣾⣿
⣿⣿⣿⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢋⣿⠙⣷⢸⣷⠀⣿⣿⣿
⣿⣿⣿⡇⢻⣿⣿⣿⡿⠿⢿⣿⣿⣿⠟⠋⣡⡈⠻⣇⢹⣿⣿⢠⣿⣿⣿
⣿⣿⣿⣿⠘⣿⣿⣿⣿⣯⣽⣉⣿⣟⣛⠷⠙⢿⣷⣌⠀⢿⡇⣼⣿⣿⣿
⣿⣿⣿⡿⢀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣶⣤⡙⢿⢗⣀⣁⠈⢻⣿
*/

//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")

//Donald Trump pleese save this code
//Babahnineeleven will win IOI 2040
//Babahnineeleven will win IOI 2041
//Babahnineeleven will win IOI 2042
//Babahnineeleven will win IOI 2043
//Babahnineeleven will win IOI 2044
//Babahnineeleven will win IOI 2045
//Babahnineeleven will win IOI 2046
//Babahnineeleven will win IOI 2047
//Babahnineeleven will win IOI 2048
//Tanya Zadniprovska will win EGOI 2025
//Andrew Holod will NOT win IOI 2025
//Andrew Holod did not qualify to IOI 2025))))))))))
//Andrew Kholod will win ICPC WF 2026
//Andrew Pavlyk is best coder in Khmelnytski

#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <bitset>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <deque>
#include <stack>

#define endl '\n'

using namespace std;

FILE* stream;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair < int, int > pii;
typedef pair < ld, ld > pdd;
const long long DIM = 1000007;
const ld eps = 1e-12;
const long long INF = 1e18;
const long long Bsize = 450;
const int mod = 998244353;
const long long NUMSZ = 500;

class fenwickTree {
public:

    void init(int sz) {
        this->sz = sz;
        T.resize(sz + 1);
    }

    void add(int pos, int val) {
        for (int i = pos; i <= sz; i += i & -i) T[i] += val;
    }

    int sumPref(int pos) {
        int res = 0;
        for (int i = pos; i > 0; i -= i & -i) res += T[i];
        return res;
    }
    int sum(int l, int r) {
        return sumPref(r) - (l != 1 ? sumPref(l - 1) : 0);
    }

private:

    int sz;
    vector < int > T;
}t;

int n, m, q, timer;
vector < vector < int > > v;

vector < int > mi, d;

vector < set < int > > s;
set < int > ss;

vector < int > tin, tout, euler;
vector < vector < int > > j;

void precount(int val) {
    mi[val] = val;
    for (auto to : v[val]) {
        precount(to);
        mi[val] = min(mi[val], mi[to]);
    }
}

void dfs(int val, int prev) {
    tin[val] = ++timer;
    euler[timer] = val;

    d[val] = d[prev] + 1;

    j[0][val] = prev;
    for (int p = 1; p < 20; p++) {
        j[p][val] = j[p - 1][j[p - 1][val]];
    }

    auto cmp = [&](int v1, int v2) {
        return mi[v1] < mi[v2];
    };
    
    sort(v[val].begin(), v[val].end(), cmp);

    for (auto to : v[val]) {
        if (to == val) continue;
        dfs(to, val);
    }

    tout[val] = timer;
}

int sumOnRout(int v1, int v2) {
    return t.sumPref(tin[v1]) - (v2 != j[0][v2] ? t.sumPref(tin[j[0][v2]]) : 0);
}

int add() {
    int depth = *ss.rbegin();
    int val = euler[*s[depth].begin()];

    t.add(tin[val], -1);
    t.add(tout[val] + 1, +1);

    s[depth].erase(tin[val]);
    if (s[depth].size() == 0) ss.erase(depth);

    return val;
}

int del(int val) {
    int st = val;
    for (int p = 19; p >= 0; p--) {
        if (sumOnRout(val, j[p][val]) == 0) {
            val = j[p][val];
        }
    }

    t.add(tin[val], +1);
    t.add(tout[val] + 1, -1);

    s[d[val]].insert(tin[val]);
    if (s[d[val]].size() == 1) ss.insert(d[val]);

    return d[st] - d[val];
}

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

#ifdef _DEBUG
    freopen_s(&stream, "input.txt", "r", stdin);
    freopen_s(&stream, "output.txt", "w", stdout);
#endif

    cin >> n >> q;
    v.resize(n + 1);

    d.resize(n + 1);
    mi.resize(n + 1);

    tin.resize(n + 1);
    tout.resize(n + 1);
    euler.resize(n + 1);

    j.resize(20, vector < int >(n + 1));

    vector < int > par(n + 1, 0);

    for (int i = 1; i <= n; i++) {
        cin >> par[i];
    }

    int root = -1;
    for (int i = 1; i <= n; i++) {
        if (par[i] == 0) root = i;
        else v[par[i]].push_back(i);
    }

    timer = 0;

    d[root] = 0;

    precount(root);
    dfs(root, root);

    t.init(timer + 1);

    for (int i = 1; i <= n; i++) {
        t.add(tin[i], +1);
        t.add(tout[i] + 1, -1);
    }

    s.resize(n + 1);
    for (int i = 1; i <= n; i++) {
        s[d[i]].insert(tin[i]);
    }
    for (int i = 1; i <= n; i++) {
        if (s[i].size() > 0) ss.insert(i);
    }

    for (int i = 1; i <= q; i++) {
        int type, val;
        cin >> type >> val;

        if (type == 1) {
            for (int j = 1; j < val; j++) {
                add();
            }
            cout << add() << endl;
        }
        if (type == 2) {
            cout << del(val) << endl;
        }
    }

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