-
Notifications
You must be signed in to change notification settings - Fork 15
segment trees questions added #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Manish2397
wants to merge
1
commit into
algoholics-ntua:master
Choose a base branch
from
Manish2397:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+138
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,59 @@ | ||
#include<bits/stdc++.h> | ||
#define lld long long int | ||
#define ld long int | ||
#define str string | ||
#define dbl double | ||
using namespace std; | ||
|
||
|
||
void buildSegmentTree(int tree[],int values[],int index,int start,int end){ | ||
if(start>end) return; | ||
if(start==end){ | ||
tree[index]=values[start]; | ||
} | ||
else{ | ||
int mid = (start+end)/2; | ||
|
||
//left subTree | ||
buildSegmentTree(tree,values,2*index,start,mid); | ||
//right subTree | ||
buildSegmentTree(tree,values,2*index+1,mid+1,end); | ||
|
||
tree[index] = tree[2*index]+tree[2*index+1]; | ||
} | ||
} | ||
|
||
void updateNode(int tree[],int s,int e,int index,int ind,int val){ | ||
if(ind<s or ind>e) return; | ||
else if(s==e){ | ||
tree[index] = val; | ||
return; | ||
} | ||
//left subtree call | ||
int mid = (s+e)/2; | ||
updateNode(tree,s,mid,2*index,ind,val); | ||
updateNode(tree,mid+1,e,2*index+1,ind,val); | ||
tree[index] = tree[2*index]+tree[2*index+1]; | ||
return; | ||
} | ||
|
||
int rangeSumQuery(int tree[],int index,int start,int end,int qStart,int qEnd){ | ||
if(qStart>end or qEnd<start) return 0; | ||
if(qStart<=start and qEnd>=end) return tree[index]; | ||
|
||
int mid = (start+end)/2; | ||
int left = rangeSumQuery(tree,2*index,start,mid,qStart,qEnd); | ||
int right = rangeSumQuery(tree,2*index+1,mid+1,end,qStart,qEnd); | ||
return left+right; | ||
|
||
} | ||
|
||
int main(){ | ||
int values[] = {1,3,2,-2,4,5,8,12,10,40}; | ||
int size = sizeof(values)/sizeof(values[0]); | ||
int *segmentTree = new int[4*size+1]; | ||
buildSegmentTree(segmentTree,values,1,0,size-1); | ||
updateNode(segmentTree,0,size-1,1,4,5); | ||
cout<<rangeSumQuery(segmentTree,1,0,size-1,0,4); | ||
return 0; | ||
} |
This file contains hidden or 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,79 @@ | ||
#include<bits/stdc++.h> | ||
#define lld long long int | ||
#define ld long int | ||
#define str string | ||
#define dbl double | ||
using namespace std; | ||
|
||
//to update in a given range | ||
void updateRange(int tree[],int s,int e,int index,int qs,int qe,int inc){ | ||
if(s>qe or e<qs){ | ||
return; | ||
} | ||
if(s==e){ | ||
tree[index]+=inc; | ||
return; | ||
} | ||
|
||
int mid = (s+e)/2; | ||
updateRange(tree,s,mid,2*index,qs,qe,inc); | ||
updateRange(tree,mid+1,e,2*index+1,qs,qe,inc); | ||
tree[index] = min(tree[2*index],tree[2*index+1]); | ||
return; | ||
} | ||
|
||
//to update at particular index | ||
void updateNode(int tree[],int s,int e,int index,int ind,int val){ | ||
if(ind<s or ind>e) return; | ||
else if(s==e){ | ||
tree[index] = val; | ||
return; | ||
} | ||
int mid = (s+e)/2; | ||
updateNode(tree,s,mid,2*index,ind,val); | ||
updateNode(tree,mid+1,e,2*index+1,ind,val); | ||
tree[index] = min(tree[2*index],tree[2*index+1]); | ||
return; | ||
} | ||
//building segment tree | ||
void buildSegmentTree(int tree[],int values[],int index,int start,int end){ | ||
if(start>end) return; | ||
if(start==end){ | ||
tree[index]=values[start]; | ||
} | ||
else{ | ||
int mid = (start+end)/2; | ||
|
||
//left subTree | ||
buildSegmentTree(tree,values,2*index,start,mid); | ||
//right subTree | ||
buildSegmentTree(tree,values,2*index+1,mid+1,end); | ||
|
||
tree[index] = min(tree[2*index],tree[2*index+1]); | ||
} | ||
} | ||
|
||
//give minimum element in given range | ||
int minElementQuery(int tree[],int index,int start,int end,int qStart,int qEnd){ | ||
if(qStart>end or qEnd<start) return INT_MAX; | ||
if(qStart<=start and qEnd>=end) return tree[index]; | ||
|
||
int mid = (start+end)/2; | ||
int left = minElementQuery(tree,2*index,start,mid,qStart,qEnd); | ||
int right = minElementQuery(tree,2*index+1,mid+1,end,qStart,qEnd); | ||
return min(left,right); | ||
|
||
} | ||
|
||
int main(){ | ||
int values[] = {1,3,2,-2,4,5}; | ||
int size = 6; | ||
int *segmentTree = new int[4*size+1]; | ||
buildSegmentTree(segmentTree,values,1,0,size-1); | ||
cout<<"minimum value is : "<<segmentTree[1]<<endl; | ||
cout<<minElementQuery(segmentTree,1,0,size-1,0,5)<<endl; | ||
updateNode(segmentTree,0,5,1,5,-8); | ||
updateRange(segmentTree,0,5,1,0,3,-10); | ||
cout<<minElementQuery(segmentTree,1,0,size-1,0,5)<<endl; | ||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example, in the generic segment tree described in my comment, line 22 would be:
tree[index] = f( tree[2*index], tree[2*index+1] );