Guest User

Untitled

a guest
Jan 12th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. using ll = long long;
  5.  
  6. ll sum = 0;
  7.  
  8. struct Node {
  9. Node *left, *right;
  10. ll val;
  11. int prior, sz;
  12. Node(ll new_val = 0LL) {
  13. left = right = nullptr;
  14. val = new_val;
  15. prior = rand();
  16. sz = 1;
  17. }
  18. };
  19.  
  20. int safe_get_size(Node *root) {
  21. if (!root)
  22. return 0;
  23. return root->sz;
  24. }
  25.  
  26. void relax(Node *root) {
  27. if (!root)
  28. return;
  29. root->sz = safe_get_size(root->left) + safe_get_size(root->right) + 1;
  30. }
  31.  
  32. Node *merge(Node *L, Node *R) {
  33. if (!L)
  34. return R;
  35. if (!R)
  36. return L;
  37. if (L->prior < R->prior) {
  38. L->right = merge(L->right, R);
  39. relax(L);
  40. return L;
  41. } else {
  42. R->left = merge(L, R->left);
  43. relax(R);
  44. return R;
  45. }
  46. }
  47.  
  48. pair<Node*, Node*> split(Node *root, int k) {
  49. if (!root)
  50. return {nullptr, nullptr};
  51. if (safe_get_size(root->left) >= k) {
  52. auto p = split(root->left, k);
  53. root->left = p.second;
  54. relax(root);
  55. return {p.first, root};
  56. } else {
  57. auto p = split(root->right, k - safe_get_size(root->left) - 1);
  58. root->right = p.first;
  59. relax(root);
  60. return {root, p.second};
  61. }
  62. }
  63.  
  64. void outt(Node *root) {
  65. if (root->left) {
  66. outt(root->left);
  67. }
  68. cout << root->val << " " << safe_get_size(root) << "\n";
  69. if (root->right) {
  70. outt(root->right);
  71. }
  72. }
  73.  
  74. int main() {
  75. freopen("input.txt", "r", stdin), freopen("a.out", "w", stdout);
  76. // freopen("hall.in", "r", stdin), freopen("hall.out", "w", stdout);
  77. ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  78. int n, type;
  79. cin >> n >> type;
  80. Node *root = nullptr;
  81. for (int i = 0; i < n; ++i) {
  82. ll cur_len;
  83. cin >> cur_len;
  84. sum += cur_len * cur_len;
  85. Node *v = new Node(cur_len);
  86. root = merge(root, v);
  87. }
  88. cout << sum << "\n";
  89. int q;
  90. cin >> q;
  91. while (q--) {
  92. int e, v;
  93. cin >> e >> v;
  94. if (e == 1) {
  95.  
  96. } else {
  97.  
  98. }
  99. }
  100. return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment