This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// cre: Nguyen Ngoc Hung - Train VOI 2024 :>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <unordered_set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <cstring>
#include <unordered_map>
#include <cmath>
#include <array>
#include <cassert>
#include <random>
using namespace std;
#define SORT_UNIQUE(v) sort(ALL(v)),(v).resize(unique(ALL(v))-(v).begin())
#define __nnhzzz__ signed main()
#define BIT(i,j) ((i>>j)&1LL)
#define MASK(i) (1LL<<i)
#define RALL(x) (x).rbegin(),(x).rend()
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) (int)(x).size()
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvi>
#define vvvvi vector<vvvi>
#define pii pair<int,int>
#define vpii vector<pii>
#define RESET(x,val) memset((x),(val),sizeof(x))
#define REPDIS(i,be,en,j) for(int i = (be); i<=(en); i+=j)
#define REPD(i,be,en) for(int i = (be); i>=(en); i--)
#define REP(i,be,en) for(int i = (be); i<=(en); i++)
//-------------------------------------------------------------//
const int oo = 1e9,LOG = 17,MAXN = 1e5+7,N = 1e2+3;
const int MOD = 1e9+7,MOD1 = 1e9+207,MOD2 = 1e9+407,MOD3 = 998244353;
//-------------------------------------------------------------//
template<typename T1, typename T2> bool mini(T1 &a, T2 b){if(a>b){a=b;return true;}return false;}
template<typename T1, typename T2> bool maxi(T1 &a, T2 b){if(a<b){a=b;return true;}return false;}
template<typename T> T gcd(T a, T b) { while(b) { a %= b; swap(a,b); } return a; }
template<typename T> T lcm(T a, T b) { return a/gcd(a,b)*b; }
/*
----------------------------------------------------------------
END OF TEMPLATE
----------------------------------------------------------------
Nguyen Ngoc Hung - nnhzzz
Training for VOI24 gold medal
----------------------------------------------------------------
*/
int f[MAXN][LOG],pos[MAXN],ok[MAXN];
vi adj[MAXN][2];
int root,times = 0;
int dfs1(int u, int par){
priority_queue<pii,vpii,greater<pii>> heap;
for(auto v:adj[u][0]){
if(v==par){
continue;
}
heap.emplace(dfs1(v,u),v);
}
int res = u;
while(SZ(heap)!=0){
auto [mi,v] = heap.top(); heap.pop();
adj[u][1].push_back(v);
// cerr << u << " " << v << "\n";
mini(res,mi);
}
// cerr << res << " ";
return res;
}
priority_queue<pii,vpii,greater<pii>> heap;
void dfs2(int u){
REP(i,1,LOG-1){
f[u][i] = f[f[u][i-1]][i-1];
}
for(auto v:adj[u][1]){
dfs2(v);
}
pos[u] = ++times;
heap.emplace(pos[u],u);
}
void solve(){
int n,q; cin >> n >> q;
REP(u,1,n){
int v; cin >> v;
f[u][0] = v;
if(v==0){
root = u;
continue;
}
adj[u][0].push_back(v);
adj[v][0].push_back(u);
}
dfs1(root,0); dfs2(root);
while(q--){
int res = 0,t,u; cin >> t >> u;
if(t==1){
while(u--){
auto [mi,v] = heap.top(); heap.pop();
ok[v] = 1; res = v;
}
}else{
REPD(i,LOG-1,0){
if(ok[f[u][i]]==1){
res += MASK(i);
u = f[u][i];
}
}
ok[u] = 0;
heap.emplace(pos[u],u);
}
cout << res << "\n";
}
}
__nnhzzz__{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "test"
if(fopen(name".inp","r")){
freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);
}
#define name1 "nnhzzz"
if(fopen(name1".inp","r")){
freopen(name1".inp","r",stdin);
freopen(name1".out","w",stdout);
}
int test = 1;
while(test--){
solve();
}
cerr << "\nTime elapsed: " << 1000*clock()/CLOCKS_PER_SEC << "ms\n";
return 0;
}
Compilation message (stderr)
ballmachine.cpp: In function 'int dfs1(int, int)':
ballmachine.cpp:100:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
100 | auto [mi,v] = heap.top(); heap.pop();
| ^
ballmachine.cpp: In function 'void solve()':
ballmachine.cpp:139:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
139 | auto [mi,v] = heap.top(); heap.pop();
| ^
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:161:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
161 | freopen(name".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp:162:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
162 | freopen(name".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp:166:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
166 | freopen(name1".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp:167:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
167 | freopen(name1".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |