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;
const int N = 1e5 + 15, L = 2e0 + 20;
int n, q, r, par[N][L], lst[N], st[N], ft[N], dfa[N], num[N];
bool ext[4 * N], mark[N];
vector<int> adj[N];
bool cmp(const int &a, const int &b) {
return lst[a] < lst[b];
}
void dfs1(int v, int p) {
lst[v] = v;
for (int u: adj[v])
if (u != p) {
dfs1(u, v);
lst[v] = min(lst[v], lst[u]);
}
}
void dfs2(int v, int p) {
static int time = 0;
dfa[time] = v;
st[v] = time++;
for (int u: adj[v])
if (u != p)
dfs2(u, v);
ft[v] = time;
}
void make_tree(int l, int r, int t) {
if (r - l == 1) {
ext[t] = (st[dfa[l]] == ft[dfa[l]] - 1);
return;
}
int k = (r + l + 1) / 2;
make_tree(l, k, 2 * t);
make_tree(k, r, 2 * t + 1);
ext[t] = ext[2 * t] | ext[2 * t + 1];
}
int find_place(int l, int r, int t) {
if (r - l == 1)
return dfa[l];
int k = (r + l + 1) / 2;
if (ext[2 * t])
return find_place(l, k, 2 * t);
else
return find_place(k, r, 2 * t + 1);
}
void update(int p, bool x, int l, int r, int t) {
if (r - l == 1) {
ext[t] = x;
return;
}
int k = (r + l + 1) / 2;
if (p < k)
update(p, x, l, k, 2 * t);
else
update(p, x, k, r, 2 * t + 1);
ext[t] = ext[2 * t] | ext[2 * t + 1];
}
void read_input() {
cin >> n >> q;
for (int i = 0; i < n; i++) {
cin >> par[i + 1][0];
if (par[i + 1][0] != 0)
adj[par[i + 1][0]].push_back(i + 1);
else
r = i + 1;
}
}
void solve() {
memset(lst, 63, sizeof lst);
dfs1(r, 0);
for (int i = 1; i <= n; i++)
sort(adj[i].begin(), adj[i].end(), cmp);
dfs2(r, 0);
make_tree(0, n, 1);
for (int i = 1; i < L; i++)
for (int j = 1; j <= n; j++)
par[j][i] = par[par[j][i - 1]][i - 1];
while (q--) {
int t;
cin >> t;
if (t == 1) {
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int v = find_place(0, n, 1);
update(st[v], false, 0, n, 1);
mark[v] = true;
num[par[v][0]]++;
if (num[par[v][0]] == adj[par[v][0]].size())
update(st[par[v][0]], true, 0, n, 1);
if (i == k - 1)
cout << v << '\n';
}
}
else { // update mark, ext, num
int x;
cin >> x;
int p = x, res = 0;
for (int i = L - 1; i >= 0; i--)
if (mark[par[p][i]]) {
p = par[p][i];
res += (1 << i);
}
cout << res << '\n';
mark[p] = false;
num[par[p][0]]--;
update(st[p], true, 0, n, 1);
}
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
read_input(), solve();
return cout << '\n', 0;
}
Compilation message (stderr)
ballmachine.cpp: In function 'void solve()':
ballmachine.cpp:102:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | if (num[par[v][0]] == adj[par[v][0]].size())
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |