-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from arpit3018/master
Added SegmentTree.cpp
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include<iostream> | ||
#define max_in 1048575 | ||
using namespace std; | ||
|
||
void constructTree(int inp[],int segTree[],int low, int high, int pos) | ||
{ | ||
if(high == low) | ||
{ | ||
segTree[pos] = inp[low]; | ||
return; | ||
} | ||
int mid = (low + high)/2; | ||
constructTree(inp,segTree,low,mid,2*pos+1); | ||
constructTree(inp,segTree,mid+1,high,2*pos+2); | ||
segTree[pos] = segTree[2*pos+1] & segTree[2*pos+2]; | ||
} | ||
|
||
int minQuery(int segTree[],int qlow, int qhigh, int low, int high, int pos) | ||
{ | ||
if(qlow <= low && qhigh >= high ) //total overlap | ||
return segTree[pos]; | ||
if(qlow > low || qhigh < high) | ||
return max_in; | ||
int mid = (low + high)/2; | ||
return(minQuery(segTree,qlow,qhigh,low,mid,2*pos+1) & minQuery(segTree,qlow,qhigh,mid+1,high,2*pos+2)); | ||
} | ||
|
||
int main() | ||
{ | ||
int n = 3; | ||
int a[] = {1,2,3}; | ||
int seg[7]; | ||
for(int i=0;i<7;i++) | ||
seg[i] = max_in; | ||
|
||
constructTree(a,seg,0,n-1,0); | ||
cout<<minQuery(seg,1,3,0,n-1,0); | ||
for(int i=0;i<7;i++) | ||
cout<<seg[i]<<endl; | ||
} |