Advertisement
Josif_tepe

Untitled

Feb 26th, 2023
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <map>
  5. #include <cstring>
  6. #include <algorithm>
  7. #include <stack>
  8. #include <queue>
  9. using namespace std;
  10. typedef long long ll;
  11. const int maxn = 2e5 + 10;
  12. int arr[maxn];
  13. int segment_tree[3 * maxn];
  14.  
  15. void build_tree(int L, int R, int position) {
  16.     if(L == R) {
  17.         segment_tree[position] = arr[L];
  18.     }
  19.     else {
  20.         int middle = (L + R) / 2;
  21.         build_tree(L, middle, 2 * position);
  22.         build_tree(middle + 1, R, 2 * position + 1);
  23.         segment_tree[position] = min(segment_tree[2 * position], segment_tree[2 * position + 1]);
  24.     }
  25. }
  26. int query(int L, int R, int position, int i, int j) {
  27.     // L R i L R j L R
  28.     if(i <= L and R <= j) {
  29.         return segment_tree[position];
  30.     }
  31.     if(R < i or j < L) {
  32.         return 2e9;
  33.     }
  34.     int middle = (L + R) / 2;
  35.     return min(query(L, middle, 2 * position, i, j), query(middle + 1, R, 2 * position + 1, i, j));
  36. }
  37. int main() {
  38.     ios_base::sync_with_stdio(false);
  39.     int n, q;
  40.     cin >> n >> q;
  41.    
  42.     for(int i = 0; i < n; i++) {
  43.         cin >> arr[i];
  44.     }
  45.    
  46.     build_tree(0, n - 1, 1);
  47.    
  48.     for(int i = 0; i < q; i++) {
  49.         int a, b;
  50.         cin >> a >> b;
  51.         a--; b--;
  52.        
  53.         cout << query(0, n - 1, 1, a, b) << endl;
  54.     }
  55.     return 0;
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement