POJ 3321 Apple Tree

来源:互联网 发布:二元期权模拟软件 编辑:程序博客网 时间:2024/05/01 02:20
Apple Tree
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 16705 Accepted: 5051

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 has N forks which are connected by branches. Kaka numbers the forks by 1 to N and 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 integer N (N ≤ 100,000) , which is the number of the forks in the tree.
The following N - 1 lines each contain two 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 is either
"x" which means the existence of the apple on fork x has 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
"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, 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




很明显的一个维护单点查询区间的题目
但是一开始丝毫看不出来任何区间的意思 明明是棵树啊!!!

其实 有一种东西叫做 时间戳
这是什么呢?
时间戳就是按dfs序给到过的点打上标记
一个开始的时间 一个结束的时间
那么从开始时间到结束时间的这段时间就是它和它子节点的区间



#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int lim=200011;int sumv[lim<<2],bt[lim],et[lim];struct self{int x,y;}s[lim<<1];int first[lim<<1],nxt[lim<<1];int m,n,a,b,c;char chin;void pushup(int o){sumv[o]=sumv[o<<1]+sumv[o<<1|1];}int x,y;int query(int o,int l,int r){    if(l>=x&&r<=y)return sumv[o];    int m=(l+r)>>1,ret=0;    if(x<=m)ret+=query(o<<1,l,m);    if(y>m)ret+=query(o<<1|1,m+1,r);    return ret;}void update(int o,int l,int r){    if(l==r)    {        sumv[o]^=1;        return;    }    int m=(l+r)>>1;    if(x<=m)update(o<<1,l,m);    else update(o<<1|1,m+1,r);    pushup(o);}void makeside(int i,int x,int y){    s[i].x=x;s[i].y=y;    nxt[i]=first[x];    first[x]=i;}void build(int o,int l,int r){    if(l==r)    {        sumv[o]=1;        return;    }    int m=(l+r)>>1;    build(o<<1,l,m);    build(o<<1|1,m+1,r);    pushup(o);}bool flag[lim];int t;void maketree(int i){    flag[i]=1;    t++;    bt[i]=t;    for(int e=first[i];e!=-1;e=nxt[e])    if(!flag[s[e].y])maketree(s[e].y);    et[i]=t;}    int main(){    scanf("%d\n",&m);    memset(first,-1,sizeof(first));    memset(nxt,-1,sizeof(nxt));    for(a=1;a<m;a++)    {        scanf("%d %d\n",&x,&y);        makeside(a,x,y);        makeside(a+m,y,x);    }    maketree(1);    build(1,1,m);    scanf("%d\n",&n);    for(a=1;a<=n;a++)    {        scanf("%c %d\n",&chin,&b);        x=bt[b],y=et[b];        if(chin=='Q')printf("%d\n",query(1,1,m));        else update(1,1,m);    }    return 0;}



原创粉丝点击