Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<algorithm>
  4. #define INT_MAX 1e18;
  5.  
  6. typedef long long ll;
  7.  
  8. using namespace std;
  9.  
  10. void updateQuery(vector<ll>& toQuery, ll s, ll e, ll indexToBeUpdated, ll newVal, ll index)
  11. {
  12. if (indexToBeUpdated > e || indexToBeUpdated < s)
  13. {
  14. return;
  15. }
  16.  
  17. if (s == e)
  18. {
  19. toQuery[index] = newVal;
  20. return;
  21. }
  22.  
  23.  
  24. ll mid = (s + e) / 2;
  25. updateQuery(toQuery, s, mid, indexToBeUpdated, newVal, 2 * index);
  26. updateQuery(toQuery, mid + 1, e, indexToBeUpdated, newVal, 2 * index + 1);
  27. toQuery[index] = min(toQuery[2 * index], toQuery[2 * index + 1]);
  28.  
  29. return;
  30. }
  31.  
  32.  
  33. ll minQuery(vector<ll> toQuery, ll qs, ll qe, ll s, ll e, ll currIndex)
  34. {
  35. if (s >= qs && e <= qe)
  36. {
  37. return toQuery[currIndex];
  38. }
  39.  
  40. if (e<qs || s>qe)
  41. {
  42. return INT_MAX;
  43. }
  44.  
  45. ll mid = (s + e) / 2;
  46. ll left = minQuery(toQuery, qs, qe, s, mid, 2 * currIndex);
  47. ll right = minQuery(toQuery, qs, qe, mid + 1, e, 2 * currIndex + 1);
  48. ll result = min(left, right);
  49.  
  50. return result;
  51.  
  52. }
  53.  
  54.  
  55. void buildtoQuery(vector<ll> arr, ll s, ll e, vector<ll>& toQuery, ll index)
  56. {
  57. if (s == e)
  58. {
  59. toQuery[index] = arr[s];
  60. return;
  61. }
  62.  
  63. ll mid = (s + e) / 2;
  64.  
  65. buildtoQuery(arr, s, mid, toQuery, 2 * index);
  66. buildtoQuery(arr, mid + 1, e, toQuery, 2 * index + 1);
  67.  
  68. toQuery[index] = min(toQuery[2 * index], toQuery[2 * index + 1]);
  69. }
  70.  
  71. int main()
  72. {
  73. ll n, q;
  74. cin >> n >> q;
  75.  
  76. ll i = n;
  77. vector<ll> arr, toQuery;
  78. arr.resize(n);
  79. toQuery.resize(4 * n + 3);
  80.  
  81. //...
  82.  
  83. for (i = 0; i<n; i++)
  84. cin >> arr[i];
  85.  
  86. //build toQuery ...
  87. buildtoQuery(arr, 0, n - 1, toQuery, 1);
  88.  
  89. while (q--)
  90. {
  91. ll decider, result;
  92. cin >> decider;
  93.  
  94. if (decider == 1)
  95. {
  96. //asking query
  97. ll qs, qe;
  98. cin >> qs >> qe;
  99. result = minQuery(toQuery, qs - 1, qe - 1, 0, n - 1, 1);
  100. cout << result << endl;
  101. }
  102.  
  103. else if (decider == 2)
  104. {
  105. //updation query;
  106. ll i, newVal;
  107. cin >> i >> newVal;
  108. updateQuery(toQuery, 0, n - 1, i - 1, newVal, 1);
  109. }
  110. }
  111. return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement