이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/** MIT License Copyright (c) 2018 Vasilyev Daniil **/
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
template<typename T> using v = vector<T>;
#define int long long
typedef long double ld;
typedef string str;
typedef vector<int> vint;
#define rep(a, l, r) for(int a = (l); a < (r); a++)
#define pb push_back
#define sz(a) ((int) a.size())
const long long inf = 4611686018427387903; //2^62 - 1
#if 0 //FileIO
const string fileName = "";
ifstream fin ((fileName == "" ? "input.txt" : fileName + ".in" ));
ofstream fout((fileName == "" ? "output.txt" : fileName + ".out"));
#define get fin>>
#define put fout<<
#else
#define get cin>>
#define put cout<<
#endif
#define eol put endl
#define printVar(a) put #a << ": " << a << endl;
void read() {} template<typename Arg,typename... Args> void read (Arg& arg,Args&... args){get (arg) ;read(args...) ;}
void print(){} template<typename Arg,typename... Args> void print(Arg arg,Args... args){put (arg)<<" ";print(args...);}
void debug(){eol;} template<typename Arg,typename... Args> void debug(Arg arg,Args... args){put (arg)<<" ";debug(args...);}
int getInt(){int a; get a; return a;}
//code goes here
const int N = 1e5 + 1;
vint g[N];
const int M = 20;
int kthAncestor[N][M];
int tim[N];
int timCur = 1;
set<pair<int, int>> nxt;
void dfs(int cur, int par) {
kthAncestor[cur][0] = par;
int p = par;
int k = 0;
while (p != -1) {
p = kthAncestor[p][k];
k++;
kthAncestor[cur][k] = p;
}
for (int i : g[cur])
dfs(i, cur);
tim[cur] = timCur++;
nxt.insert({tim[cur], cur});
}
bool hasBall[N];
void run() {
int n, q;
read(n, q);
n++;
int r = -1;
rep(i, 1, n) {
int p;
get p;
if (p == 0)
r = i;
else
g[p].pb(i);
}
rep(i, 1, n)
rep(j, 0, M)
kthAncestor[i][j] = -1;
//printVar(r);
dfs(r, -1);
for (auto i : nxt) debug(i.first, i.second);
memset(&hasBall, 0, sizeof(hasBall));
for (; q; q--) {
int t;
get t;
if (t == 1) {
int k;
get k;
while (k) {
auto d = *nxt.begin();
nxt.erase(d);
hasBall[d.second] = 1;
k--;
if (k == 0)
put d.second;
}
} else {
int v;
get v;
int cur = v;
int d = 0;
for (int i = M - 1; i >= 0;) {
if (!hasBall[kthAncestor[cur][i]])
i--;
else {
cur = kthAncestor[cur][i];
d += (1 << i);
}
}
hasBall[cur] = 0;
nxt.insert({tim[cur], cur});
put d;
}
eol;
}
}
int32_t main() {srand(time(0)); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); put fixed; put setprecision(15); run(); return 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... |