Guest User

RCC 2016 Round 3 - ashmelev v2

a guest
Jun 5th, 2016
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <map>
  7. #include <set>
  8. #include <bitset>
  9. #include <queue>
  10. #include <stack>
  11. #include <sstream>
  12. #include <cstring>
  13. #include <numeric>
  14. #include <ctime>
  15.  
  16. #define re return
  17. #define fi first
  18. #define se second
  19. #define mp make_pair
  20. #define pb push_back
  21. #define all(x) (x).begin(), (x).end()
  22. #define sz(x) ((int) (x).size())
  23. #define rep(i, n) for (int i = 0; i < (n); i++)
  24. #define rrep(i, n) for (int i = (n) - 1; i >= 0; i--)
  25. #define y0 y32479
  26. #define y1 y95874
  27. #define fill(x, y) memset(x, y, sizeof(x))
  28. #define sqr(x) ((x) * (x))
  29. #define sqrt(x) sqrt(abs(x))
  30. #define unq(x) (x.resize(unique(all(x)) - x.begin()))
  31. #define spc(i,n) " \n"[i == n - 1]
  32. #define next next239
  33. #define prev prev239
  34.  
  35. using namespace std;
  36.  
  37. typedef vector<int> vi;
  38. typedef vector<vi> vvi;
  39. typedef pair<int, int> ii;
  40. typedef vector<ii> vii;
  41. typedef vector<string> vs;
  42. typedef double D;
  43. typedef long double LD;
  44. typedef long long ll;
  45. typedef pair<ll, ll> pll;
  46. typedef vector<ll> vll;
  47.  
  48. template<class T> T abs(T x) { return x > 0 ? x : -x;}
  49.  
  50. int nint() {
  51.     int x;
  52.     scanf("%d", &x);
  53.     re x;
  54. }
  55.  
  56. int m;
  57. int n;
  58.  
  59. int dd = 400;
  60. vi cv;
  61.  
  62. vi v[100500];
  63. int ok[100500];
  64.  
  65. int ct, t1[100500], t2[100500], h[100500];
  66.  
  67. int p[100500];
  68. int pp[20][100500];
  69.  
  70. void dfs(int x, int d) {
  71.     h[x] = d + ok[x];
  72.     t1[x] = ct++;
  73.  
  74.     for (int y : v[x])
  75.         dfs(y, h[x]);
  76.  
  77.     t2[x] = ct++;
  78. }
  79.  
  80. int check(int x, int y) {
  81.     re t1[x] <= t1[y] && t2[x] >= t2[y];
  82. }
  83.  
  84. void build() {
  85.     rep(i, n)
  86.         pp[0][i] = p[i];
  87.     pp[0][0] = 0;
  88.     for (int i = 1; i <= 17; i++)
  89.         rep(j, n)
  90.             pp[i][j] = pp[i - 1][pp[i - 1][j]];
  91. }
  92.  
  93. int lca(int x, int y) {
  94.     if (check(x, y))
  95.         re x;
  96.     if (check(y, x))
  97.         re y;
  98.     for (int o = 17; o >= 0; o--) {
  99.         int z = pp[o][x];
  100.         if (!check(z, y))
  101.             x = z;
  102.     }
  103.     re p[x];
  104. }
  105.  
  106. void del(int x) {
  107.     cv.pb(x);
  108.     ok[x] = 0;
  109.  
  110.     if (sz(cv) >= dd) {
  111.         cv.clear();
  112.         dfs(0, 0);
  113.     }
  114. }
  115.  
  116. int getans(int x, int y) {
  117.     int z = lca(x, y);
  118.     int ans = h[x] + h[y] - 2 * h[z];
  119.     for (int o : cv) {
  120.         if (check(o, z))
  121.             continue;
  122.         if (check(o, x))
  123.             ans--;
  124.         if (check(o, y))
  125.             ans--;
  126.     }
  127.     re ans;
  128. }
  129.  
  130. int main() {
  131.     cin >> n;
  132.     rep(i, n - 1) {
  133.         int x = nint() - 1;
  134.         v[x].pb(i + 1);
  135.         p[i + 1] = x;
  136.     }
  137.     rep(i, n)
  138.         ok[i] = 1;
  139.  
  140.     dfs(0, 0);
  141.     build();
  142.  
  143.     cin >> m;
  144.     rep(i, m) {
  145.         int t = nint();
  146.         if (t == 1) {
  147.             int a = nint() - 1;
  148.             int b = nint() - 1;
  149.             cout << getans(a, b) << "\n";
  150.         }
  151.         else {
  152.             int x = nint() - 1;
  153.             del(x);
  154.         }
  155.     }
  156.  
  157.  
  158.     re 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment