1. an Application of the "tree array" data structure to an array containing n elements (a [1],..., a [k],..., a [n]): (1) obtain the sum of elements I to j, sum = a [I] +... + a [j]; (2) modify any element. For the preceding two operations, the time complexity of the sum operation is O (n ^ 2), and the time complexity of the modification operation is O (1 ). If a large number of summation operations (1) are performed in the system, the time complexity of the system performance is O (n ^ 2), and the performance is poor. You can use a tree array to make the time complexity of the preceding two operations O (nlogn ). 2. Tree Arrays: (1) each element in the tree array is the sum of one or more continuous elements in the original array. (2) When performing the continuous summation operation a [1] +... + a [n], you only need to sum the elements of a tree array. The time complexity is O (lgn) (3) when modifying an element a [I], you only need to modify the sum of several elements in the tree array. The time complexity is O (lgn), which is a tree array. The explanation is as follows: 1) a []: An array that stores the original data. (Operation (1) Calculate the sum of multiple consecutive numbers, operation (2) modify any element) e []: Tree array, any element e [I] may be the sum of one or more elements in the array. E [2] = a [1] + a [2]; e [3] = a [3]; e [4] = a [1] + a [2] + a [3] + a [4]. 2) e [I] is the sum of several elements in array? If k consecutive zeros exist at the end of the binary representation of the number I, e [I] is the sum of 2 ^ k elements in array, then e [I] = a [I-2 ^ k + 1] + a [I-2 ^ k + 2] +... + a [I-1] + a [I]. For example, 4 = 100 (2) e [4] = a [1] + a [2] + a [3] + a [4]; 6 = 110 (2) e [6] = a [5] + a [6] 7 = 111 (2) e [7] = a [7] 3) Follow-up: it can be understood as the Father's Day of a node. Is the closest to it, and the last digit of the number is 0 consecutive than it is its father, for example, e [2] is the successor of e [1; e [4] is the successor of e [2. For example, e [4] = e [2] + e [3] + a [4] = a [1] + a [2] + a [3] + a [4], e [2] and e [3] are followed by e [4]. The next step is to calculate the e array and add the calculated e [I] to their next step. Precursor: the number of the node precursor is smaller than the current node, the nearest node, and the last node is more than the current node. For example, the precursor of e [7] Is e [6], and the precursor of e [6] Is e [4]. The precursor is mainly to avoid repeated addition of elements in computing continuity and time. For example, Sum (7) = a [1] +... + a [7] = e [7] + e [6] + e [4]. (E [7] Is e [6], and e [6] Is e [4]). (lowbit (I) = (I-1) ^ I) & I; the precursor of node e [I] is e [I-lowbit (I)]; the precursor of node e [I] is e [I + lowbit (I)] 3. tree array sample code copy code 1 # include <iostream> 2 # include <stdio. h> 3 4 using namespace std; 5 6 int input (int *, int *, int); // input data 7 int calStageSum (int *, int ); /// calculate the tree array 8 int getSum (int *, int); // obtain the first n digits and 9 int updataElement (int *, int *, int, int); // update the element 10 11 int main () {12 int n; 13 int newValue; 14 cout <"Input the n (n> 3):"; 15 cin> n; 16 17 int * num = new int [n + 1]; 18 int * sum = new int [n + 1]; 19 20 cout <"Input" <n <"numbers" <endl; 21 input (num, sum, n); 22 calStageSum (sum, n ); 23 24 cout <"The sum of first three number:" <getSum (sum, 3) <endl; 25 26 cout <"Update the 2nd number value :"; 27 cin> newValue; 28 updataElement (sum, num, n, 2, newValue); 29 30 cout <"The sum of fir St three number: "<getSum (sum, 3) <endl; 31 32 delete [] num; 33 delete [] sum; 34 return 0; 35} 36 37 int input (int * num, int * sum, int n) {38 for (int I = 1; I <= n; I ++) {39 cin> num [I]; 40 sum [I] = num [I]; 41} 42 return 0; 43} 44 45 int calStageSum (int * sum, int n) {46 int lowbit; 47 int par; 48 for (int I = 1; I <= n; I ++) {49 lowbit = (I-1) ^ I) & I; 50 par = lowbit + I; // subsequent node id51 if (par <= n) {52 sum [par] = sum [par] + sum [I]; 53} 54} 55 return 0; 56} 57 58 int getSum (int * sum, int n) {59 int sumPreN = 0; 60 int lowbit = 0; 61 while (n! = 0) {62 sumPreN + = sum [n]; 63 lowbit = (n-1) ^ n) & n; 64 n = n-lowbit; /// precursor node id65} 66 return sumPreN; 67} 68 69 int updataElement (int * sum, int * num, int n, int pos, int newvalue) {70 int lowbit = 0; 71 int dis = newvalue-num [pos]; 72 num [pos] = newvalue; 73 sum [pos] = sum [pos] + dis; 74 75 while (true) {76 lowbit = (pos-1) ^ pos) & pos; 77 pos = pos + lowbit; // successor node id78 if (pos <= n) {79 sum [pos] = sum [pos] + dis; 80} 81 else82 break; 83} 84 return 0; 85}