ACM poj 3321 apple tree (tree array)

Source: Internet
Author: User
Apple Tree
Time limit:2000 ms Memory limit:65536 K
Total submissions:11282 Accepted:3214

Description

There is an apple tree outside of Kaka's house. every autumn, a lot of apples will grow in the tree. kaka likes apple very much, so he has been carefully nurturing the big apple tree.

The tree hasNForks which are connected by branches. Kaka numbers the forks by 1NAnd the root is always numbered by 1. apples will grow on the forks and two apple won't grow on the same fork. kaKa wants to know how many apples are there in a sub-tree, for his study of the produce ability of the apple tree.

The trouble is that a new Apple may grow on an empty fork some time and Kaka may pick an apple from the tree for his dessert. Can you help Kaka?

Input

The first line contains an integerN(N≤ 100,000), which is the number of the forks in the tree.
The followingN-1 lines each contain two integersUAndV, Which means forkUAnd forkVAre connected by a branch.
The next line contains an integerM(M≤ 100,000 ).
The followingMLines each contain a message which is either
"CX"Which means the existence of the apple on forkXHas been changed. I. e. If there is an apple on the fork, then Kaka pick it; otherwise a new Apple has grown on the empty fork.
Or
"QX"Which means an inquiry for the number of apples in the sub-tree above the forkX, Including the apple (if exists) on the fork x
Note the tree is full of apples at the beginning

Output

For every inquiry, output the correspond answer per line.

Sample Input

31 21 33q 1C 2q 1

Sample output

 
32

Source

Poj monthly -- 2007.08.05, Huang, Jinsong
 /*  **************************************** *********
Poj 3321 apple tree
A tree has an apple, and each tree branch node has two statuses: Long apple and not long apple,
One operation can change the status of the apple on the branches,
Another operation is to query the number of all the apples on a tree branch node. Specific Practices
Is to do a DFS, write down the start time of each node start [I] and end time end [I],
The Start Time and end time of all descendants of node I should be at start [I]
And end [I], and use an array C [I] to record the number of apples attached to node I,
Then, use a tree array to count the total number of additional apples from start [I] to end [I. Here
You can use sum (start [I])-sum (end [I]-1) to calculate the statistical interval using a tree array.

**************************************** ********** */
# Include < Iostream >
# Include < Vector >
# Include < Stdio. h >
Using Namespace STD;
# Define Maxn 220000
Int C [maxn];
Typedef Vector < Int > Vct_int;
Vector < Vct_int > G (maxn / 2 );
Int Lowbit [maxn];
Int Start [maxn]; // DFS Start Time
Int End [maxn]; // End Time of DFS
Bool Hasapple [maxn / 2 ];
Int Ncount;
Void DFS ( Int V)
{
Start [v] = ++ Ncount;
For ( Int I = 0 ; I < G [v]. Size (); I ++ )
DFS (G [v] [I]);
End [v] = ++ Ncount;
}
Int Querysum ( Int P)
{
Int Nsum = 0 ;
While (P > 0 )
{
Nsum + = C [p];
P -= Lowbit [p];
}
Return Nsum;
}
Void Modify ( Int P, Int Val)
{
While (P <= Ncount)
{
C [p] + = Val;
P + = Lowbit [p];
}
}
Int Main ()
{
Int N;
Scanf ( " % D " , & N );
Int X, Y;
Int I, J, K;
// Graph Creation
For (I = 0 ; I < N - 1 ; I ++ )
{
Int A, B;
Scanf ( " % D " , & A, & B );
G [A]. push_back (B );
}
Ncount = 0 ;
DFS ( 1 );
// The subscript range of the original array to be processed in the tree array is 1 -- ncount
For (I = 1 ; I <= Ncount; I ++ )
{
Lowbit [I] = I & (I ^ (I - 1 ));
}
For (I = 1 ; I <= N; I ++ )
Hasapple [I] = 1 ;
Int M;
// Evaluate the C Array
For (I = 1 ; I <= Ncount; I ++ )
C [I] = I - (I - Lowbit [I] + 1 ) + 1 ;
Scanf ( " % D " , & M );
For (I = 0 ; I < M; I ++ )
{
Char CMD [ 10 ];
Int A;
Scanf ( " % S % d " , CMD, & A );
If (CMD [ 0 ] = ' C ' )
{
If (Hasapple [a])
{
Modify (start [a], - 1 );
Modify (end [a], - 1 );
Hasapple [A] = 0 ;
}
Else
{
Modify (start [a], 1 );
Modify (end [a], 1 );
Hasapple [A] = 1 ;
}
}
Else
{
Int T1 = Querysum (end [a]);
Int T2 = Querysum (start [a]);
Printf ( " % D \ n " , (T1 - T2) / 2 + Hasapple [a]);
}
}
Return 0 ;

}

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.