Practical Data Structure-tree array (Binary Index Tree)

Source: Internet
Author: User

Practical Data Structure-tree array (Binary Index Tree)

The tree array is suitable for dynamic continuity and query problems, that is, a given interval,

Query and modify the values of a certain segment.

For the tree array structure, go to Baidu Baike; otherwise, you will not be able to understand the following content.

Let's look at this question.

Soldiers fight the enemy (2)

Time Limit: 1000 MS | memory limit: 65535 KB difficulty: 5

Description

There are N soldiers under general Nan, numbered 1 to N respectively. The number of these soldiers killing the enemy is known.

The minor engineer is a military division under general Nan. General Nan often wants to know the total number of enemies from general m to general n. Please help the minor engineer to answer the question.

After an inquiry by General Nan, the soldier I may have killed another q-man. Then, General Nan needs to consider the number of new enemies.

Input

Only one set of test data
The first line is two integers N, M, where N represents the number of soldiers (1 The next line is N integers, and ai indicates the number of attacked soldiers on the I. (0 <= ai <= 100)
Each row of the subsequent M line is an instruction, which contains a string and two integers. The first is a string. If it is a string QUERY, it indicates that General Nan has performed the QUERY operation, the two integers m, n, which indicate the start and end soldiers of the query. If the string is added, the two integers I, A (1 <= I <= N, 1 <= A <= 100), indicating that the number of new enemies of the I-th soldier is.

Output

For each query, an integer R is output to indicate the total number of enemies from soldiers m to soldiers n. Each group occupies one row.

Sample Input

5 6

1 2 3 4 5

QUERY 1 3

ADD 1 2

QUERY 1 3

ADD 2 3

QUERY 1 2

QUERY 1 5

Sample output

6

8

8

20

This is a typical problem of dynamic interval continuity and query.

Tree array requires three functions

Int lowBit (int x) {return x & (-x); // The returned value is the k power of 2, k is the number of zeros from the low position to the high position in x binary} int sum (int n) {int res = 0; while (n> 0) {res ++ = c [n]; n-= lowBit (n);} return res;} void change (int I, int x, int n) {if (I> n) return; c [I] + = x; change (I + lowBit (I), x, n );}

For lowBit, I would like to explain why x &-x is used as an integer in the computer to represent
Therefore,-x is actually the result of bitwise inversion and adding 1 at the end. For example
32388 = 1001010110010000
-32388 = 0110101001110000
Therefore, the value returned by lowBit is the value corresponding to 1 in the rightmost column from the low position to the high position in the binary system.
The complete procedure is as follows:

# Include
 
  
# Define MAX_NUM 1000005int a [MAX_NUM] = {0}; int c [MAX_NUM] = {0}; int lowBit (int x) {return x & (-x ); // The returned value is the k power of 2. k is the number from low to high 0 in the x binary system.} int sum (int n) {int res = 0; while (n> 0) {res + = c [n]; n-= lowBit (n);} return res;} void change (int I, int x, int n) {if (I> n) return; c [I] + = x; change (I + lowBit (I), x, n);} int main () {int n, m; scanf ("% d", & n, & m); int I; for (I = 1; I <= n; I ++) {scanf ("% d", & a [I]); if (I % 2! = 0) c [I] = a [I]; else {int j; for (j = I-lowBit (I) + 1; j <= I; j ++) c [I] + = a [j] ;}} char order [10]; int B, e, t = 0; for (I = 0; I <m; I ++) {scanf ("% s", & order); scanf ("% d", & B, & e ); if (order [0] = 'q') {printf ("% d \ n", sum (e)-sum (B-1 ));} else {change (B, e, n) ;}} return 0 ;}
 

Related Article

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.