Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. using namespace std;
  4.  
  5. const int INF = 2e18;
  6.  
  7. struct node {
  8. int left, right, real_val = -INF;
  9. node *left_child, *right_child;
  10. };
  11.  
  12. node *build(vector<int> &a, int left, int right) {
  13. node *cur = new node;
  14. if (left == right) {
  15. cur->left = left;
  16. cur->right = right;
  17. cur->real_val = a[left];
  18. return cur;
  19. }
  20.  
  21. int mid = (right + left) / 2;
  22.  
  23. cur->left_child = build(a, left, mid);
  24. cur->right_child = build(a, mid + 1, right);
  25. cur->left = left;
  26. cur->right = right;
  27.  
  28. return cur;
  29. }
  30.  
  31. int get_val(node* root, int ind) {
  32.  
  33. //cout << root->left << " " << root->right << " " << ind << '\n';
  34. if (root->left > ind || root->right < ind) {
  35. return -INF;
  36. }
  37.  
  38. if (root->left == ind && root->right == ind)
  39. return root->real_val;
  40.  
  41. if (root->left <= ind && ind <= root->right) {
  42.  
  43. if (root->real_val != -INF) {
  44. int ans = root->real_val;
  45. root->real_val = -INF;
  46. root->left_child->real_val = ans;
  47. root->right_child->real_val = ans;
  48.  
  49. return ans;
  50. } else {
  51. return max(get_val(root->left_child, ind), get_val(root->right_child, ind));
  52. }
  53. }
  54. }
  55.  
  56. void set_val(node* root, int left, int right, int val) {
  57.  
  58. if (root->left > right || root->right < left) {
  59. return;
  60. }
  61.  
  62. if (left <= root->left && root->right <= right) {
  63. root->real_val = val;
  64. return;
  65. }
  66.  
  67. set_val(root->left_child, left, right, val);
  68. set_val(root->right_child, left, right, val);
  69. }
  70.  
  71. signed main() {
  72.  
  73.  
  74. int n;
  75. cin >> n;
  76.  
  77. vector<int> a(n);
  78.  
  79. for (int i = 0; i < n; i++)
  80. cin >> a[i];
  81.  
  82. int m;
  83. cin >> m;
  84.  
  85. node *root = build(a, 0, n - 1);
  86.  
  87. for (int i = 0; i < m; i++) {
  88. char t;
  89. cin >> t;
  90.  
  91. if (t == 'g') {
  92. int ind;
  93. cin >> ind;
  94. ind--;
  95. cout << get_val(root, ind) << ' ';
  96. } else {
  97. int l, r, x;
  98. cin >> l >> r >> x;
  99.  
  100. l--, r--;
  101. set_val(root, l, r, x);
  102. }
  103. }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement