mitkonikov

Sladoledite

Jun 5th, 2023
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. // #include <bits/stdc++.h>
  2. // #define ll long long
  3.  
  4. // using namespace std;
  5.  
  6. // vector<vector<int>> up;
  7. // vector<int> depth;
  8. // vector<int> mapped;
  9. // const int LOG = 20;
  10.  
  11. // void add(int u, int p) {
  12. // depth[u] = depth[p] + 1;
  13. // up[u][0] = p;
  14. // for (int l = 1; l < LOG; l++) {
  15. // up[u][l] = up[up[u][l-1]][l-1];
  16. // }
  17. // }
  18.  
  19. // int lca(int u, int v) {
  20. // if (u == v) return u;
  21. // if (depth[u] > depth[v]) swap(u, v);
  22. // for (int i = depth[v] - depth[u], k = 0; i > 0; i /= 2, k++) {
  23. // if (i & 1) v = up[v][k];
  24. // }
  25. // if (u == v) return u;
  26. // for (int i = LOG - 1; i >= 0; i--) {
  27. // if (up[u][i] != up[v][i]) {
  28. // u = up[u][i];
  29. // v = up[v][i];
  30. // }
  31. // }
  32. // return up[u][0];
  33. // }
  34.  
  35. // int main() {
  36. // int N;
  37. // cin >> N;
  38. // depth.resize(N + 10);
  39. // up.resize(N + 10, vector<int>(LOG, 0));
  40. // mapped.resize(N + 10);
  41. // for (int i = 1; i <= N; i++) {
  42. // char c;
  43. // cin >> c;
  44. // if (c == 'a') {
  45. // int u;
  46. // cin >> u;
  47. // add(i, mapped[u]);
  48. // mapped[i] = i;
  49. // } else if (c == 'b') {
  50. // int u;
  51. // cin >> u;
  52. // u = mapped[u];
  53. // int p = up[u][0];
  54. // cout << u << endl;
  55. // mapped[i] = p;
  56. // } else {
  57. // int u, v;
  58. // cin >> u >> v;
  59. // u = mapped[u];
  60. // v = mapped[v];
  61. // mapped[i] = u;
  62. // cout << depth[lca(u, v)] << endl;
  63. // }
  64. // }
  65. // return 0;
  66. // }
  67.  
  68. #include <bits/stdc++.h>
  69. using ll = long long;
  70. using ull = unsigned long long;
  71. const int mod = 1e9 + 7;
  72. int LOG = 0;
  73. const int maxn = 3e5 + 5;
  74. using namespace std;
  75.  
  76. void setUSA(string name) {
  77. freopen( (name + ".in").c_str(), "r", stdin);
  78. freopen( (name + ".out").c_str(), "w", stdout);
  79. }
  80.  
  81. void setIO(string name = "") {
  82. ios_base::sync_with_stdio(false);
  83. cout.tie(nullptr);
  84. cin.tie(nullptr);
  85. if(name != "") setUSA(name);
  86. }
  87.  
  88. vector<int> depth;
  89. vector<int> cream;
  90. vector<vector<int> > up;
  91.  
  92. void update(int node, int parent, int cnt) {
  93. depth[node] = depth[parent] + 1;
  94. up[node][0] = parent;
  95.  
  96. for(int j=1; j<LOG; j++)
  97. up[node][j] = up[ up[node][j-1] ][j-1];
  98. }
  99.  
  100. int jmp(int x, int d) {
  101. for(int j=LOG-1; j>=0; j--) {
  102. if(d & (1 << j)) {
  103. x = up[x][j];
  104. //cout << "jumped to depth: " << depth[x] << ", steps: " << pow(2, j) << '\n';
  105. }
  106. }
  107.  
  108. return x;
  109. }
  110.  
  111. int get_lca(int a, int b) {
  112. if(depth[a] < depth[b]) swap(a, b);
  113. //cout << "before jump: " << depth[a] << " - "<< depth[b] << '\n';
  114. a = jmp(a, depth[a] - depth[b]);
  115. //cout << "after jump: " << depth[a] << " - " << depth[b] << '\n';
  116. if(a == b) return a;
  117.  
  118. for(int j=LOG - 1; j>=0; j--)
  119. if(up[a][j] != up[b][j])
  120. a = up[a][j], b = up[b][j];
  121.  
  122. return up[a][0];
  123. }
  124.  
  125. int main() {
  126. //setIO();
  127.  
  128. int n, cnt = 1;
  129. cin >> n;
  130. while((1 << LOG) <= n) LOG++;
  131. LOG += 2;
  132. up.resize(n+1, vector<int>(LOG, 0));
  133. depth.resize(n+1, 0);
  134. cream.resize(n+1, 0);
  135.  
  136. while(n--) {
  137. char ch;
  138. cin >> ch;
  139.  
  140. if(ch == 'a') {
  141. int a;
  142. cin >> a;
  143. a = cream[a];
  144. update(cnt, a, cnt);
  145. cream[cnt] = cnt;
  146. } else if(ch == 'b') {
  147. int a;
  148. cin >> a;
  149. a = cream[a];
  150. cream[cnt] = up[a][0];
  151. cout << a << '\n';
  152. } else {
  153. int a, b;
  154. cin >> a >> b;
  155. a = cream[a];
  156. b = cream[b];
  157. cream[cnt] = a;
  158. int lca = get_lca(a, b);
  159. cout << depth[lca] << '\n';
  160. }
  161.  
  162. cnt++;
  163. }
  164.  
  165. return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment