Advertisement
Guest User

RCC 2016 Round 3 - ashmelev

a guest
Jun 5th, 2016
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 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], nv[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 p) {
  71.     if (x) {
  72.         h[x] = h[p] + 1;
  73.         if (ok[x])
  74.             nv[p].pb(x);
  75.     }
  76.  
  77.     t1[x] = ct++;
  78.     ::p[x] = p;
  79.  
  80.     for (int y : v[x])
  81.         dfs(y, ok[x] ? x : p);
  82.  
  83.     t2[x] = ct++;
  84. }
  85.  
  86. int check(int x, int y) {
  87.     re t1[x] <= t1[y] && t2[x] >= t2[y];
  88. }
  89.  
  90. void build() {
  91.     rep(i, n)
  92.         pp[0][i] = p[i];
  93.     pp[0][0] = 0;
  94.     for (int i = 1; i <= 17; i++)
  95.         rep(j, n)
  96.             pp[i][j] = pp[i - 1][pp[i - 1][j]];
  97. }
  98.  
  99. int lca(int x, int y) {
  100.     if (check(x, y))
  101.         re x;
  102.     if (check(y, x))
  103.         re y;
  104.     for (int o = 17; o >= 0; o--) {
  105.         int z = pp[o][x];
  106.         if (!check(z, y))
  107.             x = z;
  108.     }
  109.     re p[x];
  110. }
  111.  
  112. void del(int x) {
  113.     cv.pb(x);
  114.     ok[x] = 0;
  115.  
  116.     if (sz(cv) >= dd) {
  117.         cv.clear();
  118.         rep(i, n)
  119.             nv[i].clear();
  120.         dfs(0, -1);
  121.         rep(i, n)
  122.             v[i] = nv[i];
  123.         build();
  124.     }
  125. }
  126.  
  127. int getans(int x, int y) {
  128.     int z = lca(x, y);
  129.     int ans = h[x] + h[y] - 2 * h[z];
  130.     for (int o : cv) {
  131.         if (check(o, z))
  132.             continue;
  133.         if (check(o, x))
  134.             ans--;
  135.         if (check(o, y))
  136.             ans--;
  137.     }
  138.     re ans;
  139. }
  140.  
  141. int main() {
  142.     cin >> n;
  143.     rep(i, n - 1) {
  144.         int x = nint() - 1;
  145.         v[x].pb(i + 1);
  146.     }
  147.     rep(i, n)
  148.         ok[i] = 1;
  149.  
  150.     dfs(0, -1);
  151.     build();
  152.  
  153.     cin >> m;
  154.     rep(i, m) {
  155.         int t = nint();
  156.         if (t == 1) {
  157.             int a = nint() - 1;
  158.             int b = nint() - 1;
  159.             cout << getans(a, b) << "\n";
  160.         }
  161.         else {
  162.             int x = nint() - 1;
  163.             del(x);
  164.         }
  165.     }
  166.  
  167.  
  168.     re 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement