POJ 3321 Apple tree (Trees array)

Source: Internet
Author: User

Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 20671 Accepted: 6257

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  are connected by a branch.
The next line contains an integer  m   ( m  ≤100,000).
the following  M  lines each contain a message which be either
" x strong> "which means the existence of the apple on fork  x  has 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
" x " which means an inquiry for the number of apples in the sub-tree above the F ork  x , including the apple (if exists) on the fork x
Note The tree was full of apples at the beginning

Output

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

Sample Input

33Q 1C 2Q 1

Sample Output

32

Source

POJ monthly--2007.08.05, Huang, Jinsong

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <queue> #include <stack> #include <vector> #include <set> #include <map > #define L (x) (x<<1) #define R (x) (x<<1|1) #define MID (x, y) ((x+y) >>1) #define EPS 1e-8typedef __ Int64 ll;using namespace std; #define INF 0x3f3f3f3f#define N 100005struct stud{//Pit Daddy Place is the vector will time out int to,next;} E[n*2];int e_num;int head[n];int c[n];int n;int le[n],ri[n];int num;int a[n];int vis[n];inline int lowbit (int x) {retur n x & (-X);}    void Dfs (int x) {le[x]=++num;    int i;        for (i=head[x];i!=-1;i=e[i].next) {int to=e[i].to;    DFS (to); } Ri[x]=num;}        void update (int x,int va) {while (x<=n) {c[x]+=va;    X+=lowbit (x);    }}int sum (int x) {int s=0;        while (x>0) {s+=c[x];    X-=lowbit (x); } return s;}    int main () {int i,j;    scanf ("%d", &n); {for (i=0;i<=n;i++) c[i]=0;        int u,v;        i=n-1;        e_num=0;        memset (head,-1,sizeof (head));            while (i--) {scanf ("%d%d", &u,&v);            E[e_num].to=v;            E[e_num].next=head[u];        head[u]=e_num++;            } for (i=1;i<=n;i++) {a[i]=1;        Update (i,1);        } num=0;        DFS (1);        scanf ("%d", &u);        Char ch[10];          while (u--) {scanf ("%s%d", ch,&v);                if (ch[0]== ' Q ') {int lee=sum (le[v]-1);                int rii=sum (ri[v]);            printf ("%d\n", Rii-lee);              } else {if (A[le[v]]) update (LE[V],-1);              else update (le[v],1);          A[LE[V]]=!A[LE[V]]; }}} return 0;}




POJ 3321 Apple tree (Trees array)

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.