fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4.  
  5. ll *seg , *arr , *lazy;
  6.  
  7. void build(ll low , ll high , ll pos){
  8.  
  9. if(low>high)return;
  10.  
  11. if(low == high){
  12. seg[pos] = arr[low];
  13. return;
  14. }
  15.  
  16. int mid = (low + high)/2;
  17.  
  18. build(low , mid , 2*pos);
  19. build(mid+1 , high , 2*pos+1);
  20.  
  21. seg[pos] = min(seg[2*pos] , seg[2*pos+1]);
  22. }
  23.  
  24. ll query(ll low , ll high , ll qlow , ll qhigh , ll pos){
  25.  
  26.  
  27.  
  28. if(qlow>high || qhigh<low || low >high)return INT_MAX;
  29.  
  30. if(qlow<=low && qhigh>=high)return seg[pos];
  31.  
  32. ll mid = (low+high)/2;
  33.  
  34. return min(query(low , mid , qlow , qhigh , 2*pos) , query(mid+1 , high , qlow , qhigh , 2*pos+1));
  35. }
  36.  
  37. void init(ll n){
  38. arr = new ll[n];
  39. seg = new ll[4*n];
  40.  
  41. //memset(seg , 0 , sizeof(seg));
  42. //memset(arr , 0 , sizeof(arr));
  43.  
  44. }
  45.  
  46. int main(){
  47.  
  48. // #ifndef check
  49. // freopen("input.txt" , "r" , stdin);
  50. // #endif
  51.  
  52. ios::sync_with_stdio(0);
  53. cin.tie(0);
  54.  
  55. int t;cin>>t;
  56. for(int tc = 1; tc <=t ; tc++){
  57. cout<<"Scenario #"<<tc<<":"<<'\n';
  58. ll i,n,q;cin>>n>>q;
  59. init(n+5);
  60. for(i=0;i<n;i++)cin>>arr[i];
  61. build(0 , n-1 , 1);
  62. while(q--){
  63. ll low , high;
  64. cin>>low>>high;
  65. cout<<query(0 , n-1 , low-1 , high-1 , 1)<<'\n';
  66. }
  67. }
  68. return 0;
  69. }
Success #stdin #stdout 0s 3460KB
stdin
2

5 3

1 2 3 4 5

1 5

1 3

2 4

5 3

1 -2 -4 3 -5

1 5

1 3

2 4
stdout
Scenario #1:
1
1
2
Scenario #2:
-5
-4
-4