This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast,O3,unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define MP make_pair
//#define endl '\n'
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
typedef long long ll;
const int MOD = 1e9 + 7;
const int N = 1e6 + 3e2;
int p[N];
vector<int> g[N];
int sub_min[N];
void dfs0(int v){
sub_min[v] = v;
for (int u : g[v]){
dfs0(u);
sub_min[v] = min(sub_min[v], sub_min[u]);
}
}
int order[N], vertex[N];
int timer = 0;
void traversal(int v){
sort(all(g[v]), [&](int x, int y){
return sub_min[x] < sub_min[y];
});
for (int u : g[v]) traversal(u);
timer++;
order[v] = timer;
vertex[timer] = v;
}
bool flag[N];
void solve(){
int n, q;
cin >> n >> q;
int root = 0;
for (int i = 1; i <= n; i++){
cin >> p[i];
if (p[i] != 0){
g[p[i]].push_back(i);
} else {
root = i;
}
}
dfs0(root);
traversal(root);
set<int> free;
for (int i = 1; i <= n; i++) free.insert(i);
while (q--){
int t;
cin >> t;
if (t == 1){
int k;
cin >> k;
int last = 0;
while (k--){
int v = *free.begin();
free.erase(free.begin());
v = vertex[v];
flag[v] = true;
last = v;
}
cout << last << endl;
} else {
int v;
cin >> v;
int last = v, cnt = 0;
while (v != 0 & flag[v]){
cnt++;
last = v;
v = p[v];
}
flag[last] = false;
cout << --cnt << endl;
}
}
}
int main(){
clock_t startTime = clock();
ios_base::sync_with_stdio(false);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int test_cases = 1;
// cin >> test_cases;
for (int test = 1; test <= test_cases; test++){
// cout << (solve() ? "YES" : "NO") << endl;
solve();
}
#ifdef LOCAL
cerr << "Time: " << int((double) (clock() - startTime) / CLOCKS_PER_SEC * 1000) << " ms" << endl;
#endif
return 0;
}
Compilation message (stderr)
ballmachine.cpp: In function 'void solve()':
ballmachine.cpp:91:22: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
91 | while (v != 0 & flag[v]){
| ~~^~~~
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:106:13: warning: unused variable 'startTime' [-Wunused-variable]
106 | clock_t startTime = clock();
| ^~~~~~~~~
# | 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... |