Tree array (Template)

Source: Internet
Author: User

One-dimensional and two-dimensional templates of tree Arrays

Template

 Int Lowbit ( Int X)
{
Return X & (-x );
}
Void Modify ( Int X, Int Add) // One-dimensional
{
While (X <= maxn)
{
A [x] + = add;
X + = lowbit (X );
}
}
Int Get_sum ( Int X)
{
Int Ret = 0 ;
While (X! = 0 )
{
RET + = A [x];
X-= lowbit (X );
}
Return RET;
}
Void Modify ( Int X, Int Y, Int Data) // Two-dimensional
{
For ( Int I = x; I <maxn; I + = lowbit (I ))
For ( Int J = y; j <maxn; j + = lowbit (j ))
A [I] [J] + = data;
}
Int Get_sum ( Int X, Int Y)
{
Int Res = 0 ;
For ( Int I = x; I> 0 ; I-= lowbit (I ))
For ( Int J = y; j> 0 ; J-= lowbit (j ))
Res + = A [I] [J];
Return Res;
}

Typical template questions

Hdu1166 one-dimensional deployment:

View code

# Include <stdio. h>
# Include < String . H>
# Include <stdlib. h>
Int C [ 50011 ], N;
Int Lowbit ( Int X) // Calculate lowbit
{
Return X & (-x );
}
Void Add ( Int I, Int Val) // Change element I to Val
{
While (I <= N)
{
C [I] + = val;
I + = lowbit (I );
}
}
Int Sum ( Int I) // Calculate the sum of the first I and
{
Int S = 0 ;
While (I> 0 )
{
S + = C [I];
I-= lowbit (I );
}
Return S;
}
Int Main ()
{
Int I, j = 0 , A, B, V, T, num;
Char STR [] = " Add " , Str1 [] = " Sub " , Str2 [] = " End " , Sub1 [ 6 ];

Scanf ( " % D " , & T );
While (T --)
{
Scanf ( " % D " , & N );
Printf ( " Case % d: \ n " , ++ J );
Memset (C, 0 ,Sizeof (C ));
For (I = 1 ; I <= N; ++ I)
{
Scanf ( " % D " , & V );
Add (I, V );
}
While ( 1 )
{
Scanf ( " % S " , Sub1 );
If (! Strcmp (str2, sub1 ))
Break ;
If (! Strcmp (sub1, STR ))
{
Scanf ( " % D " , & A, & B );
Add (A, B );
}
Else If (! Strcmp (sub1, str1 ))
{
Scanf (" % D " , & A, & B );
Add (A,-B );
}
Else {
Scanf ( " % D " , & A, & B );
Printf ( " % D \ n " , Sum (B)-sum (- 1 ));
}
}
}
Return 0 ;
}

 

Hdu2642 stars two-dimensional:

View code

# Include <iostream>
# Include <algorithm>
Using Namespace STD;
Const Int Maxn = 1010 ;
Int A [maxn] [maxn];
Bool B [maxn] [maxn];
Int Lowbit ( Int X)
{
Return X & (-x );
}
Void Modify ( Int X, Int Y, Int Data)
{
For ( Int I = x; I <maxn; I + = lowbit (I ))
For (Int J = y; j <maxn; j + = lowbit (j ))
A [I] [J] + = data;
}
Int Get_sum ( Int X, Int Y)
{
Int Res = 0 ;
For ( Int I = x; I> 0 ; I-= lowbit (I ))
For (Int J = y; j> 0 ; J-= lowbit (j ))
Res + = A [I] [J];
Return Res;
}
Int Main ()
{
Int N, x, y, X1, Y1;
Char STR [ 2 ];
Scanf ( " % D " , & N );
Memset (, 0 , Sizeof ());
Memset (B, False , Sizeof (B ));
While (N --)
{
Scanf ( " % S " , STR );
If (STR [ 0 ] = ' B ' )
{
Scanf ( " % D " , & X, & Y );
X ++; y ++;
If (B [x] [Y]) Continue ;
Modify (x, y, 1 );
B [x] [Y] = True ;
}
Else If (STR [ 0 ] = ' D ' )
{
Scanf ( " % D " , & X, & Y );
X ++; y ++;
If (! B [x] [Y]) Continue ;
Modify (X, Y ,- 1 );
B [x] [Y] =False ;
}
Else {
Scanf ( " % D " , & X, & X1, & Y, & Y1 );
X ++, y ++;
X1 ++, Y1 ++;
If (X> X1) Swap (x, X1 );
If (Y> Y1) Swap (Y, Y1 );
Int Ans = get_sum (x1, Y1)-get_sum (X- 1 , Y1)-get_sum (x1, y-1 ) + Get_sum (X- 1 , Y- 1 );
Printf ( " % D \ n " , ANS );
}
}
Return 0 ;
}
Related Article

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.