이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
*/
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
using ll = long long;
const int mod = 1e9+7;
vector<vector<int>> adj, up;
vector<int> par, submn, tout, depth;
vector<bool> boulder;
set<pair<int, int>> s;
int LOG = 0, timer = 0;
void dfs1(int u, int p = -1) {
submn[u] = u;
for (int v : adj[u]) {
if (v != p) {
depth[v] = depth[u] + 1;
dfs1(v, u);
submn[u] = min(submn[u], submn[v]);
}
}
}
void dfs2(int u, int p = -1) {
set<pair<int, int>> curs;
for (int v : adj[u]) {
if (v != p) {
curs.insert({submn[v], v});
}
}
for (auto pr : curs) {
dfs2(pr.second, u);
}
tout[u] = ++timer;
s.insert({tout[u], u});
}
void solve(int tc) {
int n, q;
cin >> n >> q;
boulder.resize(n); adj.resize(n); up.resize(n); submn.resize(n); tout.resize(n); par.resize(n); depth.resize(n);
int root = -1;
while ((1 << LOG) <= n) ++LOG;
for (auto &x : up) x.resize(LOG);
for (int i = 0; i < n; ++i) {
int p;
cin >> p;
if (p == 0) {
root = i;
continue;
}
--p;
par[i] = p;
adj[p].push_back(i);
adj[i].push_back(p);
}
for (int i = 0; i < n; ++i) up[i][0] = par[i];
up[root][0] = root;
for (int l = 1; l < LOG; ++l) {
for (int i = 0; i < n; ++i) {
up[i][l] = up[up[i][l-1]][l-1];
}
}
dfs1(root);
dfs2(root);
while (q--) {
int a, b;
cin >> a >> b;
// cerr << "a b : " << a << " " << b << '\n';
if (a == 1) {
pair<int, int> cur;
for (int i = 0; i < b; ++i) {
cur = *s.begin();
auto it = s.begin();
s.erase(it);
boulder[cur.second] = true;
}
cout << cur.second+1 << '\n';
} else {
--b;
int cur = LOG-1;
int dist = 0;
while (cur >= 0) {
if (depth[b] >= (1<<cur) and boulder[up[b][cur]]) {
// cout << "b cur up[b][cur] : " << b << " " << cur << " " << up[b][cur] << '\n';
b = up[b][cur];
dist ^= (1<<cur);
// cout << "b : " << b << '\n';
}
--cur;
}
s.insert({tout[b], b});
boulder[b] = false;
cout << dist << '\n';
}
}
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int tc = 1;
//cin >> tc;
for (int i = 1; i <= tc; ++i) solve(i);
return 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... |