Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #pragma comment(linker, "/STACK:256000000")
- #include <iostream>
- #include <cstdio>
- #include <cmath>
- #include <vector>
- #include <ctime>
- #include <map>
- #include <set>
- #include <string>
- #include <queue>
- #include <deque>
- #include <cassert>
- #include <cstdlib>
- #include <bitset>
- #include <algorithm>
- #include <string>
- #include <list>
- #include <fstream>
- #include <cstring>
- #include <climits>
- #include <stack>
- #include <unordered_map>
- #include <unordered_set>
- using namespace std;
- typedef unsigned long long ull;
- typedef long long ll;
- typedef pair<int, int> pii;
- typedef pair<ll, ll> pll;
- typedef vector<pii> vpii;
- typedef vector<int> vi;
- typedef vector<ll> vll;
- typedef vector<pair<ll, ll> > vpll;
- #define mk make_pair
- #define inb push_back
- #define outb pop_back
- #define all(v) v.begin(), v.end()
- #define X first
- #define Y second
- int solve();
- int main()
- {
- #define TASK ""
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- srand(2299);
- #else
- //freopen(TASK".in", "r", stdin), freopen(TASK".out", "w", stdout);
- //freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
- srand(228);
- #endif
- solve();
- return 0;
- }
- const double EPS = 1e-9;
- const int MAXN = (int)1e5 + 7;
- const int INF = (int)1e9 + 7;
- const ll LINF = (ll)7e18;
- const int LOG2 = 18;
- int n, q, root;
- int timer;
- int tin[MAXN];
- int mnpd[MAXN];
- int tr[MAXN];
- int id[MAXN];
- int up[MAXN][19];
- vi g[MAXN];
- int fans;
- bool cmp(int a, int b)
- {
- return mnpd[a] > mnpd[b];
- }
- void dfscalcmn(int u, int pr)
- {
- mnpd[u] = u;
- for (auto to : g[u])
- {
- if (to == pr)
- continue;
- dfscalcmn(to, u);
- mnpd[u] = min(mnpd[u], mnpd[to]);
- }
- }
- void dfs(int u, int pr)
- {
- tin[u] = timer;
- ++timer;
- for (auto to : g[u])
- {
- if (to == pr)
- continue;
- dfs(to, u);
- }
- }
- bool cmp2(int a, int b)
- {
- return tin[a] < tin[b];
- }
- pii t[4 * MAXN];
- int pos, val;
- int cnt;
- void push(int v, int tl, int tr)
- {
- if (t[v].Y != -1)
- {
- t[v].X = t[v].Y * (tr - tl + 1);
- if (tl != tr)
- {
- t[2 * v + 1].Y = t[v].Y;
- t[2 * v + 2].Y = t[v].Y;
- }
- t[v].Y = -1;
- }
- }
- void update(int v, int tl, int tr)
- {
- push(v, tl, tr);
- if (tr < pos || pos < tl)
- return;
- if (tl == tr && tl == pos)
- {
- t[v].Y = val;
- push(v, tl, tr);
- return;
- }
- int tm = tl + tr >> 1;
- if (tl <= pos && pos <= tm)
- update(2 * v + 1, tl, tm), push(2 * v + 2, tm + 1, tr);
- else
- push(2 * v + 1, tl, tm), update(2 * v + 2, tm + 1, tr);
- t[v].X = t[2 * v + 1].X + t[2 * v + 2].X;
- }
- void slidedown(int v, int tl, int tr)
- {
- if (!cnt)
- return;
- push(v, tl, tr);
- if (tr - tl + 1 - t[v].X <= cnt)
- {
- fans = tl;
- cnt -= tr - tl + 1 - t[v].X;
- t[v].Y = 1;
- push(v, tl, tr);
- return;
- }
- int tm = tl + tr >> 1;
- {
- //tm + 1..tr - R
- push(2 * v + 2, tm + 1, tr);
- if (tr - tm == t[2 * v + 2].X)
- {
- slidedown(2 * v + 1, tl, tm);
- t[v].X = t[2 * v + 1].X + t[2 * v + 2].X;
- return;
- }
- if (tr - tm - t[2 * v + 2].X <= cnt)
- {
- fans = tm + 1;
- cnt -= tr - tm - t[2 * v + 2].X;
- t[2 * v + 2].Y = 1;
- push(2 * v + 2, tm + 1, tr);
- slidedown(2 * v + 1, tl, tm);
- push(2 * v + 1, tl, tm);
- t[v].X = t[2 * v + 1].X + t[2 * v + 2].X;
- return;
- }
- slidedown(2 * v + 2, tm + 1, tr);
- }
- if (cnt)
- {
- slidedown(2 * v + 1, tl, tm);
- }
- push(2 * v + 1, tl, tm);
- push(2 * v + 2, tm + 1, tr);
- t[v].X = t[2 * v + 1].X + t[2 * v + 2].X;
- }
- int get(int v, int tl, int tr)
- {
- push(v, tl, tr);
- if (tl == tr)
- return t[v].X;
- int tm = tl + tr >> 1;
- if (tl <= pos && pos <= tm)
- get(2 * v + 1, tl, tm);
- else
- get(2 * v + 2, tm + 1, tr);
- }
- int solve()
- {
- scanf("%d %d", &n, &q);
- fill(t, t + 4 * MAXN, mk(0, -1));
- for (int i = 0; i < n; ++i)
- {
- memset(up[i], -1, sizeof up[i]);
- id[i] = i;
- int x;
- scanf("%d", &x);
- if (x)
- {
- --x;
- g[x].inb(i);
- up[i][0] = x;
- }
- else
- root = i, up[root][0] = -1;
- }
- dfscalcmn(root, -1);
- for (int i = 0; i < n; ++i)
- sort(all(g[i]), cmp);
- dfs(root, -1);
- //calcpr
- for (int st = 1; st <= LOG2; ++st)
- {
- for (int i = 0; i < n; ++i)
- {
- if (up[i][st - 1] == -1)
- up[i][st] = -1;
- else
- up[i][st] = up[up[i][st - 1]][st - 1];
- }
- }
- sort(id, id + n, cmp2);
- for (int i = 0; i < n; ++i)
- {
- tr[id[i]] = i;
- }
- int cmd;
- while (scanf("%d", &cmd) == 1)
- {
- --cmd;
- if (!cmd)
- {
- scanf("%d", &cnt);
- slidedown(0, 0, n - 1);
- printf("%d\n", id[fans] + 1);
- }
- else
- {
- int x;
- scanf("%d", &x);
- --x;
- int cur = x;
- int cntpr = 0;
- for (int st = LOG2; st >= 0; --st)
- {
- if (up[cur][st] == -1)
- continue;
- pos = tr[up[cur][st]];
- if (!get(0, 0, n - 1))
- continue;
- cntpr |= 1 << st;
- cur = up[cur][st];
- }
- pos = tr[cur];
- val = 0;
- update(0, 0, n - 1);
- printf("%d\n", cntpr);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement