The tree array is suitable for dynamic continuous and query problems, that is, given an interval,
Query the value of a paragraph and or modify a location.
About the structure of the tree array please go to Baidu Encyclopedia, otherwise you will not understand the following content
Let's see the question.
Soldier Kills (ii)
time limit: + MS | memory limit: 65535 KB Difficulty: 5
Describe
South Generals have N soldiers, numbered 1 to N, each of these soldiers is known for their kills.
The staff is the South General's military advisers, the South General often want to know the number m to nth soldier's total kills, please help the handyman to answer the South General.
The South General's inquiry after the soldier I may also kill the q person, after the South General asked again, need to consider the new kill number.
Input
only one set of test data
The first line is a two integer n,m, where N represents the number of soldiers (1<n<1000000),M the number of bars representing the instruction. (1<m<100000)
the next line is N integers, andai represents the number of soldier kills. (0<=ai<=100)
the subsequent M - line is an instruction that contains a string and two integers, first a string, and if a string query indicates that the general South has a query operation, followed by two integers m,n, which represents the start and end of the query soldier number, if it is the string ADD followed by the two integer i,a (1<=i<=n,1<=a <=100), which indicates that The number of new kills for the first soldier is A.
Output
For each query, output an integer R that represents the total number of kills of soldier number m to nth soldier, one row per set of outputs
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 the typical dynamic interval continuous and query problem.
A tree array requires three functions
int lowbit (int x) {return x& (-X);//The value returned is 2 k power, K is the number of x binary from low to high 0}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 explain why X & X., the integers in the computer are in complement notation
So-X is actually bitwise reversed and then adds 1 to the end of the result, for example
32388 = 1001010110010000
-32388 = 0110101001110000
So the value returned by Lowbit is the value of x in the binary from the low to the right of the rightmost 1
The complete program is below
#include <stdio.h> #define Max_num 1000005int A[max_num] = {0};int c[max_num] = {0};int lowbit (int x) {return x& ;(-X);//The value returned is 2 k power, K is the number of x binary from low to high 0}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%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%d", &b, &e); if (order[ 0] = = ' Q ') {printf ("%d\n", SUM (e)-sum (b-1));} Else{change (b, E, n);}} return 0;}
Useful data Structure---tree array (binary index tree)