[Leetcode] Range Sum query-mutable

Source: Internet
Author: User

This problem requires a new data structure---Segment Tree. Geeksforgeeks article to get some ideas of it. However, the code in this article is too verbose. To solve this problem using segment tree, 2guotou have posted a very nice Java code, which are rewritten below in C + +. I try to conform to the leetcode traditions of defining structures and classes.

1 structSegmenttreenode {2     ints, e, sum;3segmenttreenode*Left ;4segmenttreenode*Right ;5Segmenttreenode (int_s,int_e): S (_s), E (_e), SUM (0), left (null), right (null) {}6 };7 8 classSegmenttree {9  Public:TenSegmenttree (vector<int>&nums) { One         intn =nums.size (); ARoot = Buildst (Nums,0N1); -     } -  the     voidUpdateintIintval) { - Update (root, I, Val); -     } -  +     intSumrange (intIintj) { -         returnSumrange (Root, I, j); +     } A Private: atsegmenttreenode*Root; -segmenttreenode* BUILDST (vector<int>& Nums,intSinte) { -         if(S > E)returnNULL; -         Else { -segmenttreenode* res =NewSegmenttreenode (S, e); -             if(s = = e) Res->sum =Nums[s]; in             Else { -                 intm = s + (e-s)/2; toRes->left =BUILDST (Nums, S, m); +Res->right = Buildst (nums, M +1, e); -Res->sum = Res->left->sum + res->right->sum; the             } *             returnRes; $         }Panax Notoginseng     } -     voidUpdate (segmenttreenode* root,intIintval) { the         if(Root->s = = root->e) Root->sum =Val; +         Else { A             intm = root->s + (root->e-root->s)/2; the             if(i <= m) update (root->Left , I, Val); +             ElseUpdate (root->Right , I, Val); -Root->sum = Root->left->sum + root->right->sum; $         } $     } -     intSumrange (segmenttreenode* root,intSinte) { -         if(Root->s = = S && root->e = = e)returnRoot->sum; the         Else { -             intm = root->s + (root->e-root->s)/2;Wuyi             if(e <= m)returnSumrange (root->Left , S, e); the             Else if(s >= m +1)returnSumrange (root->Right , S, e); -             Else returnSumrange (Root->left, S, m) + Sumrange (root->right, M +1, e); Wu         } -     } About }; $  - classNumarray { -  Public: -Numarray (vector<int>&nums) { ASt =NewSegmenttree (nums); +     } the  -     voidUpdateintIintval) { $St->update (I, Val); the     } the  the     intSumrange (intIintj) { the         returnSt->Sumrange (i, j); -     } in Private: thesegmenttree*St; the }; About  the  the //Your Numarray object would be instantiated and called as such: the //Numarray Numarray (nums); + //numarray.sumrange (0, 1); - //numarray.update (1, ten); the //Numarray.sumrange (1, 2);

[Leetcode] Range Sum query-mutable

Contact Us

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.