[Line Segment tree] Line Segment Tree Series 0.2 Single Point modification interval summation line segment tree

Source: Internet
Author: User
1080 line segment tree exercisesDescription Description

There are n squares in a row. At the beginning, each grid contains an integer. Now, I want to dynamically raise some questions and modify them: the form of a question is to find the sum of all elements in a specific subinterval [a, B; the modified rule is to specify a lattice X and add or subtract a specific value. Now you are required to answer each question correctly. 1 ≤ n <100000, total number of questions and modifications m <10000.

Input description Input description

The first row of the input file is an integer N, followed by N integers in N rows, representing the original integer in the grid. The next positive integer is m, followed by M, which indicates M queries. The first integer indicates the query code and the query code 1 indicates the increase, the two numbers X and a indicate that A is added to the value at the position X. The query Code 2 indicates the sum of the intervals. The two integers A and B indicate that [, b.

Output description Output description

A total of m rows, each integer

Sample Input Sample Input

6

4

5

6

2

1

3

4

1 3 5

2 1 4

1 1 9

2 2 6

Sample output Sample output

22

22

Data range and prompt Data size & hint

1 ≤ n ≤ 100000, m ≤ 10000.

Code
# include # include # include # include using namespace STD; struct node {int left, right, value ;}; node point [1000000]; int father [1000000]; int g; void buildtree (int I, int left, int right) // build {FATHER [I] = (INT) floor (I)/2.0; point [I]. left = left; point [I]. right = right; point [I]. value = 0; If (Left = right) {FATHER [I] = I;} else {buildtree (I <1, left, (INT) floor (right + Left)/2.0); buildtree (I <1) + 1, (INT) floor (right + Left)/2.0) + 1, right) ;}}// synchronized void Update (int x, int add, int left, int right, int now) // Change {int mid = (left + right)> 1; point [now]. value + = add; If (right-left) <1) {G = left; return;} If (left <= x) & (x <= mid )) {Update (x, add, left, mid, 2 * Now);} If (Mid + 1) <= x) & (x <= right )) {Update (x, add, Mid + 1, right, 2 * Now + 1) ;}}// specify int search (int x, int left, int right, int begin, int end) // query {If (Left = begin) & (Right = END) {return point [X]. value;} int mid = (begin + end)> 1; int ans = 0; If (left> = begin) & (right <= mid )) {ans + = search (2 * X, left, right, begin, mid);} // else if (left> = Mid + 1) on the left) & (right <= END) {ans + = search (2 * x + 1, left, right, Mid + 1, end );} // else {ans + = search (2 * X, left, mid, begin, mid) on the Right; ans + = search (2 * x + 1, Mid + 1, right, mid + 1, end) ;}// return ans;} // returns int main () {int n, m, K; CIN> N; buildtree, n); For (INT I = 1; I <= N; I ++) {int ad; CIN> ad; Update (I, AD, 1, n, 1);} CIN> m; For (INT I = 1; I <= m; I ++) {int TMP; CIN> TMP; if (TMP = 1) {int X, AD; CIN> x> ad; Update (x, AD, 1, n, 1 );} if (TMP = 2) {int p, q; CIN> P> q; cout

[Line Segment tree] Line Segment Tree Series 0.2 Single Point modification interval summation line segment tree

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.