Advertisement
K_Y_M_bl_C

Untitled

Mar 20th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.63 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #pragma comment(linker, "/STACK:256000000")
  3.  
  4. #include <iostream>
  5. #include <cstdio>
  6. #include <cmath>
  7. #include <vector>
  8. #include <ctime>
  9. #include <map>
  10. #include <set>
  11. #include <string>
  12. #include <queue>
  13. #include <deque>
  14. #include <cassert>
  15. #include <cstdlib>
  16. #include <bitset>
  17. #include <algorithm>
  18. #include <string>
  19. #include <list>
  20. #include <fstream>
  21. #include <cstring>
  22. #include <climits>
  23. #include <stack>
  24. #include <unordered_map>
  25. #include <unordered_set>
  26.  
  27. using namespace std;
  28.  
  29. typedef unsigned long long ull;
  30. typedef long long ll;
  31. typedef pair<int, int> pii;
  32. typedef pair<ll, ll> pll;
  33. typedef vector<pii> vpii;
  34. typedef vector<int> vi;
  35. typedef vector<ll> vll;
  36. typedef vector<pair<ll, ll> > vpll;
  37.  
  38. #define mk make_pair
  39. #define inb push_back
  40. #define outb pop_back
  41. #define all(v) v.begin(), v.end()
  42. #define X first
  43. #define Y second
  44.  
  45. int solve();
  46.  
  47. int main()
  48. {
  49. #define TASK ""
  50. #ifdef _DEBUG
  51.     freopen("input.txt", "r", stdin);
  52.     freopen("output.txt", "w", stdout);
  53.     srand(2299);
  54. #else
  55.     //freopen(TASK".in", "r", stdin), freopen(TASK".out", "w", stdout);
  56.     //freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  57.     srand(228);
  58. #endif
  59.     solve();
  60.     return 0;
  61. }
  62.  
  63. const double EPS = 1e-9;
  64.  
  65. const int MAXN = (int)1e5 + 7;
  66. const int INF = (int)1e9 + 7;
  67. const ll LINF = (ll)7e18;
  68. const int LOG2 = 18;
  69.  
  70. int n, q, root;
  71. int timer;
  72. int tin[MAXN];
  73. int mnpd[MAXN];
  74. int tr[MAXN];
  75. int id[MAXN];
  76. int up[MAXN][19];
  77. vi g[MAXN];
  78. int fans;
  79.  
  80. bool cmp(int a, int b)
  81. {
  82.     return mnpd[a] > mnpd[b];
  83. }
  84.  
  85. void dfscalcmn(int u, int pr)
  86. {
  87.     mnpd[u] = u;
  88.     for (auto to : g[u])
  89.     {
  90.         if (to == pr)
  91.             continue;
  92.         dfscalcmn(to, u);
  93.         mnpd[u] = min(mnpd[u], mnpd[to]);
  94.     }
  95. }
  96.  
  97. void dfs(int u, int pr)
  98. {
  99.     tin[u] = timer;
  100.     ++timer;
  101.     for (auto to : g[u])
  102.     {
  103.         if (to == pr)
  104.             continue;
  105.         dfs(to, u);
  106.     }
  107. }
  108.  
  109. bool cmp2(int a, int b)
  110. {
  111.     return tin[a] < tin[b];
  112. }
  113.  
  114. pii t[4 * MAXN];
  115. int pos, val;
  116. int cnt;
  117.  
  118. void push(int v, int tl, int tr)
  119. {
  120.     if (t[v].Y != -1)
  121.     {
  122.         t[v].X = t[v].Y * (tr - tl + 1);
  123.         if (tl != tr)
  124.         {
  125.             t[2 * v + 1].Y = t[v].Y;
  126.             t[2 * v + 2].Y = t[v].Y;
  127.         }
  128.         t[v].Y = -1;
  129.     }
  130. }
  131.  
  132. void update(int v, int tl, int tr)
  133. {
  134.     push(v, tl, tr);
  135.     if (tr < pos || pos < tl)
  136.         return;
  137.     if (tl == tr && tl == pos)
  138.     {
  139.         t[v].Y = val;
  140.         push(v, tl, tr);
  141.         return;
  142.     }
  143.     int tm = tl + tr >> 1;
  144.     if (tl <= pos && pos <= tm)
  145.         update(2 * v + 1, tl, tm), push(2 * v + 2, tm + 1, tr);
  146.     else
  147.         push(2 * v + 1, tl, tm), update(2 * v + 2, tm + 1, tr);
  148.     t[v].X = t[2 * v + 1].X + t[2 * v + 2].X;
  149. }
  150.  
  151. void slidedown(int v, int tl, int tr)
  152. {
  153.     if (!cnt)
  154.         return;
  155.     push(v, tl, tr);
  156.     if (tr - tl + 1 - t[v].X <= cnt)
  157.     {
  158.         fans = tl;
  159.         cnt -= tr - tl + 1 - t[v].X;
  160.         t[v].Y = 1;
  161.         push(v, tl, tr);
  162.         return;
  163.     }
  164.     int tm = tl + tr >> 1;
  165.     {
  166.         //tm + 1..tr - R
  167.         push(2 * v + 2, tm + 1, tr);
  168.         if (tr - tm == t[2 * v + 2].X)
  169.         {
  170.             slidedown(2 * v + 1, tl, tm);
  171.             t[v].X = t[2 * v + 1].X + t[2 * v + 2].X;
  172.             return;
  173.         }
  174.         if (tr - tm - t[2 * v + 2].X <= cnt)
  175.         {
  176.             fans = tm + 1;
  177.             cnt -= tr - tm - t[2 * v + 2].X;
  178.             t[2 * v + 2].Y = 1;
  179.             push(2 * v + 2, tm + 1, tr);
  180.             slidedown(2 * v + 1, tl, tm);
  181.             push(2 * v + 1, tl, tm);
  182.             t[v].X = t[2 * v + 1].X + t[2 * v + 2].X;
  183.             return;
  184.         }
  185.         slidedown(2 * v + 2, tm + 1, tr);
  186.     }
  187.     if (cnt)
  188.     {
  189.         slidedown(2 * v + 1, tl, tm);
  190.     }
  191.     push(2 * v + 1, tl, tm);
  192.     push(2 * v + 2, tm + 1, tr);
  193.     t[v].X = t[2 * v + 1].X + t[2 * v + 2].X;
  194. }
  195.  
  196. int get(int v, int tl, int tr)
  197. {
  198.     push(v, tl, tr);
  199.     if (tl == tr)
  200.         return t[v].X;
  201.     int tm = tl + tr >> 1;
  202.     if (tl <= pos && pos <= tm)
  203.         get(2 * v + 1, tl, tm);
  204.     else
  205.         get(2 * v + 2, tm + 1, tr);
  206. }
  207.  
  208. int solve()
  209. {
  210.     scanf("%d %d", &n, &q);
  211.     fill(t, t + 4 * MAXN, mk(0, -1));
  212.     for (int i = 0; i < n; ++i)
  213.     {
  214.         memset(up[i], -1, sizeof up[i]);
  215.         id[i] = i;
  216.         int x;
  217.         scanf("%d", &x);
  218.         if (x)
  219.         {
  220.             --x;
  221.             g[x].inb(i);
  222.             up[i][0] = x;
  223.         }
  224.         else
  225.             root = i, up[root][0] = -1;
  226.     }
  227.     dfscalcmn(root, -1);
  228.     for (int i = 0; i < n; ++i)
  229.         sort(all(g[i]), cmp);
  230.     dfs(root, -1);
  231.     //calcpr
  232.     for (int st = 1; st <= LOG2; ++st)
  233.     {
  234.         for (int i = 0; i < n; ++i)
  235.         {
  236.             if (up[i][st - 1] == -1)
  237.                 up[i][st] = -1;
  238.             else
  239.                 up[i][st] = up[up[i][st - 1]][st - 1];
  240.         }
  241.     }
  242.     sort(id, id + n, cmp2);
  243.     for (int i = 0; i < n; ++i)
  244.     {
  245.         tr[id[i]] = i;
  246.     }
  247.     int cmd;
  248.     while (scanf("%d", &cmd) == 1)
  249.     {
  250.         --cmd;
  251.         if (!cmd)
  252.         {
  253.             scanf("%d", &cnt);
  254.             slidedown(0, 0, n - 1);
  255.             printf("%d\n", id[fans] + 1);
  256.         }
  257.         else
  258.         {
  259.             int x;
  260.             scanf("%d", &x);
  261.             --x;
  262.             int cur = x;
  263.             int cntpr = 0;
  264.             for (int st = LOG2; st >= 0; --st)
  265.             {
  266.                 if (up[cur][st] == -1)
  267.                     continue;
  268.                 pos = tr[up[cur][st]];
  269.                 if (!get(0, 0, n - 1))
  270.                     continue;
  271.                 cntpr |= 1 << st;
  272.                 cur = up[cur][st];
  273.             }
  274.             pos = tr[cur];
  275.             val = 0;
  276.             update(0, 0, n - 1);
  277.             printf("%d\n", cntpr);
  278.         }
  279.     }
  280.     return 0;
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement