Skip to content

Commit

Permalink
Merge pull request #26 from arpit3018/master
Browse files Browse the repository at this point in the history
Added SegmentTree.cpp
  • Loading branch information
i-vishi authored Oct 2, 2018
2 parents e7c81bf + 2a911f9 commit 420bed1
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Common Algorithms/segmentTree.cpp
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;
}

0 comments on commit 420bed1

Please sign in to comment.