Submission #1125602

#TimeUsernameProblemLanguageResultExecution timeMemory
1125602KasymKBall Machine (BOI13_ballmachine)C++20
100 / 100
126 ms21720 KiB
#include "bits/stdc++.h"
using namespace std;
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define tr(i, c) for(auto i = c.begin(); i != c.end(); ++i)
#define wr puts("----------------")
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int N = 1e5+5;
const int B = 22;
vector<int> adj[N];
int sp[N][B], ad, A[N], mal[N], d[N], mn[N];
bool belki[N];
priority_queue<int, vector<int>, greater<int>> pq;

void dfs(int x){
    mn[x]=x;
    tr(it, adj[x]){
        d[*it]=d[x]+1;
        dfs(*it);
        umin(mn[x], mn[*it]);
    }
}

void dfs2(int x){
    tr(it, adj[x])
        dfs2(*it);
    mal[x]=ad;
    A[ad]=x;
    pq.push(ad);
    ad++;
}

int bok(int x){
    for(int i = B-1; i >= 0; --i)
        if(belki[sp[x][i]])
            x=sp[x][i];
    return x;
}

int main(){
    // freopen("file.txt", "r", stdin);
    int n, q;
    scanf("%d%d", &n, &q);
    int kok;
    for(int i = 1; i <= n; ++i){
        int x;
        scanf("%d", &x);
        sp[i][0]=x;
        if(x==0)
            kok=i;
        else
            adj[x].pb(i);
    }
    dfs(kok);
    for(int j = 1; j < B; ++j)
        for(int i = 1; i <= n; ++i)
            sp[i][j]=sp[sp[i][j-1]][j-1];
    auto cmp = [&](int i, int j) -> bool {
        return mn[i]<mn[j];
    };
    for(int i = 1; i <= n; ++i)
        sort(all(adj[i]), cmp);
    dfs2(kok);
    while(q--){
        int op, x;
        scanf("%d%d", &op, &x);
        if(op==1){
            int answer=0;
            while(x--){
                int ii = pq.top();
                answer=A[ii];
                belki[answer]=1;
                pq.pop();
            }
            printf("%d\n", answer);
            continue;
        }
        int samsyk = bok(x);
        belki[samsyk]=0;
        pq.push(mal[samsyk]);
        printf("%d\n", d[x]-d[samsyk]);
    }
    return 0;
}

Compilation message (stderr)

ballmachine.cpp: In function 'int main()':
ballmachine.cpp:50:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |     scanf("%d%d", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~
ballmachine.cpp:54:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 |         scanf("%d", &x);
      |         ~~~~~^~~~~~~~~~
ballmachine.cpp:73:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |         scanf("%d%d", &op, &x);
      |         ~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...