Submission #850152

#TimeUsernameProblemLanguageResultExecution timeMemory
850152NK_Ball Machine (BOI13_ballmachine)C++17
0 / 100
260 ms28644 KiB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
 
using namespace std;
 
#define nl '\n'
#define pb push_back
#define pf push_front
 
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
 
template<class T> using V = vector<T>;
using pi = pair<int, int>;
using vi = V<int>;
using vpi = V<pi>;
 
using ll = long long;
using pl = pair<ll, ll>;
using vpl = V<pl>;
using vl = V<ll>;
 
using db = long double;
 
template<class T> using pq = priority_queue<T, V<T>, greater<T>>;
 
const int MOD = 1e9 + 7;
const ll INFL = ll(1e17);
const int LG = 18;

int main() {
	cin.tie(0)->sync_with_stdio(0);

	int N, Q; cin >> N >> Q;

	int R = -1;
	vi P(N);
	V<vi> chd(N); for(int i = 0; i < N; i++) {
		int p; cin >> p; --p;
		if (p != -1) chd[p].pb(i), P[i] = p;
		else R = i, P[i] = -1;
	}

	vi mn(N, MOD), dep(N);
	V<vi> up(N+1, vi(LG));
	function<void(int)> gen = [&](int u) {
		for(int i = 1; i < LG; i++) up[u][i] = up[up[u][i-1]][i-1];

		mn[u] = u;
		for(auto& v : chd[u]) {
			up[v][0] = u; dep[v] = dep[u] + 1; gen(v);
			mn[u] = min(mn[v], mn[u]);
		}

	};

	up[R][0] = N, dep[R] = 0; gen(R);

	vi deg(N); for(int i = 0; i < N; i++) deg[i] = sz(chd[i]);

	vi ord(N);

	pq<pi> q; for(int i = 0; i < N; i++) if (deg[i] == 0) {
		q.push(mp(mn[i], i));
	}

	int cur = 0;
	while(sz(q)) {
		int u = q.top().s; q.pop();

		// cout << u << " " << cur << endl;
		ord[u] = cur++;

		if (P[u] != -1) {
			// cout << P[u] << endl;
			// cout << deg[P[u]] << endl;
			deg[P[u]]--; 
			if (deg[P[u]] == 0) q.push(mp(mn[P[u]], P[u]));
		}
	}
 		
 	pq<pi> W; for(int i = 0; i < N; i++) W.push(mp(ord[i], i));

 	vi C(N+1); // C[i] = 0 (off) / 1 (on)
 	for(int t = 0; t < Q; t++) {
 		int type; cin >> type;
 		if (type == 1) {
 			int k; cin >> k;
 			int lst = -1;
 			while(k--) {
 				assert(sz(W));
 				int u = W.top().s; W.pop();
 				C[u] = 1; lst = u; 
 			}
 			cout << lst + 1 << nl;
 		} else {
 			int u; cin >> u; --u;
 			int ans = 0;

 			for(int i = LG-1; i >= 0; i--) {
 				if (C[up[u][i]]) {
 					u = up[u][i]; ans += (1 << i);
 					// assert(false);
 				}
 			}

 			C[u] = 0; if (u != N) W.push(mp(ord[u], u));
 			cout << ans << nl;
 		}
 	}

 	for(int i = 0; i < N; i++) cout << i << " " << C[i] << endl;


	exit(0-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...