# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
667236 | AmirH | Ball Machine (BOI13_ballmachine) | C++14 | 637 ms | 49140 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.
#include<iostream>
#include<algorithm>
#include<math.h>
#include<vector>
#include<bitset>
#include<queue>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<unordered_map>
#include<list>
#include<utility>
#include<stack>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
#define cl clear
#define F first
#define S second
#define pb push_back
#define Sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define faster ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
const int MAX_N = 2e5 + 25;
const ll INF = 1e18;
const int inf = 1e9;
void free() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
vector<int> vtx[MAX_N];
set<pii> adj[MAX_N];
int mini[MAX_N];
vector<int> res;
int par[MAX_N];
set<int> notseen;
int ar[MAX_N];
int pr[MAX_N];
int height[MAX_N];
int lcm[MAX_N][20];
int listpo[MAX_N];
int get_height(int v, int h) {
if(h == 0)
return v;
int max_z = listpo[h];
return get_height(lcm[v][max_z], h - (1 << max_z));
}
int get_last_seen(int v) {
int l = 0, r = height[v] + 1;
while(r - l > 1) {
int mid = (l + r) / 2;
if(notseen.find(ar[get_height(v, mid)]) == notseen.end())
l = mid;
else
r = mid;
}
return get_height(v, l);
}
void find_po() {
int j = 0;
for(int i = 1; i <= 1e5; i++) {
if(i >= (1 << (j + 1))) {
j++;
listpo[i] = j;
}
else
listpo[i] = j;
}
}
void rmq(int v) {
queue<int> q;
q.push(v);
while(!q.empty()) {
int x = q.front();
q.pop();
for(auto i : vtx[x]) {
if(height[i] > height[x])
q.push(i);
}
for(int i = 0; (1 << i) <= height[x]; i++) {
if(i == 0)
lcm[x][i] = par[x];
else
lcm[x][i] = lcm[lcm[x][i - 1]][i - 1];
}
}
}
void dfs2(int v) {
for(auto i : adj[v])
dfs2(i.second);
res.pb(v);
}
void dfs(int v, int s) {
mini[v] = v;
for(auto i : vtx[v]) {
if(i != s) {
height[i] = height[v] + 1;
dfs(i, v);
mini[v] = min(mini[v], mini[i]);
adj[v].insert({mini[i], i});
}
}
}
int main() {
faster; //free();
find_po();
int n, q; cin >> n >> q;
int st;
for(int i = 1; i <= n; i++) {
int x; cin >> x;
par[i] = x;
if(x == 0) {
st = i;
}
else {
vtx[x].pb(i);
vtx[i].pb(x);
}
}
height[st] = 0;
dfs(st, 0);
dfs2(st);
rmq(st);
for(int i = 0; i < res.size(); i++) {
ar[res[i]] = i + 1;
pr[i + 1] = res[i];
}
for(int i = 1; i <= n; i++)
notseen.insert(i);
while(q--) {
int t, x; cin >> t >> x;
if(t == 1) {
int ft;
while(x--) {
ft = pr[*notseen.begin()];
notseen.erase(notseen.begin());
}
cout << ft << '\n';
}
if(t == 2) {
int sec = get_last_seen(x);
notseen.insert(ar[sec]);
cout << height[x] - height[sec] << '\n';
}
}
}
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... |