Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Please note that it's Function problem i.e.
- you need to write your solution in the form of Function(s) only.
- Driver Code to call/invoke your function would be added by GfG's Online Judge.*/
- /* The functions which
- builds the segment tree */
- int *constructST(int arr[],int n)
- {
- //Your code here
- int st[4*n]; //size
- for(int i=0;i<4*n;i++){
- st[i]=5000;
- }
- buildSegmentTree(st,arr,0,n,0);
- return st;
- }
- /* The functions returns the
- min element in the range
- from a and b */
- int RMQ(int st[] , int n, int a, int b)
- {
- //Your code here
- query(st,0,n,a,b);
- }
- }
- void buildSegmentTree(int st[],int arr[],int low ,int high,int pos){
- if(low==high){
- st[pos]=arr[low];
- return;
- }
- int mid =(low+high)/2;
- int left=buildSegmentTree(st,arr,low,mid,2*pos+1);
- int right=buildSegmentTree(sr,arr,mid+1,high,2*pos+2);
- st[pos]=min(left,right);
- }
- int query(int st[],int low , int high ,int a,int b){
- if(low<=a && high >=b){
- return st[low/2];
- }else if (high <a || low >b ){
- return 5000;
- }else{
- int mid =(low+high)/2;
- return min(query(st,low,mid,a,b),query(st,mid+1,high,a,b));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement