SHARE
TWEET

Sagment_Tree_min_range_query

mias7246 Oct 23rd, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<bits/stdc++.h>
  2. #define mx 100001
  3. using namespace std;
  4. int ara[mx];
  5. int tree[mx*4];
  6. void init(int node,int b,int e){
  7.     if(b==e){
  8.         tree[node]=ara[b];
  9.         return ;
  10.     }
  11.     int mid=(b+e)/2;
  12.     init(2*node,b,mid);
  13.     init(2*node+1,b=mid+1,e);
  14.     tree[node] =min(tree[2*node],tree[2*node+1]);
  15. }
  16.  
  17.  
  18. int query(int node,int b,int e,int i,int j){
  19.     if(i>e || j<b)
  20.         return INT_MAX;
  21.     int mid =(b+e)/2;
  22.     if(b>=i && e<=j)
  23.         return tree[node];
  24.     int p1=query(2*node,b,mid,i,j);
  25.     int p2=query(2*node+1,mid+1,e,i,j);
  26.     return min(p1,p2);
  27. }
  28.  
  29. int main(){
  30.     int n,q,l,r,t;
  31.    scanf("%d", &t);
  32.     for(int tt=1;tt<=t;tt++){
  33.     scanf("%d%d",&n,&q);
  34. for(int i=1;i<=n;i++) scanf("%d",&ara[i]);
  35. init(1,1,n);
  36. printf("Case %d:\n",tt);
  37. for(int i=1;i<=q;i++){
  38.     scanf("%d%d",&l,&r);
  39.     printf("%d\n",query(1,1,n,l,r));
  40.  
  41. }
  42.     }
  43. return 0;
  44. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top