This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
///////////////////////////////////////////////////////////////////////////////////////////////
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define FILE_IO(x) freopen((string(x) + ".in").c_str(), "r", stdin); freopen((string(x) + ".out").c_str(), "w", stdout)
#define f first
#define s second
#define x1 x1qwer
#define y1 y1qwer
#define right right123
#define left left123
#define foreach(it, v) for (auto it : v)
#define rep(it, n) for (int it = 0; it < n; ++it)
#define forin(it, l, r) for (int it = l; it < r; ++it)
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const double DINF = numeric_limits<double>::infinity();
const ll MOD = 1e9 + 7;
const double EPS = 1e-7;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
mt19937 mmtw_(MOD);
uniform_int_distribution<ll> rd_;
ll randomll() { return rd_(mmtw_);}
ll rnd(ll x, ll y) { return rd_(mmtw_) % (y - x + 1) + x; }
////////////////////////////////////////////////////////////////////////////////////////////////
const int MAXN = 1e5 + 5;
int n, q;
vector<int> g[MAXN];
int root;
int up[MAXN][20];
set<pair<int, int>> s;
int mn[MAXN];
int tin[MAXN];
bool used[MAXN];
int h[MAXN];
void dfsm(int v) {
mn[v] = v;
for (auto to : g[v]) {
h[to] = h[v] + 1;
dfsm(to);
mn[v] = min(mn[v], mn[to]);
}
sort(all(g[v]), [&](int a, int b){
return mn[a] < mn[b];
});
}
int tt = 0;
void dfs(int v) {
for (int i = 1; i < 20; ++i) {
up[v][i] = up[up[v][i - 1]][i - 1];
}
for (auto to : g[v]) {
dfs(to);
}
tin[v] = tt++;
}
int main() {
FAST_IO;
cin >> n >> q;
rep(i, n) {
int p;
cin >> p;
p--;
if (p == -1) {
root = i;
} else {
g[p].push_back(i);
up[i][0] = p;
}
}
dfsm(root);
dfs(root);
rep(i, n) {
s.insert({tin[i], i});
}
// for (auto &it : s) {
// cout << it.f << ' ' << it.s << endl;
// }
for (int i = 0; i < q; ++i) {
int t;
cin >> t;
if (t == 1) {
int k, r = -1;
cin >> k;
rep(j, k) {
used[s.begin()->s] = true;
r = s.begin()->s;
s.erase(s.begin());
}
cout << r + 1 << '\n';
} else {
int v, r = 0;
cin >> v;
v--;
int w = v;
for (int i = 19; i >= 0; --i) {
if (used[up[v][i]]) {
v = up[v][i];
}
}
used[v] = false;
s.insert({tin[v], v});
cout << h[w] - h[v] << '\n';
}
}
}
Compilation message (stderr)
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:110:20: warning: unused variable 'r' [-Wunused-variable]
int v, r = 0;
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |