fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. int maxN = 2e5+1;
  6. int logN = 19;
  7.  
  8. int lookup[200001][19];
  9.  
  10. int main() {
  11.  
  12. int n,q;
  13. cin>>n>>q;
  14.  
  15. int arr[n];
  16. for(int i=0;i<n;i++)
  17. {
  18. cin>>arr[i];
  19. }
  20.  
  21. for(int i=0;i<n;i++)
  22. {
  23. lookup[i][0]=i;
  24. }
  25.  
  26. for(int j=1;(1<<j)<=n;j++)
  27. {
  28. for(int i=0; (i+(1<<j)-1)<n;i++)
  29. {
  30. if(arr[lookup[i][j-1]] <= arr[lookup[i+(1<<(j-1))][j-1]])
  31. {
  32. lookup[i][j] = lookup[i][j-1];
  33. }
  34. else
  35. {
  36. lookup[i][j] = lookup[i+(1<<(j-1))][j-1];
  37. }
  38. }
  39. }
  40.  
  41. /*for(int j=0;(1<<j)<=n;j++)
  42. {
  43. for(int i=0;i+(1<<j)-1 < n;i++)
  44. {
  45. cout<<lookup[i][j]<<" ";
  46. }
  47. cout<<endl;
  48. }*/
  49.  
  50.  
  51. for(int i=0;i<q;i++)
  52. {
  53. int a,b;
  54. cin>>a>>b;
  55.  
  56. int j = log2(b-a+1);
  57.  
  58. cout<<min(arr[lookup[a-1][j]],arr[lookup[b-(1<<j)][j]])<<endl;
  59. }
  60.  
  61.  
  62.  
  63. return 0;
  64. }
Success #stdin #stdout 0.01s 5288KB
stdin
8 4
3 2 4 5 1 1 5 3
2 4
5 6
1 8
3 3
stdout
2
1
1
4