Advertisement
bibaboba12345

Untitled

Nov 14th, 2022
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <random>
  4. #include <cstdlib>
  5. #include <cassert>
  6. #include <algorithm>
  7. #include <cmath>
  8. #include <set>
  9. #include <iomanip>
  10. #include <deque>
  11. #include <bitset>
  12. #include <map>
  13.  
  14. using namespace std;
  15.  
  16. const int N = 1e5 + 7, MOD = 1e9 + 7;
  17. const int A = 26, C = 2;
  18.  
  19. int n,m, qr;
  20.  
  21. mt19937 rnd;
  22. int a[N], ans[N];
  23. vector<int> e[N];
  24.  
  25. struct Node {
  26. int sum, x, y;
  27. Node* l, *r;
  28. Node(int val) {
  29. y = rnd();
  30. x = val;
  31. l = nullptr;
  32. r = nullptr;
  33. sum = 1;
  34. }
  35. };
  36.  
  37. void recalc(Node* v) {
  38. if (v == nullptr) {
  39. return;
  40. }
  41. v->sum = 1;
  42. if (v->l != nullptr) {
  43. v->sum += v->l->sum;
  44. }
  45. if (v->r != nullptr) {
  46. v->sum += v->r->sum;
  47. }
  48. }
  49.  
  50. pair<Node*, Node*> split(Node* v, int x) {
  51. if (v == nullptr) {
  52. return { nullptr, nullptr };
  53. }
  54. if (v->x > x) {
  55. auto res = split(v->l, x);
  56. v->l = res.second;
  57. recalc(v->l);
  58. recalc(v);
  59. return { res.first, v };
  60. }
  61. else {
  62. auto res = split(v->r, x);
  63. v->r = res.first;
  64. recalc(v->r);
  65. recalc(v);
  66. return { v, res.second };
  67. }
  68. }
  69.  
  70. Node* merge(Node* l, Node* r) {
  71. if (l == nullptr || r == nullptr) {
  72. return l == nullptr ? r : l;
  73. }
  74. if (l->y > r->y) {
  75. l->r = merge(l->r, r);
  76. recalc(l);
  77. recalc(l->r);
  78. return l;
  79. }
  80. else {
  81. r->l = merge(l, r->l);
  82. recalc(r);
  83. recalc(r->l);
  84. return r;
  85. }
  86. }
  87.  
  88. int sz(Node* v) {
  89. if (v == nullptr) {
  90. return 0;
  91. }
  92. return v->sum;
  93. }
  94.  
  95. Node* insert(Node* root, Node* v) {
  96. auto res = split(root, v->x);
  97. return merge(merge(res.first, v), res.second);
  98. }
  99.  
  100. Node* sliv(Node* root, Node* v) {
  101. if (v == nullptr) {
  102. return root;
  103. }
  104. root = sliv(root, v->l);
  105. recalc(root);
  106. root = sliv(root, v->r);
  107. recalc(root);
  108. v->l = nullptr;
  109. v->r = nullptr;
  110. root = insert(root, v);
  111. recalc(root);
  112. return root;
  113. }
  114.  
  115. Node* Merge(Node* u, Node* v) {
  116. if (sz(u) < sz(v)) {
  117. return sliv(v, u);
  118. }
  119. else {
  120. return sliv(u, v);
  121. }
  122. }
  123.  
  124. Node* dfs(int v) {
  125. Node* root = new Node(a[v]);
  126. for (auto u : e[v]) {
  127. Node* g = dfs(u);
  128. root = Merge(g, root);
  129. }
  130. if (v == 1) {
  131. cout << "";
  132. }
  133. auto res = split(root, a[v]);
  134. if (res.second != nullptr)
  135. ans[v] = res.second->sum;
  136. return merge(res.first, res.second);
  137. }
  138.  
  139. signed main() {
  140. #ifdef _DEBUG
  141. freopen("input.txt", "r", stdin);
  142. #else
  143. ios::sync_with_stdio(false);
  144. cin.tie(0);
  145. #endif
  146. cin >> n;
  147. for (int i = 1; i <= n; i++) {
  148. cin >> a[i];
  149. }
  150. for (int i = 2; i <= n; i++) {
  151. int p;
  152. cin >> p;
  153. e[p].push_back(i);
  154. }
  155. dfs(1);
  156. for (int i = 1; i <= n; i++) {
  157. cout << ans[i] << "\n";
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement