0% found this document useful (0 votes)
4 views1 page

Segment Tree Code

The document contains code for building a segment tree from an array and querying sum ranges in the segment tree. The build function recursively constructs the segment tree from the input array, and the query function recursively sums elements in the specified range.

Uploaded by

sanjaydosswork17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Segment Tree Code

The document contains code for building a segment tree from an array and querying sum ranges in the segment tree. The build function recursively constructs the segment tree from the input array, and the query function recursively sums elements in the specified range.

Uploaded by

sanjaydosswork17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

long build(long[] arr,int ind,int start,int end){

if(start == end){
tree[ind] = arr[start];
return tree[ind];
}
int mid = (start+end)/2;
tree[ind] = build(arr,2*ind+1,start,mid) + build(arr,2*ind+2,mid+1,end);
return tree[ind];
}

long query(long[] arr,int ind,int start,int end,int l,int r){


if(start>=l && end<=r)
return tree[ind];
if(end<l || start>r)
return 0;
int mid = (start+end)/2;
return query(arr,2*ind+1,start,mid,l,r) + query(arr,2*ind+2,mid+1,end,l,r);
}

You might also like