[POJ3321]Apple Tree

来源:互联网 发布:数据库安全设计原则 编辑:程序博客网 时间:2024/06/05 01:53

Time Limit: 2000MS
Memory Limit: 65536K

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
“C 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
“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, 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

题意:有n个点的有根树(根节点为1),起先每个点点权都为1,需要你支持两种操作:
C x : 若编号为x的点点权是1,则将点权改为0;否则改为1
Q x :询问以x为跟节点的子树中所有点的点权之和

题解:dfs序+线段树。

#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#include<cstdlib>#include<algorithm>#include<vector>#define LiangJiaJun main#define INF 1999122700using namespace std;struct poi{int l,r;}fg[100004];struct data{    int l,r,w;}tr[800004];int h[100004],ne;struct edge{    int to,nt;}e[250004];int dfn[100004],n,m,cnt=0,a[200004];void add(int u,int v){e[++ne].to=v;e[ne].nt=h[u];h[u]=ne;}void dfs(int x){     dfn[x]=++cnt;     fg[x].l=cnt;     for(int i=h[x];i;i=e[i].nt)if(!dfn[e[i].to])dfs(e[i].to);     fg[x].r=cnt;}void build(int k,int l,int r){     tr[k].l=l;tr[k].r=r;     if(l==r){        tr[k].w=a[l];return;     }     int mid=(l+r)>>1;     build(k<<1,l,mid);     build(k<<1|1,mid+1,r);     tr[k].w=tr[k<<1].w+tr[k<<1|1].w;}void change(int k,int pos){     int l=tr[k].l,r=tr[k].r;     if(l==pos&&r==pos){        tr[k].w^=1;return;     }     int mid=(l+r)>>1;     if(pos <= mid)change(k<<1,pos);     else change(k<<1|1,pos);     tr[k].w=tr[k<<1].w+tr[k<<1|1].w;}int query(int k,int a,int b){    int l=tr[k].l,r=tr[k].r;    if(l==a&&r==b)return tr[k].w;    int mid=(l+r)>>1;    if(b<=mid)return query(k<<1,a,b);    else if(a>mid)return query(k<<1|1,a,b);    else{        return query(k<<1,a,mid)+query(k<<1|1,mid+1,b);    }}int LiangJiaJun(){    scanf("%d",&n);    for(int i=1;i<n;i++){        int u,v;        scanf("%d%d",&u,&v);        add(u,v);add(v,u);    }    memset(dfn,0,sizeof(dfn));    dfs(1);    for(int i=1;i<=cnt;i++)a[i] = 1;    build(1,1,n);    scanf("%d",&m);    while(m --){        char s[4];int x;        scanf("%s",s+1);        scanf("%d",&x);        if(s[1]=='Q'){            printf("%d\n",query(1,fg[x].l,fg[x].r));        }        else{            change(1,fg[x].l);        }    }    return 0;}
原创粉丝点击