#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define TranLeThanhTam "TranLeThanhTam"
#define all(v) v.begin(),v.end()
#define reset(a,i) memset(a,i,sizeof(a))
#define FOR(i,a,b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i,a,b) for (int i = (a), _b = (b); i >= _b; --i)
#define compact(v) sort(all(v)), v.erase(unique(all(v)), end(v))
#define sz(v) (int) v.size()
#define MASK(i) (1 << (i))
typedef long long ll;
typedef long double lb;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
const int Thanh_Tam = 1e5 + 17;
const int inf = 1e9 + 17;
const long long infll = 1e18 + 17;
const int MOD = 1e9 + 7;
const int Log = 18;
const int dx[4] = {-1, 0, 0, 1};
const int dy[4] = {0, -1, 1, 0};
const int max_block_size = 270;
const ll P = 311;
const int maxA = 1e2 + 17;
const int addi = 1e9;
const char dir[4] = {'W', 'S', 'N', 'E'};
const int Mod[3] = {998244353, MOD, 759186269};
ll mul(ll a, ll b, ll mod);
ll binpow(ll a, ll b, ll mod)
{
ll res = 1;
while (b > 0)
{
if (b & 1) res = mul(res,a,mod);
b >>= 1;
a = mul(a,a,mod);
}
return res;
}
ll add(ll a, ll b, ll mod) { return (a + b) % mod; }
ll sub(ll a, ll b, ll mod) { return (a - b + mod) % mod; }
ll mul(ll a, ll b, ll mod) { return ((a % mod) * (b % mod)) % mod; }
ll div(ll a, ll b, ll mod) { return mul(a, binpow(b, mod - 2, mod), mod); }
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand_range(ll l, ll r) { return uniform_int_distribution<ll>(l,r)(rng); }
struct Segment
{
vector<int> st;
vector<int> lz;
Segment() {}
Segment(int n)
{
int _n = 1;
while (_n < n) _n <<= 1;
st.assign((_n << 1) + 1, 0);
lz.assign((_n << 1) + 1, false);
return;
}
void down(int id, int l, int r)
{
if (!lz[id]) return;
if (lz[id] == 1)
{
int mid = (l + r) >> 1;
st[id << 1] = (mid - l + 1);
st[(id << 1) | 1] = (r - mid);
lz[id << 1] = lz[(id << 1) | 1] = 1;
}
else
{
int mid = (l + r) >> 1;
st[id << 1] = 0;
st[(id << 1) | 1] = 0;
lz[id << 1] = lz[(id << 1) | 1] = 2;
}
lz[id] = 0;
return;
}
void update(int id, int l, int r, int u, int v)
{
if (v < l || r < u) return;
if (u <= l && r <= v)
{
st[id] = (r - l + 1);
lz[id] = 1;
return;
}
down(id, l, r);
int mid = (l + r) >> 1;
update(id << 1, l, mid, u, v);
update((id << 1) | 1, mid + 1, r, u, v);
st[id] = st[id << 1] + st[(id << 1) | 1];
return;
}
void update2(int id, int l, int r, int u, int v)
{
if (v < l || r < u) return;
if (u <= l && r <= v)
{
st[id] = 0;
lz[id] = 2;
return;
}
down(id, l, r);
int mid = (l + r) >> 1;
update2(id << 1, l, mid, u, v);
update2((id << 1) | 1, mid + 1, r, u, v);
st[id] = st[id << 1] + st[(id << 1) | 1];
return;
}
int query(int id, int l, int r, int u, int v)
{
if (v < l || r < u) return 0;
if (u <= l && r <= v) return st[id];
down(id, l, r);
int mid = (l + r) >> 1;
return query(id << 1, l, mid, u, v) + query((id << 1) | 1, mid + 1, r, u, v);
}
} tree;
int n, q, root, timer = 0;
vector<int> adj[Thanh_Tam];
int tour[Thanh_Tam], f[Thanh_Tam], tin[Thanh_Tam];
int par[Thanh_Tam][Log + 1], h[Thanh_Tam];
void dfs3(int u, int Par)
{
f[u] = u;
for (int v : adj[u])
{
if (v == Par) continue;
dfs3(v, u);
f[u] = min(f[u], f[v]);
}
return;
}
void dfs1(int u, int Par)
{
vector<pii> value;
for (int v : adj[u])
{
if (v == Par) continue;
value.pb({f[v], v});
}
sort(all(value));
for (pii T : value) dfs1(T.se, u);
tour[++timer] = u;
tin[u] = timer;
return;
}
void dfs2(int u, int Par)
{
for (int v : adj[u])
{
if (v == Par) continue;
h[v] = h[u] + 1;
par[v][0] = u;
dfs2(v, u);
}
return;
}
int walk(int u, int dis)
{
int v = u;
FORD(j, Log, 0)
if (dis >= MASK(j))
{
dis -= MASK(j);
v = par[v][j];
}
return v;
}
void prepare()
{
timer = 0;
dfs3(root, -1);
dfs1(root, -1);
dfs2(root, -1);
tree = Segment(n);
par[root][0] = 0;
FOR(j,1,Log)
FOR(i,1,n)
par[i][j] = par[par[i][j - 1]][j - 1];
return;
}
void ThanhTam()
{
cin >> n >> q;
FOR(v,1,n)
{
int u;
cin >> u;
if (u == 0)
{
root = v;
continue;
}
adj[u].pb(v);
adj[v].pb(u);
}
prepare();
while (q--)
{
int type, x;
cin >> type >> x;
if (type == 1)
{
int l = 1, r = n, pos = 0;
while (l <= r)
{
int mid = (l + r) >> 1;
if (mid - tree.query(1, 1, n, 1, mid) < x)
{
pos = mid;
l = mid + 1;
}
else r = mid - 1;
}
cout << tour[pos + 1] << '\n';
tree.update(1, 1, n, 1, pos + 1);
}
else
{
int l = 0, r = h[x], ans = 0;
while (l <= r)
{
int mid = (l + r) >> 1, acs = walk(x, mid);
if (tree.query(1, 1, n, tin[acs], tin[acs]) == 1)
{
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
int acs = walk(x, ans);
cout << ans << '\n';
tree.update2(1, 1, n, tin[acs], tin[acs]);
}
}
return;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
if (fopen(TranLeThanhTam".inp","r"))
{
freopen(TranLeThanhTam".inp","r",stdin);
freopen(TranLeThanhTam".out","w",stdout);
}
int t = 1;
//cin >> t;
while (t--)
ThanhTam();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:282:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
282 | freopen(TranLeThanhTam".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp:283:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
283 | freopen(TranLeThanhTam".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... |