Tree-like arrays and two-dimensional tree arrays

Source: Internet
Author: User

I always thought that the tree array could use a line of tree water to pass through, until I encountered a tree-shaped array template problem today.

Then began to seriously learn the tree-like array, suddenly found how to write QWQQQQ so well.

Part of the content transferred from https://www.cnblogs.com/hsd-/p/6139376.html

I. Tree-like array

The tree-like array is a data structure, and the core idea is to use the complement idea of the binary system.

The first is a tree-like array of structure diagrams

And then we deformed him.

Does it feel better to understand?

And then we label it.

The C array represents the value of the record, and a array represents the original sequence. Then is all about the tree-like array of the blog will have a C array of the display of the value of the Bo master more lazy do not write, specifically see recommended blog.

The most important nature is

c[i]=a[i-2^k+1]+a[i-2^k+2]+ ... A[i]; (k is the length of the binary of I from the lowest bit to the high position 0 consecutive)

It says that K is the lowest bit in the binary, the last 0 of the continuous length is 6 of the binary is 110, then K is 1, then we bring in:

C[6]=A[6-2+1]+A[6-2+2]=A[5]+A[6]

See, is not in line with the way, this is lowbit to achieve the function. So, what is the principle oflowbit implementation?

First, glue the code for the lowbit function.

1 int lowbit (int  k)2{3     return k& (-k); 4 }
Lowbit

What do you mean by k& (-K)?

K is the complement, that is, anti-code +1, anti-code is what Baidu, anyway is very simple.

Then you will find that you can take out the smallest one 1, and in the above example is 2, you can try again with a few sets of data, found Lowbit (i) =2^i.

4 (0100), anti-code 4 (0011), complement (0100), 0100&0100=0100, then lowbit (i) = 4, and according to the number of consecutive 0 digits, then k=2,2^2=4, is assumed to be established.

So, is it very clear?

In fact, there is a kind of lowbit,k& (k^ (k-1)), you can push your own hand.

Then there is a single point of modification , code

1 void Add (int x,int  val)2{3while     (x <=N)4    {5         tree[x]+=val; 6         x+=lowbit (x); 7     }8 }
single-point modification

Why is it i<=n? Because your single point of modification is to jump from the leaf node to the parent node, so from small to large.

1 intSumintx)2 {3     intans=0;4      while(x>0)5     {6ans+=Tree[x];7x-=lowbit (x);8     }9     returnans;Ten}
interval Query

Then why is the interval query i!=0? Because the interval query is jumping from parent node to child node, I want to keep decreasing, remember interval query is the prefix of query and, so to check [l,r] words need sum (r)-sum (L-1).

The above operation, first do a template problem P3374 "template" Tree array 1,

What if we want to change the interval? You're not going to be enumerating and then add (i), not t!

At this time we used a new idea, the difference of thought. The idea of difference is very common, as described below:

Array a[]={1,6,8,5,10}, then poor fractional group b[]={1,5,2,-3,5}

That is to say B[i]=a[i]-a[i-1];(a[0]=0), then A[i]=b[1]+....+b[i];(this very good proof).

If the interval [2,4] adds 2,

A array becomes a[]={1,8,10,7,10},b array becomes b={1,7,2,-3,3};

Found no, b array only b[2] and b[5] changed, because the interval [2,4] is also added 2, so within the interval b[i]-b[i-1] is constant.

So the interval [x, y] is modified, only modified b[x] and b[y+1]:

B[x]=b[x]+k;b[y+1]=b[y+1]-k;

So, we save the difference in the fractional group, and then we just change the L, with the value of the r+1, just fine.

What about a single point of enquiry ? Don't forget that the tree-like array is prefixed and! We direct sum (a).

What about the interval query under the difference? Direct Primitive Sequence a[r]-a[l-1]!

Two. Two-D tree-like array

A two-dimensional tree array is a matrix! You can do it in a matrix way!

In the Add and sum functions, two layers of loops, one layer y and one x, it's OK!

And then there's the two-bit tree array of all the code demos

#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<queue>using namespacestd;inta[ -][ -],n;intLowbit (intk) {    returnk& (-k);}voidChangeintIinty) {    intJ;  while(i<=N) {j=y;  while(j<=N) {a[i][j]++; J+=Lowbit (j); } I+=lowbit (i); }}intAskintIinty) {    intans=0; intJ;  while(i!=0) {J=y;  while(j!=0) {ans+=A[i][j]; J-=Lowbit (j); } I-=lowbit (i); }    returnans;}intMain () {intT; scanf ("%d",&T);  while(t--) {memset (A,0,sizeof(a)); intm; scanf ("%d%d",&n,&m);  while(m--)        {            Charopt; CIN>>opt; if(opt=='C')            {                intX1,x2,y1,y2; scanf ("%d%d%d%d",&x1,&y1,&x2,&y2); Change (x2+1, y2+1);                Change (X1,Y1); Change (X1,y2+1); Change (x2+1, y1); }            Else            {                intx, y; scanf ("%d%d",&x,&y); printf ("%d\n", ask (x, y) &1); }} printf ("\ n"); }}
Two-dimensional tree-like array

Tree-like arrays and two-dimensional tree arrays

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.