AC diary--apple tree Codevs 1228

Source: Internet
Author: User

1228 apple trees

time limit: 1 sspace limit: 128000 KBtitle level: Diamonds Diamond SolvingView Run ResultsTitle Description Description

Outside the Kaka's house, there is an apple tree. Every spring, there is always a lot of apples on the tree. Kaka was very fond of apples, so he always cared for the apple tree. We know that there are a lot of fork points in the tree, Apple will be on the fork point of the branches, and no more than two apples knot together. Kaka would like to know the number of apples on the subtree represented by a fork point, so as to study which branches of the apple tree have a stronger result.

What Kaka knows is that some of the apples will be on some forks at some point, but what Kaka doesn't know is that there are always some naughty kids to pick some apples off the tree.

So we define two operations:

C x

Indicates that the state of the fork point with the number x is changed (the original is an apple, it is removed, the original is not, the knot an apple)

G x

How many apples are there in the subtree represented by a fork point that has a number x?

We assume that at the outset, all the trees were apples, and also included the fork 1 as the root node.

Enter a description Input Description

First row one number n (n<=100000)

Next n-1 line, 2 number u,v per line, indicates that the fork point U and Fork Point v are directly connected.

Next line a number M, (m<=100000) indicates the number of queries

The next M-line, which represents the query, asks the format as described in the question Q X or C x

Output description Output Description

For each q x query, output the corresponding result, each line output a

Sample input Sample Input

3

1 2

1 3

3

Q 1

C 2

Q 1

Sample output Sample Output

3

2

Ideas:

First, the question is a tree.

The numbering order of his points is not arranged according to the order of the nodes in the segment tree.

So we're going to try and get this node into the line tree.

How to fit into line tree?

First, save the dots and the edges.

And then we're thinking about saving the node as a linear interval.

Defines a CNT to record the number of the current node and the point it is attached to (excluding the node he is extending).

And then start traversing from the first node

Extend the DFS sequence and keep the new point in the interval

And then all the points that the nodes can extend include themselves as an interval.

And then each node is just the first value of its interval.

Then the line tree completes the single-point modification and the interval query

Easy water Over

Come on, on the code:

#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;structT_tree {intL,r,dis;};structT_tree tree[400001];structT_edge {int  from, To,next;};structT_edge edge[100001];structT_interval {intLi,ri;};structT_interval find_[100001];intN,m,num_edge=0, head[100001],tot=0; inlinevoidEdge_add (int  from,intTo ) {Num_edge++; Edge[num_edge].to=to ; Edge[num_edge]. from= from; Edge[num_edge].next=head[ from]; head[ from]=Num_edge;}voidDfsintNow ) {Tot++; Find_[now].li=tot;  for(intI=head[now];i;i=Edge[i].next)    {DFS (edge[i].to); } Find_[now].ri=tot;}voidTREE_UP (intNow ) {Tree[now].dis=tree[now<<1].dis+tree[now<<1|1].dis;}voidTree_build (intNowintLintR) {TREE[NOW].L=l,tree[now].r=R; if(l==r) {Tree[now].dis=1; return ; }    intMid= (l+r) >>1; Tree_build ( now<<1, L,mid); Tree_build ( now<<1|1, mid+1, R);    TREE_UP (now); return ;}voidTree_change (intNowintTo ) {    if(to==tree[now].l&&tree[now].r==To ) {        if(Tree[now].dis) tree[now].dis=0; Elsetree[now].dis=1; return ; }    intMid= (TREE[NOW].L+TREE[NOW].R) >>1; if(To>mid) Tree_change (now<<1|1, to); ElseTree_change (now<<1, to); TREE_UP (now);}intTree_query (intNowintLintR) {    if(tree[now].l==l&&tree[now].r==r) {returnTree[now].dis; }    intMid= (TREE[NOW].L+TREE[NOW].R) >>1; if(L>mid)returnTree_query (now<<1|1, L,r); Else if(R<=mid)returnTree_query (now<<1, L,r); Else returnTree_query (now<<1, L,mid) +tree_query (now<<1|1, mid+1, R);}intMain () {scanf ("%d",&N); int  from, to;  for(intI=1; i<n;i++) {scanf ("%d%d",& from,&to ); Edge_add ( from, to); } DFS (1); Tree_build (1,1, N); scanf ("%d",&m); CharT_do; intto_ch;  for(intI=1; i<=m;i++)    {        //scanf ("%c%d", &t_do,&to_ch); //scanf ("%c", &T_DO); //scanf ("%d", &to_ch);Cin>>t_do>>to_ch; if(t_do=='Q') printf ("%d\n", Tree_query (1, Find_[to_ch].li,find_[to_ch].ri)); ElseTree_change (1, Find_[to_ch].li); }    return 0;}

AC diary-apple tree Codevs 1228

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.