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>
#define ll long long
#define ull unsigned long long
#define all(x) x.begin(), x.end()
#define Neco "Ball Machine"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 2e5 + 7
using namespace std;
const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r)
{
return uniform_int_distribution<ll> (l, r)(rng);
}
int n, q, N, rt;
int cl[maxn], vt[maxn], cha[maxn][18];
int Min[maxn], h[maxn], a[maxn];
vector<int> edges[maxn];
set<int> S;
void pre_dfs(int u, int par) {
for(int v : edges[u]) if(v != par) {
pre_dfs(v, u);
Min[u] = min(Min[u], Min[v]);
}
}
int In[maxn], Out[maxn];
void dfs(int u, int par) {
static int Time = 0;
for(int v : edges[u]) if(v != par) {
h[v] = h[u] + 1, cha[v][0] = u;
fi(i, 1, 17) cha[v][i] = cha[cha[v][i - 1]][i - 1];
dfs(v, u);
}
a[++N] = u, vt[u] = N;
}
int Add(int k, int ans = 0) {
while(k--) {
ans = a[*S.begin()];
cl[a[*S.begin()]] = 1, S.erase(S.begin());
}
return ans;
}
int Del(int u, int ans = 0) {
fid(i, 17, 0) if(cl[cha[u][i]]) u = cha[u][i], ans += (1 << i);
S.insert(vt[u]), cl[u] = 0;
return ans;
}
void solve()
{
cin >> n >> q;
int rt = 0;
fi(i, 1, n) {
Min[i] = i;
int x; cin >> x;
if(x == 0) rt = i;
else edges[x].push_back(i);
}
pre_dfs(rt, 0);
fi(i, 1, n) sort(all(edges[i]), [](int x, int y) { return Min[x] < Min[y]; } );
dfs(rt, 0);
fi(i, 1, n) S.insert(i);
fi(i, 1, q) {
int type, x;
cin >> type >> x;
if(type == 1) cout << Add(x) << '\n';
else cout << Del(x) << '\n';
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(Neco".inp", "r")) {
freopen(Neco".inp", "r", stdin);
freopen(Neco".out", "w", stdout);
}
int nTest = 1;
// cin >> nTest;
while(nTest--)
{
solve();
}
return 0;
}
Compilation message (stderr)
ballmachine.cpp: In function 'void dfs(int, int)':
ballmachine.cpp:43:16: warning: unused variable 'Time' [-Wunused-variable]
43 | static int Time = 0;
| ^~~~
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:103:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | freopen(Neco".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp:104:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | freopen(Neco".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp: At global scope:
ballmachine.cpp:43:16: warning: 'Time' defined but not used [-Wunused-variable]
43 | static int Time = 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... |