Advertisement
Vince14

/<> 14438 (top down seg tree)

Sep 9th, 2023
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <vector>
  7. #include <set>
  8. #include <map>
  9. #include <stack>
  10. #include <queue>
  11. #include <deque>
  12. #include <unordered_map>
  13. #include <numeric>
  14. #include <iomanip>
  15. using namespace std;
  16. #define pii pair<long long, long long>
  17. #define ll long long
  18. #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL)
  19. const long long dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
  20. const long long dl[2] = {1, -1};
  21. const long long MOD = 1000000007;
  22. const long long MAXN = 100005;
  23.  
  24. int N, M;
  25. int arr[MAXN];
  26. int seg[MAXN * 4];
  27.  
  28. int build(int x, int s, int e){
  29.     if(s == e){
  30.         return seg[x] = arr[s];
  31.     }
  32.     int mid = (s + e)/2;
  33.     return seg[x] = min(build(x * 2, s, mid), build(x * 2 + 1, mid + 1, e));
  34. }
  35.  
  36. void update(int x, int s, int e, int idx, int val){
  37.     if(idx < s || e < idx) return;
  38.     if(s == e){
  39.         seg[x] = val;
  40.         return;
  41.     }
  42.     int mid = (s + e)/2;
  43.     update(x * 2, s, mid, idx, val);
  44.     update(x * 2 + 1, mid + 1, e, idx, val);
  45.     seg[x] = min(seg[x * 2], seg[x * 2 + 1]);
  46. }
  47.  
  48. int query(int x, int s, int e, int a, int b){
  49.     if(e < a || b < s) return 2e9;
  50.     if(a <= s && e <= b){
  51.         return seg[x];
  52.     }
  53.     int mid = (s + e)/2;
  54.     return min(query(x * 2, s, mid, a, b), query(x * 2 + 1, mid + 1, e, a, b));
  55. }
  56.  
  57. int main() {
  58.     FAST;
  59.     cin >> N;
  60.     for(int i = 1; i <= N; i++){
  61.         cin >> arr[i];
  62.     }
  63.     build(1, 1, N);
  64.     cin >> M;
  65.     for(int i = 0; i < M; i++){
  66.         int a, b, c;
  67.         cin >> a >> b >> c;
  68.         if(a == 1){
  69.             update(1, 1, N, b, c);
  70.         }
  71.         else{
  72.             cout << query(1, 1, N, b, c) << "\n";
  73.         }
  74.     }
  75. }
  76.  
  77. /*
  78. 5
  79. 5 4 3 2 1
  80. 6
  81. 2 1 3
  82. 2 1 4
  83. 1 5 3
  84. 2 3 5
  85. 1 4 3
  86. 2 3 5
  87.  */
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement