# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
697644 | Do_you_copy | Ball Machine (BOI13_ballmachine) | C++17 | 384 ms | 28216 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Then
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
mt19937_64 Rand(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e5 + 1;
//const int Mod = 1e9 + 7;
//const int inf =
int n, q, root;
vector <int> adj[maxN];
int lift[maxN][20];
int depth[maxN];
int dp[maxN];
int tout[maxN], rtout[maxN], timedfs;
void pre_dfs(int u, int p){
lift[u][0] = p;
depth[u] = depth[p] + 1;
dp[u] = u;
for (int i = 1; i < 20; ++i){
lift[u][i] = lift[lift[u][i - 1]][i - 1];
}
for (int i: adj[u]){
pre_dfs(i, u);
dp[u] = min(dp[u], dp[i]);
}
}
void dfs(int u){
sort(adj[u].begin(), adj[u].end(),[&](const int &i, const int &j){
return dp[i] < dp[j];
});
for (int i: adj[u]){
dfs(i);
}
tout[u] = ++timedfs;
rtout[tout[u]] = u;
}
void Init(){
cin >> n >> q;
for (int i = 1; i <= n; ++i){
int x; cin >> x;
if (x == 0){
root = i;
}
else{
adj[x].pb(i);
}
}
pre_dfs(root, 0);
dfs(root);
tout[0] = n + 1;
rtout[n + 1] = 0;
set <int> S;
for (int i = 1; i <= n + 1; ++i) S.insert(i);
while (q--){
int t, x;
cin >> t >> x;
if (t == 1){
int last = 0;
while (x){
--x;
last = *S.begin();
S.erase(*S.begin());
}
cout << rtout[last] << "\n";
}
else{
int u = x;
for (int i = 18; i >= 0; --i){
if (S.find(tout[lift[u][i]]) == S.end()){
u = lift[u][i];
}
}
cout << depth[x] - depth[u] << "\n";
S.insert(tout[u]);
}
}
}
#define debug
#define taskname "test"
signed main(){
faster
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
int tt = 1;
//cin >> tt;
while (tt--){
Init();
}
if (fopen("timeout.txt", "r")){
ofstream timeout("timeout.txt");
timeout << signed(double(clock()) / CLOCKS_PER_SEC * 1000);
timeout.close();
#ifndef debug
cerr << "Time elapsed: " << signed(double(clock()) / CLOCKS_PER_SEC * 1000) << "ms\n";
#endif // debug
}
}
Compilation message (stderr)
# | 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... |