Poj3321--apple Tree (tree-like array)

Source: Internet
Author: User

Description

There is a apple tree outside of Kaka ' s house. Every autumn, a lot of apples would grow in the tree. Kaka likes Apple very much, so he has been carefully nurturing the Big Apple tree.

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

The trouble is, a new Apple may grow in an empty fork some time and Kaka could pick an apple from the tree for his Desse Rt. Can you help Kaka?

Input

The first line contains a integer N (n≤100,000), which is the number of the forks in the tree.
The following N-1 lines each contain the integers u and V, which means fork U and Fork v is connected by a branch.
The next line contains an integer M (m≤100,000).
The following M lines each contain a message which is either
"C X" which means the existence of the Apple on Fork X have been changed. i.e. if there is a apple on the fork and then Kaka pick it; Otherwise a new Apple has grown on the empty fork.
Or
"Q X" which means an inquiry for the number of apples in the sub-tree above the fork X, including the Apple (if exists) on The fork X
Note The tree is full of apples at the beginning

Output

For every inquiry, the output of the correspond answer per line.
Sample Input

3
1 2
1 3
3
Q 1
C 2
Q 1
Sample Output

3
2

An apple tree, each fork and end point in the tree will grow apple, there are two types of operation
The C operation is to remove or grow the apple of a fork or end point, depending on whether there is an apple at this location before the operation, and all the nodes have Apple at the beginning.
Q operation is a subtree that asks for a fork. How many apples are there?

By deep search for each node to assign the initial and end of two labels, this number is equivalent to the jurisdiction, representing the nodes under this node within this range, that is, the entire subtree range. Assuming that the initial and end labels for a node are a-B, the total number of apples in this range is [1,b]-[1,a-1], and the tree-like array is used to maintain the interval.

#include <iostream> #include <cstring> #include <string> #include <vector> #include <queue > #include <cstdio> #include <set> #include <math.h> #include <algorithm> #include <queue&
Gt #include <iomanip> #include <ctime> #define INF 0x3f3f3f3f #define MAXN 500005 #define MOD 1000000007 using NA
Mespace std;
struct Node {int to,next;} edge[maxn<<2];
int n,c[maxn],head[maxn],e,cnt;
int ST[MAXN],EN[MAXN],VIS[MAXN],HAVE[MAXN];
    int lowbit (int x) {return x& (-x);} int sum (int x) {int ans=0;
    for (; x>0; x-=lowbit (x)) ans+=c[x];
return ans; } void Add (int x,int num) {for (; x<=n; x+=lowbit (x)) C[x]+=num;} void Addedge (int u,int v) {Edge[e].
    To=v;
    Edge[e].next=head[u];
head[u]=e++;
    } void Dfs (int u) {cnt++;
    st[u]=cnt;
    Vis[u]=1;
        for (int i=head[u]; i!=-1; i=edge[i].next) {int v=edge[i].to;
 if (!vis[v]) Dfs (v);   } en[u]=cnt;
        } int main () {while (~SCANF ("%d", &n)) {e=0;
        cnt=0;
        Memset (C,0,sizeof (c));
        memset (head,-1,sizeof (head));
        memset (vis,0,sizeof (VIS));
        int u,v;
            for (int i=1; i<n; ++i) {scanf ("%d%d", &u,&v);
            Addedge (U,V);
        Addedge (V,u);
        } dfs (1);
            for (int i=1; i<=n; ++i) {Add (i,1);
        Have[i]=1;
        } int m,x;
        scanf ("%d", &m);
            while (m--) {char op;
            cin>>op;
                if (op== ' C ') {scanf ("%d", &x);
                    if (Have[x]) {Add (st[x],-1);
                have[x]=0;
                    } else {Add (st[x],1);
                Have[x]=1; }} else {scanf ("%d ", &x);
            printf ("%d\n", SUM (En[x])-sum (st[x]-1));
}}} 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.