Last active
December 10, 2015 11:39
-
-
Save utkarshl/4429202 to your computer and use it in GitHub Desktop.
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
struct node{ | |
int val; | |
void split(node& l, node& r){} | |
void merge(node& a, node& b) | |
{ | |
val = min( a.val, b.val ); | |
} | |
}tree[1<<(n+1)]; | |
node range_query(int root, int left_most_leaf, int right_most_leaf, int u, int v) | |
{ | |
//query the interval [u,v), ie, {x:u<=x<v} | |
//the interval [left_most_leaf,right_most_leaf) is | |
//the set of all leaves descending from "root" | |
if(u<=left_most_leaf && right_most_leaf<=v) | |
return tree[root]; | |
int mid = (left_most_leaf+right_most_leaf)/2, | |
left_child = root*2, | |
right_child = left_child+1; | |
tree[root].split(tree[left_child], tree[right_child]); | |
node l=identity, r=identity; | |
//identity is an element such that merge(x,identity) = merge(identity,x) = x for all x | |
if(u < mid) l = range_query(left_child, left_most_leaf, mid, u, v); | |
if(v > mid) r = range_query(right_child, mid, right_most_leaf, u, v); | |
tree[root].merge(tree[left_child],tree[right_child]); | |
node n; | |
n.merge(l,r); | |
return n; | |
} | |
void mergeup(int postn) | |
{ | |
postn >>=1; | |
while(postn>0) | |
{ | |
tree[postn].merge(tree[postn*2],tree[postn*2+1]); | |
postn >>=1; | |
} | |
} | |
void update(int pos, node new_val) | |
{ | |
pos+=(1<<n); | |
tree[pos]=new_val; | |
mergeup(pos); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It should be range_query on line 22 and 23 instead of query.