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 ;
}