Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7. using ll = long long;
  8.  
  9. const int MAXN = 1000010;
  10. const ll MOD = 1e9 + 7;
  11. const ll pr = 37;
  12.  
  13. vector<bool> prime (MAXN, true);
  14.  
  15. vector<ll> p;
  16.  
  17. ll modul(ll x) {
  18. return (x + MOD + MOD) % MOD;
  19. }
  20.  
  21. void precalc() {
  22. p[0] = 1;
  23. for (int i = 1; i < MAXN; ++i) {
  24. p[i] = modul(p[i - 1] * pr);
  25. }
  26. }
  27.  
  28. struct Node{
  29. Node *left, *right;
  30. ll hash;
  31. ll prior;
  32. int mult;
  33. int sz;
  34. char symbol;
  35. Node(){}
  36. Node(int new_val = 0, char new_symbol = 0) {
  37. sz = 1;
  38. prior = rand();
  39. left = right = nullptr;
  40. hash = new_symbol;
  41. symbol = new_symbol;
  42. mult = 0;
  43. }
  44. };
  45.  
  46. int safe_get_size(Node *root) {
  47. if (root) {
  48. return root->sz;
  49. } else {
  50. return 0;
  51. }
  52. }
  53.  
  54. int safe_get_hash(Node *root) {
  55. if (root) {
  56. return root->hash;
  57. } else {
  58. return 0;
  59. }
  60. }
  61.  
  62. void relax(Node *root) {
  63. if (!root) {
  64. return;
  65. } else {
  66. root->sz = safe_get_size(root->left) + safe_get_size(root->right) + 1;
  67. ll left_hash = safe_get_hash(root->left);
  68. ll right_hash = safe_get_hash(root->right);
  69. root->hash = modul(left_hash + modul(right_hash * p[safe_get_size(root->left)]));
  70. }
  71. }
  72.  
  73.  
  74. void push(Node *root) {
  75. if (!root || !root->mult)
  76. return;
  77. if (root->left) {
  78. root->left->mult += root->mult;
  79. }
  80. if (root->right) {
  81. root->right->mult += root->mult;
  82. }
  83. root->hash = modul(root->hash * p[root->mult]);
  84. root->mult = 0;
  85. }
  86.  
  87. pair<Node*, Node*> split(Node *root, int k) {
  88. push(root);
  89. if (!root) {
  90. return {nullptr, nullptr};
  91. }
  92. if (safe_get_size(root->left) >= k) {
  93. auto p = split(root->left, k);
  94. root->left = p.second;
  95. relax(root);
  96. return {p.first, root};
  97. } else {
  98. auto p = split(root->right, k - safe_get_size(root->left) - 1);
  99. root->right = p.first;
  100. relax(root);
  101. return {root, p.second};
  102. }
  103. }
  104.  
  105. Node *merge(Node *L, Node *R) {
  106. push(L);
  107. push(R);
  108. if (!L) {
  109. return R;
  110. }
  111. if (!R) {
  112. return L;
  113. }
  114. if (L->prior < R->prior) {
  115. L->right = merge(L->right, R);
  116. relax(L);
  117. return L;
  118. } else {
  119. R->left = merge(L, R->left);
  120. relax(R);
  121. return R;
  122. }
  123. }
  124.  
  125. Node *add(Node *root, ) {
  126.  
  127. }
  128.  
  129. int main() {
  130. // freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  131. ios_base::sync_with_stdio(0), cin.tie(0);
  132. int n = MAXN - 1;
  133. prime[0] = prime[1] = false;
  134. for (int i = 2; i <= n; ++i) {
  135. if (prime[i]) {
  136. if (i * 1ll * i <= n) {
  137. for (int j = i * i; j <= n; j += i) {
  138. prime[j] = false;
  139. }
  140. }
  141. }
  142. }
  143. return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement