Nyoj 116 tree Array

Source: Internet
Author: User

Tree array for beginners ..

 

 

This is a line segment tree. We can see from the figure that C [1] = A [1]; C2 = A [1] + A [2]; c [3] = A [3]; C [4] = A [1] + A [2] + A [3] + A [4]; c [5] = A [5];

C [6] = A [5] + A [6]; C [7] = A [7]; c [8] = A [1] + A [2] + A [3] + A [4] + A [5] + A [6] + A [7] + A [8];

We can obtain this rule. If I is an odd number, C [I] = A [I]. Here, 1, 3, 5, and 7 are examples. The even number is related to the power of 2. the maximum number of power of 2 in the I factor, for example, the power of 4 or 2 is 4, therefore, the number of a [4] is a total of 4 numbers, that is, a [1], a [2], a [3], a [4]. When I = 6, the maximum number of factors is 2. Therefore, when I = 8, the three power of 2 is equal to 8, that is, a [1] + ..... the value of a [8. according to the online query, the formula is

C [N] = a (n-a ^ k + 1) + ...... + A [n]; (k indicates the number of digits going from right to 0, that is, the first position where 1 appears, and the start point starts from 0)

Here we can use a function to calculate

Int lowbit (int x)

{

Return X & (-x );

}

How can this function be understood?

When x = 1, the binary representation of X is (assuming 8 digits) 0000 0001

-The binary value of X is 1111 1111.

---> 0000 0001 = 1;

When x = 6, the binary representation of X is 0000 0110.

-The binary value of X is 1111 1010.

---> 0000 0010 = 2;

Here I believe you understand that if you do not understand the binary representation of negative numbers, you can check it by Baidu.

The purpose of this function is to find 2 ^ K (k is the number of numbers that appear from 0 to 0 at the first position where 1 appears)

The following is the sum,

Int sum (int n)

{

Int sum = 0;

While (n> 0)

{

Sum + = C [N];

N = N-lowbit (N );

}

Return sum;

}

How can I understand it? We can think of it as finding the sum of a path, different from the ordinary one-dimensional array increasing or decreasing at a time according to the subscript of 1, here is according to the tree structure, each logn, that is, finding the path (X, x-lowit (x ),.... 1) is the lower mark and

Finally, modify the value of a node.

Void change (int I, int X)

{

While (I <= m) // sum of M nodes

{

C [I] = C [I] + X;

I = I + lowbit (I );

}

}

If the sum above is understood, this step should be quickly understood. If the value of a node is changed, the corresponding points on the corresponding path should also be changed. The condition for the end is to reach the root node.

Let's take a look at this question.

Link: http://acm.nyist.net/JudgeOnline/problem.php? PID = 1, 116

1. If a conventional method is adopted, the time complexity is O (n ^ 2 );

2. The AC code is as follows:

#include<iostream>#include<stdio.h>#include<cstring>using namespace std;int c[1000008],m,Q,t;int lowbit(int x){ return x&(-x);}int Sum(int n){ int sum=0; while(n>0) {  sum=sum+c[n];  n=n-lowbit(n); } return sum;}void change(int i,int x){ while(i<=m) {  c[i]=c[i]+x;  i=i+lowbit(i); }} int main(){ scanf("%d%d",&m,&Q); char key[10]; int start,end; for(int i=1;i<=m;++i) {   scanf("%d",&t);   change(i,t); }    while(Q--)    {  scanf("%s%d%d",key,&start,&end);     if(key[0]=='Q')     printf("%d\n",Sum(end)-Sum(start-1));     else if(key[0]=='A')     change(start,end); } return 0;}

Reference Source: http://www.cnblogs.com/zhangshu/archive/2011/08/16/2141396.html

Http://hi.baidu.com/czyuan_acm/item/b14bff6ab6ffd093c5d249db

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.