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>
#include <sys/ucontext.h>
#define int long long
using namespace std;
namespace std {
template <typename T> ostream &operator<<(ostream &out, const vector<T> &vec) {
out << "[";
for (int i = 0; i < (int)vec.size(); ++i) {
out << vec[i];
if (i + 1 < (int)vec.size())
out << ", ";
}
return out << "]";
}
} // namespace std
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << H;
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
const int MAXN = 1e5;
const int MAXP = 20;
vector<int> adj[MAXN];
int par[MAXP][MAXN];
int Time;
int order[MAXN];
int depth[MAXN];
int smallestSub[MAXN];
void init(int u, int p) {
smallestSub[u] = u;
for (int v : adj[u])
if (v != p) {
init(v, u);
smallestSub[u] = min(smallestSub[u], smallestSub[v]);
}
}
void dfs(int u, int p) {
par[0][u] = p;
for (int i = 0; i < MAXP - 1; ++i)
par[i + 1][u] = par[i][par[i][u]];
sort(adj[u].begin(), adj[u].end(),
[&](int s, int t) { return smallestSub[s] < smallestSub[t]; });
for (int v : adj[u]) {
depth[v] = 1 + depth[u];
dfs(v, u);
}
order[u] = Time++;
}
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int nbSommet, nbRequetes;
cin >> nbSommet >> nbRequetes;
int root = -1;
for (int i = 0; i < nbSommet; ++i) {
int p;
cin >> p;
--p;
if (p != -1)
adj[p].push_back(i);
else
root = i;
}
init(root, root);
dfs(root, root);
auto compare = [&](int u, int v) { return order[u] > order[v]; };
vector<bool> isFilled(nbSommet);
priority_queue<int, vector<int>, decltype(compare)> pq(compare);
for (int u = 0; u < nbSommet; ++u)
pq.push(u);
for (int iRequete = 0; iRequete < nbRequetes; ++iRequete) {
int t, x;
cin >> t >> x;
if (t == 1) {
int u = -1;
for (int i = 0; i < x; ++i) {
u = pq.top();
pq.pop();
isFilled[u] = true;
}
cout << u + 1 << '\n';
} else {
--x;
assert(isFilled[x]);
int u = x;
for (int p = MAXP - 1; p >= 0; --p)
if (isFilled[par[p][x]])
x = par[p][x];
isFilled[x] = false;
pq.emplace(x);
cout << depth[u] - depth[x] << '\n';
}
}
}
# | 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... |