Tree-like array

Source: Internet
Author: User

The segment tree seems to have this unspeakable secret with a tree-like array.

The segment tree and tree array are seen recently. In my brain, a tree array is a special kind of segment tree.

Line segment Tree I wrote the article a long time ago. But with too little to forget every time.

So write and read a few times can remember, after all, write once once the harvest.


Now let's say a tree-like array.

Here's a look at my personal approach to a problem and learn something new. First to know the problem is to solve what kind of problem. Then we look at this method of thinking, do not think the problem is too complicated.

Let's take a tree-like array to give a sample.

1, a tree-like array is a data structure that is modified and queried with log (n) time. It is mainly used to find the first n subparagraphs, an interval and so on of the series.

2, the tree array is nothing complicated is three functions, with these three functions everything has been conquered. One is the function used for the calculation. A change function, a calculation and a function. It's so simple.


Let's take a look at this diagram and build the main idea.


This can be a little complicated.

The A array here is the array of arrays we entered,

C[1] "a[1" is stored,

C[2] "a[2]+a[1" is stored;

C[3] "a[3" is stored;

C[4] a[4]+c[3]+c[2] is a[1]+a[2]+a[3]+a[4];

Can be seen by the connection.

Let's start with how it's calculated.

Note that the number of our deposits starts from 1, not 0.

Let's take a look at how to deal with such a thing.

int lowbit (int n) {return n& (-n);}

The purpose of this function is to let you calculate. First of all, when I finish the example you will be clear.

void Modify (int pos,int num) {while (pos<=n) {s[pos]+=num;pos+=lowbit (pos);}}

This function is used to change a value by adding num to the number of POS locations of the array.

Here's the Lowbit function above.

Give me a sample.

Will C "5" +1. Then you need to call the Modify () function, we will first C "5" +1, and then because C "6" =a[5]+a[6], so C "6" also need to change. Then C "8" (assuming that the enemy law is not very clear we continue to look down), so in turn to join.

The 5 is how to the 6 and 6 is how to the 8 is used to lowbit () this function.

The following function is used to calculate the first n

int sum (int n) {int num=0;while (n>0) {num+=s[n];n-=lowbit (n);} return num;}

We'll take another example here and it might be easier to understand.

1 The first 8 items and can see C "8" directly or indirectly connected to all the numbers, then C "8" is the first 8 items and Lowbit (8) is 8 so 8-8=0 stop the loop.

2 The first 7 and see that C "7" Only stores the value of a[7] then it needs to add the value of the first 6 items, Lowbit (7) =1,7-1=6. So jump to the 6 position. C "6" value a[5]+a[6], we add the C "6" after the addition of the top 4. At this time lowbit (6) = 2, then 6-2=4, we add the value of C "4", we can also see. C[4] is a[4]+c[3]+c[2. a[1]+a[2]+a[3]+a[4];

And then we're done.

It should be clear.

A complete example of my own test.

#include <iostream>using namespace Std;int s[10000];int n;int lowbit (int n) {return n& (-n);} void Modify (int pos,int num) {while (pos<=n) {s[pos]+=num;pos+=lowbit (pos);}} int sum (int n) {int num=0;while (n>0) {num+=s[n];n-=lowbit (n);} return num;} int main () {int x;cin>>n;for (int i=1;i<=n;i++) {int x;cin>>x;modify (i,x);} int M;while (cin>>m) {cout<<sum (M) <<endl;}}

Okay, the tree-like array is finished. Let's take a look at the questions we can do.


1 is a single-point update for interval values.

That is, the value of the point that updates the order halfway.

This simply calls the MODIF () function.

2 is the interval update single-point evaluation. I guess this is a problem, in the 1-n color, look at a certain position painted a few times. Apply I-j each time. So we can put the array in the initial session of 0, assuming this is i-j, we will call modify (i,1), and modify (j,-1) is a bit ingenious.

3 is the problem of seeking the number of reverse order. I think I'll write an article about the number of reverse-order numbers in a tree-like array.

Well, thank you for sticking with it.


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.