Submission #480294

#TimeUsernameProblemLanguageResultExecution timeMemory
480294LoboBall Machine (BOI13_ballmachine)C++17
16.23 / 100
560 ms29480 KiB
#include <bits/stdc++.h>
 
using namespace std;

const long long INFll = (long long) 1e18 + 10;
const int INFii = (int) 1e9 + 10;
typedef long long ll;
typedef int ii;
typedef long double dbl;
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

#define maxn 110000

ii n, q, a[maxn], mn[maxn], ant[maxn], timer, p[maxn][20], h[maxn];
vector<ii> g[maxn];

bool cmp(ii a, ii b) {
    return mn[a] < mn[b];
}

void dfsmn(ii u, ii ant) {
    p[u][0] = ant;
    for(ii i = 1; i <= 20; i++) {
        p[u][i] = p[p[u][i-1]][i-1];
    }

    for(auto v : g[u]) {
        h[v] = h[u]+1;
        dfsmn(v,u);
        mn[u] = min(mn[u],mn[v]);
    }
}

void dfs(ii u, ii ant) {
    for(auto v : g[u]) {
        dfs(v,u);
    }

    a[u] = ++timer;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);

    //freopen("in.in", "r", stdin);
    //freopen("out.out", "w", stdout);

    cin >> n >> q;

    ii root;
    for(ii i = 1; i <= n; i++) {
        mn[i] = i;
        ii v;
        cin >> v;

        if(v == 0) root = i;
        else g[v].pb(i);
    }

    dfsmn(root,root);

    for(ii i = 1; i <= n; i++) {
        sort(all(g[i]),cmp);
    }

    dfs(root,root);

    set<pair<ii,ii>> s;
    for(ii i = 1; i <= n; i++) {
        s.insert(mp(a[i],i));
    }


    for(ii i = 1; i <= q; i++) {
        ii op;
        cin >> op;
        if(op == 1) {
            ii k; cin >> k;

            ii ans;
            while(k--) {
                ans = s.begin()->sc;
                s.erase(*s.begin());
            }

            cout << ans << endl;
        }
        else {
            ii u; cin >> u;
            ii ans = h[u];
            for(ii i = 20; i >= 0; i--) {
                ii v = p[u][i];
                //cout << i << " " << u << " " <<v << endl;
                if(s.find(mp(a[v],v)) == s.end()) {
                    u = v;
                }
            }

            ans-= h[u];

            s.insert(mp(a[u],u));
            cout << ans << endl;
        }
    }
    

}

Compilation message (stderr)

ballmachine.cpp: In function 'void dfsmn(ii, ii)':
ballmachine.cpp:29:17: warning: iteration 19 invokes undefined behavior [-Waggressive-loop-optimizations]
   29 |         p[u][i] = p[p[u][i-1]][i-1];
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~
ballmachine.cpp:28:21: note: within this loop
   28 |     for(ii i = 1; i <= 20; i++) {
      |                   ~~^~~~~
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:10:14: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
   10 | #define endl '\n'
      |              ^~~~
ballmachine.cpp:85:16: note: 'ans' was declared here
   85 |             ii ans;
      |                ^~~
ballmachine.cpp:71:8: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
   71 |     dfs(root,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...