Fenwick tree (on)

Fenwick tree (Binary Indexed Tree)

Brief introduction

Fenwick tree is a modify and query time complexity are \ (O (\ log_2 \! N) \) is a data structure. It supports query section and modify a single point of operation.
Ideologically, Fenwick tree similar to the tree line, also, code complexity is smaller than the tree line and space-saving than the tree line, can be extended to multi-dimensional case, but the scope is smaller than the tree line.
Different from the segment tree, the tree arrays without contribution, in use, its tree structure is an array of analog.

Structural Analysis

Look at a map:

This map shows the tree structure of the array (imaginary structure). Orange represents the node where the original array \ (A [I] \) , green represents the tree node array \ (C [I] \) , each node represents the number of the upper right corner of the node weights. You can find every green node weights are equal weight and its child nodes.
There are two important rule:
\ [C [I] = \ J = sum_ {I- \ OperatorName lowbit} {(I) + 1'd n-} ^ {} \ {A [J]} \ qquad (. 1)! \]
\ [\ sum_ ^ {J} = {I}. 1 \! A [J] = C [I] + C [I- \ OperatorName lowbit} {(I)] + C [I- \ OperatorName} {lowbit (i) - \ operatorname {lowbit
} (i- \ operatorname {lowbit} (i))] \ cdots \ qquad (2) \] where \ (I \) is a positive integer.
Before explaining these two laws, first look \ (\ operatorname {lowbit} ( i) \) is valid.

lowbit function

This is a black-bit computing technology, the function prototype is \ (the X-\) & \ (- the X-\) . It returns the first or less \ (X \) a \ (2 ^ k (k is a nonnegative integer) \) number.
For example, \ (X =. 4 \) , \ (\ OperatorName {lowbit} (X) =. 4 \) ; \ (X =. 7 \) , \ (\ OperatorName {lowbit} (X) =. 1 \) .
If you do not understand or Baidu about it

Explanation

To \ ((1) \) type of explanation:

For \ (C [I] \) , which corresponds to \ (A [I- \ OperatorName lowbit} {(I) + 1'd] + \ cdots + A [I] \) .
Therefore \ (A [i] \) plus (K \) \ , the need to make each containing a \ (A [i] \) of \ (C [j] \) plus \ (K \) .
It can be proved only \ (C [I] \) , \ (C [I + \ OperatorName {lowbit} (I)] \) , \ (C [I + \ OperatorName {lowbit} (I) + \ OperatorName {lowbit} (I + \ operatorname {lowbit} (i) )] \ cdots \) comprising \ (A [I] \) .
Example: \ (I = (. 6) _ {10} = (110) _2 \) , comprising \ (A [i] \) of \ (C [] \) have \ (C [. 6] \) , \ ( C [. 8] \) , \ (C [16] \) and the like.

To \ ((2) \) type of explanation:

Calculation \ (A [1] + \ cdots + A [i] \) when the \ (I \) subtracting \ (\ OperatorName lowbit} {(I) \) , to give a new \ (i_2 are used \) , then the \ (i_2 are used \) subtracting \ (\ OperatorName lowbit} {(i_2 are used) \) , and so on until \ (i- \ operatorname {lowbit} (i) \) is \ (0 \) so far.
Example: \ (I = (. 6) _ {10} = (110) _2 \) , \ (A [. 1] + \ cdots + A [. 6] = C [(110) _2] + C [(100) _2 ] = C [. 6] + C [. 4] = 13 is. 7 = + 20 is \) .
Let's look at the basic operation of Fenwick tree "single point of modification" and "range query" is how to achieve.

operating

A single point of modification

In the original array \ (A [i] \) of a position plus value, and maintain a tree array.
The above for \ ((1) \) explained formula, the following code can be obtained:

inline void add(int x,int k)//维护树状数组C,对应于原数组A的操作就是A[i]+=k
{
    for(;x<=n;x+=x&-x)//x & -x 就是lowbit()函数
        c[x]+=k;
}

Interval inquiry

Calculation \ (A [. 1] + \ cdots + A [I] \) .
The above for \ ((2) \) formula explanation, the following code can be obtained:

inline int ask(int x)//查询A[1]+···+A[x]的值
{
    int ans=0;
    for(;x;x-=x&-x)
        ans+=c[x];
    return ans;
}

If the above explanation you did not understand, can understand what these two pieces of code.

Variants

Modify the interval, a single point of inquiry

Here the idea of using the difference (the difference seems to be the most widely used applications)
we found that when \ (A [l] ~ A [j] \) when all add a value, the adjacent A [i] difference unchanged .
This inspired us to take advantage of differential conduct variant, it supports single query and modify the operating range.
Fenwick tree can be used to maintain the differential array of the original array.
Example: A [] = {1,2,3,5} , B [] = {1,1,1,2}, C [] = {1,2,1,5}.

template

LG3304 [template] Fenwick tree 1

Template title, the Code

#include<iostream>
using namespace std;
int n,m,a[1000005],tr[1000005];
inline void add(int x,int y)
{
    for(;x<=n;x+=x&-x)
        tr[x]+=y;
}
inline int ask(int x)
{
    int ans=0;
    for(;x;x-=x&-x)
        ans+=tr[x];
    return ans;
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=1,w;i<=n;i++)
    {
        scanf("%d",&a[i]);add(i,a[i]);
    }
    for(int i=1,t,x,y;i<=m;i++)
    {
        scanf("%d%d%d",&t,&x,&y);
        if(t==1)    add(x,y);
        else    printf("%d",ask(y)-ask(x-1));
    }
    return 0;
}

LG3368 [template] Fenwick tree 2

It is the title template, on the code

//在实际操作时其实用原数组计算出的差分就行了
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
long long a[500005],tr[500005],n,m;
inline void add(long long x,long long k)
{
    for(;x<=n;x+=x&-x)
        tr[x]+=k;
}
inline long long ask(long long x)
{
    int ans=0;
    for(;x;x-=x&-x)
        ans+=tr[x];
    return ans;
}
int main()
{
    long long t,x,y,k;
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        add(i,a[i]-a[i-1]);//a[i]-a[i-1]是差分的定义
    }
    while(m--)
    {
        cin>>t;
        if(t==1)
        {
            scanf("%lld%lld%lld",&x,&y,&k);
            add(x,k),add(y+1,-k);//差分的原理1:区间[l,r]的元素加k,在原数组要依次维护区间[l,r]中的
                                               每一个元素,在差分数组只要让b[l]+k,b[r]-k就可以了
        }
        else
        {
            scanf("%lld",&x);
            printf("%lld\n",ask(x));//差分的原理2:A[i]=B[1]+···+B[i]
        }
    }
    return 0;
}

Epilogue

Fenwick tree is still pretty amazing, the algorithm small compared with the tree line constants, code size, but also save space
Next Issue:

  • Implemented Fenwick tree \ (O (log_2 n) \ ) the query interval, the interval modification
  • RMQ solve the problem with Fenwick tree
  • Application Fenwick tree (Cantor expand, seeking reverse order, the whole arrangement of a number of techniques, etc.)
    \ [\ xrightarrow {\} qquad the To \; BE \; continued \ cdots \]

Guess you like

Origin www.cnblogs.com/LZShuing-xuan/p/12502897.html