POJ topic 3321 Apple Tree (line segment tree)

Source: Internet
Author: User

Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 21566 Accepted: 6548

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 toN 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 double integers u and v, which means forku and for Kv 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 forkx, in Cluding 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

33Q 1C 2Q 1

Sample Output

32

Source

POJ monthly--2007.08.05, Huang, Jinsong

The topic gives you a tree, each node starts with an apple, the bottom m operation, Q A, the total number of apples to query a subtree of a and a, C B, modify the operation, change the b node,, start in there change no, not become have

AC Code

#include <stdio.h> #include <string.h>struct node{int u,v,next;} Edge[100010<<1];int head[100010],cnt,num[100010],son[100010],cc,vis[100100];void Add (int u,int v) {edge[cnt]. u=u;edge[cnt].v=v;edge[cnt].next=head[u];head[u]=cnt++;} void Dfs (int u) {num[u]=++cc;//son[u]=1;vis[u]=1;for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v;if (!VIS[V] ) Dfs (v);//son[u]+=son[v];} SON[U]=CC;} struct S{int sum,cover;} Node[100010<<2];void pushdown (int tr,int m) {if (node[tr].cover) {node[tr<<1].cover=node[tr<<1|1]. cover=1;node[tr<<1].sum=m-(m>>1); node[tr<<1|1].sum= (m>>1); node[tr].cover=0;} void build (int l,int r,int tr) {node[tr].cover=1;node[tr].sum=r-l+1;if (l==r) {return;} int mid= (L+R) >>1;build (l,mid,tr<<1); build (mid+1,r,tr<<1|1);} void update (int pos,int l,int r,int tr) {if (L==pos&&r==pos) {if (node[tr].cover) {node[tr].cover=0;node[tr].sum = 0;} Else{node[tr].cover=1;node[tr].sum=1;} return;} Pushdown (tr,r-l+1); int mid= (L+R) >>1; if (pos<=mid) update (pos,l,mid,tr<<1); Elseupdate (pos,mid+1,r,tr<<1|1);node[tr].sum=node[tr< <1].sum+node[tr<<1|1].sum;if (node[tr<<1].cover&&node[tr<<1|1].cover) {NODE[TR]. Cover=1;}} int query (int l,int r,int l,int r,int tr) {if (l<=l&&r>=r) {return node[tr].sum;} int mid= (L+R) >>1;pushdown (tr,r-l+1); int ans=0;if (L<=mid) ans+=query (l,r,l,mid,tr<<1); if (R>mid) Ans+=query (l,r,mid+1,r,tr<<1|1); return ans;/*if (R<=mid) return query (l,r,l,mid,tr<<1); ElseIf (l> Mid) return query (l,r,mid+1,r,tr<<1|1); Elsereturn query (l,mid,l,mid,tr<<1) +query (mid+1,r,mid+1,r,tr &LT;&LT;1|1); */}int main () {int n;while (scanf ("%d", &n)!=eof) {Cnt=0;cc=0;memset (head,-1,sizeof (head)); Memset ( Vis,0,sizeof (VIS)), int i;for (i=0;i<n-1;i++) {int a,b;scanf ("%d%d", &a,&b), add (b);} DFS (1), int m;scanf ("%d", &m), Build (1,n,1), while (m--) {char s[2];scanf ("%s", s), if (s[0]== ' Q ') {int a;scanf ("%d", &AMP;A); int Ans=query (Num[a],son[a], 1,n,1);p rintf ("%d\n", ans); Else{int a;scanf ("%d", &a); update (num[a],1,n,1);}}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ topic 3321 Apple Tree (line segment tree)

Related Article

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.