Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1.    
  2. #include <bits/stdc++.h>
  3. #define NMAX 100005
  4.  
  5. using namespace std;
  6.  
  7. ifstream f ("rmq.in");
  8. ofstream g ("rmq.out");
  9.  
  10. int n , m , x , y;
  11. int a[NMAX][20];
  12.  
  13. void Compute()
  14. {
  15.     for(int j = 1 ; (1 << j) <= n ; j++)
  16.         for(int i = 1 ; i + (1 << j) - 1 <= n ; i++)
  17.             if(a[i][j - 1] < a[i + (1 << (j - 1))][j - 1])
  18.                 a[i][j] = a[i][j - 1];
  19.             else a[i][j] = a[i + (1 << (j - 1))][j - 1];
  20. }
  21.  
  22. void Query(int c , int b)
  23. {
  24.     int k = log2(b - c + 1);
  25.  
  26.     if(a[c][k] < a[b - (1 << k) + 1][k])
  27.         g << a[c][k] << '\n';
  28.     else g << a[b - (1 << k) + 1][k] << '\n';
  29. }
  30.  
  31. int main()
  32. {
  33.     f >> n >> m;
  34.  
  35.     for(int i = 1 ; i <= n ; i++)
  36.         f >> a[i][0];
  37.  
  38.     Compute();
  39.  
  40.     for(int i = 1 ; i <= m ; i++)
  41.     {
  42.         f >> x >> y;
  43.         Query(x , y);
  44.     }
  45.  
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement