Lintcode:interval Sum II

Source: Internet
Author: User

Given An integer array in the construct method, implement-Methods query (start, end) and modify (index, value): for q Uery (start, end),returnThe sum from index start to index end in the given array. For modify (index, value), modify the number in the given index to Valuehave you met ThisQuestion in a real interview?Yesexamplegiven Array A= [1,2,7,8,5].query (0, 2),return10. Modify (0, 4), change a[0] from 1 to 4. Query (0, 1),return6. Modify (2, 1), change a[2] from 7 to 1. Query (2, 4),return14. Notewe suggest you finish problem Segment tree Build, Segment tree Query and Segment tree Modify first. Challengeo (LOGN) time forQuery and modify.

Segment Tree:

1  Public classSolution {2     /*need to use some attributes here*/3     classSegmenttreenode {4         Longsum;5         intstart;6         intend;7 Segmenttreenode left;8 Segmenttreenode right;9Segmenttreenode (intStartintend) {Ten              This. Sum = 0; One              This. Start =start; A              This. end =end; -              This. left =NULL; -              This. right =NULL; the         } -     } -      - Segmenttreenode Root; +  -     /** +      * @paramA:an Integer Array A      */ at      PublicSolution (int[] A) { -         //Write your code here -         if(A = =NULL|| a.length==0)return; -Root = Build (A, 0, A.length-1); -          -     } in      -      PublicSegmenttreenode Build (int[] A,intStartintend) { toSegmenttreenode cur =NewSegmenttreenode (start, end); +         if(start = = end) Cur.sum =A[start]; -         Else { the             intMid = (start + end)/2; *Cur.left =Build (A, start, mid); $Cur.right = Build (A, mid+1, end);Panax NotoginsengCur.sum = Cur.left.sum +cur.right.sum; -         } the         returncur; +     } A      the     /** +      * @paramStart, End:indices -      * @return: The sum from start to end $      */ $      Public LongQueryintStartintend) { -         //Write your code here -         returnQuerytree (root, start, end); the     } -     Wuyi      Public LongQuerytree (Segmenttreenode cur,intStartintend) { the         if(Cur.start==start && Cur.end==end)returncur.sum; -         intMid = (Cur.start + cur.end)/2; Wu         if(End <= mid)returnQuerytree (Cur.left, start, end); -         Else if(Start > Mid)returnQuerytree (cur.right, start, end); About         Else returnQuerytree (Cur.left, Start, mid) + Querytree (cur.right, mid+1, end); $     } -      -     /** -      * @paramindex, value:modify A[index] to value. A      */ +      Public voidModifyintIndexintvalue) { the         //Write your code here - Modifytree (root, index, value); $     } the      the      Public voidModifytree (Segmenttreenode cur,intIndexintval) { the         if(Cur.start = =cur.end) { theCur.sum =Val; -             return; in         } the         intMid = (Cur.start + cur.end)/2; the         if(Index <=mid) Modifytree (Cur.left, Index, Val); About         ElseModifytree (cur.right, Index, Val); theCur.sum = Cur.left.sum +cur.right.sum; the     } the}

Lintcode:interval Sum II

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.