Tree-like array

Source: Internet
Author: User

tree-like array

I thought it was very difficult, but I didn't think it was difficult.

Figure is basically like this, is not much like a tree ...

An array is an array of normal inputs, and the C array is the requested tree.

C[1]=A[1];C[2]=C[1]+A[2];

C[3]=A[3];C[4]=C[2]+C[3]+A[4];

C[5]=A[5];C[6]=C[5]+A[6];

C[7]=A[7];C[8]=C[4]+C[6]+C[7]+A[8];

A tree array typically has only two operations:

1. Add a value to a number.

2. Query the and of all values for an interval.

We will find that the value of a certain number is increased, and then the value of all points connected to it must be added, how to achieve it?

At this point, we can see the lowbit of the binary system, that is, the use of the.

For example:

After adding a value of Node 1 to hahaha, the corresponding node 2, node 4, and Node 8 are incremented. At this point, we can use the lowbit.

Lowbit (x) =x&-x;

1 of the binary is 0001,-1 is 1111 (a number plus a minus sign is the binary inverse of this number + 1), they &, or 0001, or 1. The original 1 plus the obtained 1, the result is 2. (This example is not very successful, go on ha)

2 of the binary is 0010,-2 is 1110, they both &, or 0010, that is 2. The original 2 plus the obtained 2, the result is 4. (or not, then continue)

4 of the binary is 0100,-4 is 1100, they both &, or 0100, that is 4. The original 4 plus the obtained 4, the result is 8. (。。。。。。 )

It seems that the whole example is like this, then one more casually:
7 binary is 0111,-7 is 1001, they both &, is 0001, that is 1. The original 7 plus the obtained 1, the result is 8, is to increase the next node. (Finally, yes.)

So, the code is attached:

int Add (int s,int  num) {    while (s<=N) {        Tree[s]+ = num;        S+=s&-s;    }}

Then the query, in fact, is similar, but the original is the reverse of the good. The original is added Lowbit, now change to reduce, push back just fine.

To query the code for an interval:

int Read (int x,int  y) {    x--;    Sum1=0;    sum2=0;      while (x) {        sum1+ =tree[x];        X-=x&-x;    }      while (y) {        sum2+ =Tree[y];        Y-=y&-y;    }     sum2-sum1;}

Rokua has two tree-like arrays of template titles: Tree array 1 and tree-like array 2.

The first question is more normal:

1. Add the number of X to K.

2. The and of each number in the output interval [x, y].

Code:

1#include <cstdio>2#include <iostream>3 using namespacestd;4 intn,m,a,tree[500001],b,xx,yy,sum1,sum2;5 intAddintSintnum) {6      while(s<=N) {7tree[s]+=num;8s+=s&-s;9     }Ten } One intReadintXinty) { Ax--; -sum1=0; -Sum2=0; the      while(x) { -sum1+=Tree[x]; -x-=x&-x; -     } +      while(y) { -sum2+=Tree[y]; +y-=y&-y; A     } atprintf"%d\n", sum2-sum1); - } - intMain () { -scanf"%d%d",&n,&m); -      for(intI=1; i<=n;++i) { -scanf"%d",&a); in Add (i,a); -     } to      for(intI=1; i<=m;++i) { +scanf"%d%d%d",&b,&xx,&yy); -         if(b==1) the Add (xx,yy); *         Else $ read (XX,YY);Panax Notoginseng     } -     return 0; the}
View Code

The second question is disgusting:

1. Add k to each number in the interval [x, y].

2. Output the value of number x.

So we can get the value of this number by having all the numbers of all the intervals before each number. (The language is too poor, hopeless)

Explain it with an example:

The value of the 8th node is c[8]. The value of the 7th node is c[4]+c[6]+c[7].

The value of the 6th node is c[4]+c[6]. The value of the 5th node is c[4]+c[5].

The value of the 4th node is c[4]. The value of the 3rd node is c[2]+c[3].

The value of the 2nd node is c[2]. The value of the 1th node is c[1].

So the code becomes:

1#include <cstdio>2 #defineLowbit (x) x&-x3 intn,m,sum,x,y,z;4 intc[500001];5 voidAddintXintd) {6      while(x<=N) {7c[x]+=D;8x+=x&-x;9     }Ten } One intReadintx) { Asum=0; -      while(x) { -sum+=C[x]; thex-=x&-x; -     } -printf"%d\n", sum); - } + intMain () { -     Long LongK; +scanf"%d%d",&n,&m); A      for(intI=1; i<=n;i++){ aty=x; -scanf"%d",&x); -Add (i,x-y); -     } -      for(intI=1; i<=m;i++){ -scanf"%d",&z); in         if(z==1){ -scanf"%d%d%d",&x,&y,&k); to Add (x,k); +Add (y+1,-k); -         } the         Else{ *scanf"%d",&x); $ read (x);Panax Notoginseng         } -     } the     return 0; +}
View Code

The first time to write such a long blog, exhausted.

In addition, this blog is well written, I also learned through this blog tree-like array.

Tree-like array

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.