Submission #1184448

#TimeUsernameProblemLanguageResultExecution timeMemory
1184448knhatdevBall Machine (BOI13_ballmachine)C++20
100 / 100
211 ms36916 KiB
#include<bits/stdc++.h>
#define int long long
#define ii pair<int,int>
#define fi first
#define se second
#define task ""
using namespace std;

const int N = 1e5 + 5, mod = 1e9 + 7;
int n, q, root, up[N][25], mi[N];
int id[N], pos[N], timer;
vector<int> g[N];

int bit[N];
void update(int u, int val){
    for(; u <= n; u = u | (u + 1))
        bit[u] += val;
}
int get(int u){
    int res = 0;
    for(; u >= 0; u = (u & (u + 1)) - 1)
        res += bit[u];
    return res;
}

void dfs(int u){
    mi[u] = u;
    for(int v : g[u]){
        dfs(v);
        up[v][0] = u;
        mi[u] = min(mi[u], mi[v]);
    }
    sort(g[u].begin(), g[u].end(), [](int x, int y){
        return mi[x] < mi[y];
    });
}
void dfs2(int u){
    for(int v : g[u])
        dfs2(v);
    id[u] = ++timer;
    pos[timer] = u;
}

main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if(fopen(task ".inp", "r")){
        freopen(task ".inp", "r", stdin);
        freopen(task ".out", "w", stdout);
    }
    cin >> n >> q;
    for(int i = 1; i <= n; i++){
        int u; cin >> u;
        if(u == 0) root = i;
        else g[u].push_back(i);
    }
    dfs(root);
    dfs2(root);
    for(int j = 1; j <= 20; j++){
        for(int i = 1; i <= n; i++){
            up[i][j] = up[up[i][j - 1]][j - 1];
        }
    }
    for(int i = 1; i <= n; i++)
        update(i, -1);
    while(q--){
        int type, x; cin >> type >> x;
        int ans = 0;
        if(type == 1){
            for(int i = 1; i <= x; i++){
                int l = 0, r = n;
                while(l <= r){
                    int mid = l + r >> 1;
                    if(get(mid) < 0){
                        r = mid - 1;
                        ans = mid;
                    } else {
                        l = mid + 1;
                    }
                }
                update(ans, 1);
            }
            cout << pos[ans] << '\n';
        } else {
            for(int j = 20; j >= 0; j--){
                if(up[x][j] && get(id[up[x][j]]) == get(id[up[x][j]] - 1)){
                    ans += (1 << j);
                    x = up[x][j];
                }
            }
            update(id[x], -1);
            cout << ans << '\n';
        }
    }
}

Compilation message (stderr)

ballmachine.cpp:44:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   44 | main(){
      | ^~~~
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:47:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |         freopen(task ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp:48:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         freopen(task ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...