leetcode@ [307] Range Sum query-mutable/segment Tree template

Source: Internet
Author: User

Given an integer array nums, find the sum of the elements between indices I and J (iJ), inclusive.

the update (I, Val) function modifies nums by updating the element on index i to Val.

Example:

Given nums = [1, 3, 5]sumrange (0, 2), 9update (1, 2) sumrange (0, 2), 8

Note:

1. The array is a modifiable by the update function.
2. Assume the number of calls to update and sumrange function is distributed evenly
Public classNumarray {PrivateSegmenttreenode root =NULL; Private intSize = 0;  PublicNumarray (int[] nums) {Root= Buildinsegmenttree (nums, 0, Nums.length-1); Size=nums.length; }    voidUpdateintIintval) {        if(i<0 | | i>=size)return;    Updateinsegmenttree (Root, I, Val); }     Public intSumrange (intIintj) {if(i>j | | i<0 | | j>=size)return-1; returnquerysum (Root, I, j); }        classsegmenttreenode{intLC = 0, rc = 0, sum = 0; Segmenttreenode Left=NULL, right =NULL; Segmenttreenode (intLintRintval) {LC= L; rc = r; sum =Val; }    }         PublicSegmenttreenode Buildinsegmenttree (int[]nums,intLintr) {if(L > R)return NULL; if(L = =r) {Segmenttreenode leaf=NewSegmenttreenode (L, R, Nums[l]); returnLeaf; } Segmenttreenode Root=NewSegmenttreenode (l, R, 0); intMid = (L + r) >> 1; Root.left=Buildinsegmenttree (Nums, L, mid); Root.right= Buildinsegmenttree (Nums, mid+1, R); Root.sum= Root.left.sum +root.right.sum; returnRoot; }         Public voidUpdateinsegmenttree (Segmenttreenode root,intIintval) {        if(ROOT.LC = = Root.rc && ROOT.LC = =i) {root.sum=Val; return; }                intMid = (root.lc + root.rc) >> 1; if(I >= root.lc && i <=mid) Updateinsegmenttree (Root.left, I, Val); ElseUpdateinsegmenttree (Root.right, I, Val); Root.sum= Root.left.sum +root.right.sum; }         Public intQuerysum (Segmenttreenode root,intIintj) {if(ROOT.LC = = I && root.rc = = j)returnroot.sum; intMid = (root.lc + root.rc) >> 1; if(I <= Mid && J <= mid)returnquerysum (Root.left, I, J); Else if(I > Mid && J > mid)returnquerysum (Root.right, I, J); Else returnQuerysum (Root.left, I, mid) + querysum (root.right, mid+1, J); }}//Your Numarray object would be instantiated and called as such://Numarray Numarray = new Numarray (nums);//numarray.sumrange (0, 1);//numarray.update (1, ten);//Numarray.sumrange (1, 2);
View Code

The following is a Java version of the Segmenttree template code (total two files: one is Segmenttreenode.java, the other is Segmenttree.java. ）

Packagecc150; Public classSegmenttreenode { Public intLC, RC, sum, add;        Segmenttreenode left, right;  PublicSegmenttreenode () { This. LC = 0; This. rc = 0; This. sum = 0; This. Add = 0;  This. left =NULL; This. right =NULL; }         PublicSegmenttreenode (intLintRintval) {         This. LC = L; This. rc = r; This. sum = val; This. Add = 0;  This. left =NULL; This. right =NULL; }         Public Static voidMain (string[] args) {//TODO auto-generated Method Stub    }}
Segmenttreenode.java

Segmenttree.java

[email protected] [307] Range Sum query-mutable/segment Tree template

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• Sales Support

1 on 1 presale consultation

• After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.