이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define F first
#define S second
#define pii pair<int, int>
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 1e5 + 10, LOG = 20;
bool full[N];
vector<int> adj[N];
int n, root, g[N], mn[N], par[N][LOG], ord[N], turn = 0;
bool cmp(int u, int v){
return mn[u] < mn[v];
}
bool pmc(int u, int v){
return ord[u] < ord[v];
}
void p_dfs(int v, int p = 0){
par[v][0] = p;
for(int i=1; i<LOG; i++)
par[v][i] = par[par[v][i - 1]][i - 1];
mn[v] = v;
for(auto u : adj[v]){
p_dfs(u, v);
mn[v] = min(mn[v], mn[u]);
}
sort(adj[v].begin(), adj[v].end(), cmp);
}
void dfs(int v){
for(auto u : adj[v])
dfs(u);
ord[v] = turn++;
}
int add(){
int lo = -1, hi = n;
while(hi - lo > 1){
int mid = (lo + hi)/2;
if(!full[g[mid]])hi = mid;
else lo = mid;
}
full[g[hi]] = true;
return g[hi];
}
int erase(int v){
int ans = 0;
for(int i=LOG-1; i>=0; i--){
if(full[par[v][i]]){
ans += (1 << i);
v = par[v][i];
}
}
full[v] = false;
return ans;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int q; cin >> n >> q;
for(int i=1; i<=n; i++){
int p; cin >> p;
if(p == 0)root = i;
else adj[p].pb(i);
}
p_dfs(root);
dfs(root);
for(int i=0; i<n; i++)
g[i] = i + 1;
sort(g, g + n, pmc);
while(q--){
int tp; cin >> tp;
if(tp == 1){
int k, pos = -1; cin >> k;
while(k--)
pos = add();
cout << pos << '\n';
}else{
int v; cin >> v;
cout << erase(v) << '\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... |