first, about the Tree-like array
A tree array (Binary Indexed tree, or bit) is a data structure that modifies and queries the complexity of all o (logn). however, the tree array only supports Single-point modification, and when queried, the Tree-like array also requires the interval of the query to be added and Reduced. however, The tree-like array is often used instead of line-segment trees because it is easy to implement and takes up little space.
second, detailed tree-like Array
here, we define the original sequence as a, and the tree array as c, then there are:
where k is the binary representation of I at the end of the number of 0, for example: i=3 (101), when k=0;i=8 (1000), k=3.
Defines the function Lowbit (x) =2k (k is the number of the end of the 0 in the binary representation of x), using the nature of the machine complement to obtain:
1 int lowbit (int x)2{3 return x& (-x); 4 }
By definition, We can list (here we use a[i,j] to represent all elements of a[i]~a[j]:
c[1]=a[1]
c[2]=a[1,2]
c[3]=a[3]
c[4]=a[1,4]
c[5]=a[5]
c[6]=a[5,6]
c[7]=a[7]
c[8]=a[1,8]
c[9]=a[9]
c[10]=a[9,10]
......
first, by the definition of a tree array, we can get a property:
? a[x] appears in the tree array for the first time in c[x], and c[x] contains the right end of the interval x
In addition, by observing, we can find:
? a[x] only c[x], c[x+lowbit (x)], c[x+lowbit (x) +lowbit (x+lowbit (x))] ... Have an impact
Similar to the idea of binary splitting, we can also find:
? c[1]~c[x] can restore all the information of a[1]~a[x]
therefore, the spatial complexity of the tree array is o (n).
Then we can write the code that maintains the tree array (here our tree array query information is interval and):
1 void Update (int x,int val)2{3for (; x <=n;x+=lowbit (x))4 c[x]+=val; 5 return ; 6 }
next, if we ask for interval a[i,j], what do we do?
Think of this, a[i,j] and actually equal to a[1,j] and minus a[1,i-1], then we just need to know how to find a sequence prefix and on it.
first, by definition, We know:
? c[x] contains an interval length of lowbit (x)
In this nature, similar to maintenance operations, we can easily write code that asks for prefixes and:
1 int query (int x)2{3 int ans=0; 4 for (; x;x-=lowbit (x)) 5 ans+=c[x]; 6 return ans; 7 }
above, is the basic content of the Tree-like array, below we look at an Example.
third, the topic Description
A row of n squares, beginning with an integer in each lattice. Some questions and modifications are presented dynamically: the form of questioning is the summation of all elements in a particular sub-interval [a, b]; the modified rule is to specify a lattice x, plus or minus a specific value, A. You are now asked to make the right answer to each Question. 1≤n<100000, The total number of questions and modifications m<10000.
Input Description
The input file first behaves as an integer n, followed by n rows n integers, representing the original integers in the Lattice. followed by a positive integer m, followed by m-line, the m-query, the first integer to indicate the query code, the number 1 indicates an increase, the following two numbers x and a for the value on position x to increase a, ask the code 2 for the interval sum, followed by two integers for a and b, representing the interval between [a, a] and
Output Description
A total of M lines, each integer
Sample Input
6
4
5
6
2
1
3
4
1 3 5
2 1 4
1 1 9
2 2 6
Sample Output
22
22
Data Size & Hint
1≤n≤100000, m≤10000.
Attach the original title link →_→ |1080 line segment Tree Exercises | codevs, Algorithm Enthusiast Community
Although it is a line-of-tree exercise, we have said at the beginning that a tree array can replace a segment tree in some Cases.
Iv. implementation of the Code
The following is a tree-like array of single-point maintenance and a template for the interval and can be CodeVs1080. In fact, the core code has been shown above, here just to do a collation.
1#include <cstdio>2 Const intmaxn=1e5+Ten;3 intn,m;4 intLowbit (intx) {returnx& (-x);}5 intbit[maxn];6 voidUpdateintXintVal)7 {8 for(; x<=n;x+=lowbit (x))9bit[x]+=val;Ten } one intQueryintx) a { - intans=0; - for(; x;x-=lowbit (x)) theans+=bit[x]; - returnans; - } - intMain () + { -scanf"%d",&n); + for(intI=1; i<=n;++I) a { at inta; -scanf"%d",&a); - Update (i,a); - } -scanf"%d",&m); - for(intI=1; i<=m;++I) in { - intflag,l,r; toscanf"%d%d%d",&flag,&l,&r); + if(flag==1) Update (l,r); - Else the { * intAns=query (r)-query (l1); $printf"%d\n", ans);Panax Notoginseng } - } the return 0; +}
CodeVs1080 line segment tree Exercises
Weakly weak to say, the Konjac konjac code word is not easy, reproduced please indicate the source http://www.cnblogs.com/Maki-Nishikino/p/6217811.html
"tree-like array of data structures" from Zero-aware tree arrays