Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
nandwalritik committed Jun 23, 2021
2 parents 4d6d1c4 + f0336d4 commit fcf00d2
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion trees/segmentTree/segmentTreeRangeSum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,63 @@ void update(ll parent,ll start,ll end,ll pos,ll val,ll level){
segment_tree[parent] = segment_tree[2*parent+1]^segment_tree[2*parent+2];
return;
}
*/

/*
--------------------------------------ITERATIVE IMPLEMENTATION------------------------------------
class NumArray {
public:
vector<int> tree;
int n;
NumArray(vector<int>& nums) {
n = nums.size();
tree.resize(2*n);
buildTree(nums);
}
void buildTree(vector<int> &nums){
// int n = nums.size();
for(int i=n,j=0;i<2*n;i++,j++){
tree[i]=nums[j];
}
for(int i=n-1;i>0;i--){
tree[i]=tree[i*2]+tree[i*2+1];
}
}
void update(int index, int val) {
index+=n;
tree[index]=val;
while(index>0){
int left = index;
int right=index;
if(index%2 == 0)
right=index+1;
else
left=index-1;
tree[index/2]=tree[left]+tree[right];
index/=2;
}
}
int sumRange(int left, int right) {
left+=n;
right+=n;
int sum=0;
while(left<=right){
if(left%2){
sum+=tree[left++];
}
if(right%2 == 0)
{
sum+=tree[right--];
}
left/=2;
right/=2;
}
return sum;
}
};
*/

struct SegmentTree{
/*Using one based indexing*/
vector<int> tree;
Expand Down Expand Up @@ -153,4 +208,4 @@ int main(){


return 0;
}
}

0 comments on commit fcf00d2

Please sign in to comment.