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;
typedef long long ll;
const int N = 100010, INF = 1000000000;
vector<int> adj[N];
vector<int> pref(N, INF), dist(N, INF);
vector<int> order, lca;
int lg = 1;
void dfs(int n, int par){
sort(adj[n].begin(), adj[n].end(), [](int a, int b){return pref[a] < pref[b];});
if(par != -1)dist[n] = dist[par] + 1;
lg = max(lg, dist[n]);
for(int c : adj[n]){
if(c != par){
dfs(c, n);
}
}
order.push_back(n);
}
template <class S>
struct segment {
public :
explicit segment(int _n) : n(_n) {
log = 1;
while((1<<log) < n)++log;
size = 1 << log;
d.resize(2*size);
for(int i=0; i<n; i++)d[i + size] = S(0, 1, i);
for(int i=size - 1; i >= 1; i--)update(i);
}
S get(int p){
assert(0 <= p && p < n);
return d[p + size];
}
void set(int p, S v){
assert(0 <= p && p < n);
p += size;
d[p] = v;
for(int i=1; i<=log; i++)update(p>>i);
}
S get(int l, int r){
assert(0 <= l && l <= r && r <= n);
if(l == r)return S();
l += size;
r += size;
S sml = S(), smr = S();
while(l < r){
if(l&1) sml = sml + d[l++];
if(r&1) smr = d[--r] + smr;
l >>= 1;
r >>= 1;
}
return sml + smr;
}
private :
int n, log, size;
vector<S> d;
void update(int p){ d[p] = d[2*p] + d[2*p + 1]; }
};
struct node {
int sm, sz, fi;
explicit node(int s = 0,int l = 0,int f = INF) : sm(s), sz(l), fi(f) {};
node operator +(const node &other){
node res;
res.sm = sm + other.sm;
res.sz = sz + other.sz;
res.fi = min(fi, other.fi);
return res;
}
};
int main(){
ios_base :: sync_with_stdio(false);
cin.tie(nullptr);
int n, m, root;
cin >> n >> m;
vector<int> p(n);
for(int i=0; i<n; i++){
cin >> p[i];
--p[i];
if(p[i] != -1){
adj[i].push_back(p[i]);
adj[p[i]].push_back(i);
}else root = i;
}
{
int c = 0;
for(int i=0; i<n; i++){
int curr = 0;
while(curr != -1){
if(pref[curr] < c)break;
pref[curr] = c;
curr = p[curr];
}
++c;
}
}
vector<int> id(n);
dist[root] = 0;
dfs(root, -1);
for(int i=0; i<n; i++)id[order[i]] = i;
set<int> tofill;
for(int i=0; i<n; i++)tofill.insert(i);
lg = 32 - __builtin_clz(lg + 1);
vector<int> B[lg];
{
B[0].resize(n);
B[0][root] = root;
for(int i=0; i<n; i++)if(p[i] != -1)B[0][i] = p[i];
for(int c=1; c<lg; c++){
B[c].resize(n);
for(int i=0; i<n; i++){
B[c][i] = B[c-1][B[c-1][i]];
}
}
}
segment<node> K(n);
for(int i=0; i<m; i++){
int t, x;
cin >> t >> x;
if(t & 1){
vector<int> tem;
auto it = tofill.begin();
while(x--){
assert(it != tofill.end());
K.set(*it, node(1, 1, INF));
tem.push_back(*it);
++it;
}
cout << order[tem.back()] + 1 << '\n';
for(int &c : tem)tofill.erase(c);
}else {
--x;
int y = x;
{
for(int i=lg - 1; i>=0; i--)
if(K.get(id[B[i][y]]).sm)y = B[i][y];
}
K.set(id[y], node(0, 1, id[y]));
tofill.insert(id[y]);
cout << dist[x] - dist[y] << '\n';
}
}
return 0;
}
Compilation message (stderr)
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:133:20: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
133 | B[0][root] = root;
# | 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... |