Apple Tree

Time Limit: 2000MS Memory Limit: 65536K

Total Submissions: 22613 Accepted: 6875

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

3

1 2

1 3

3

Q 1

C 2

Q 1

Sample Output

3

2

题意:

就是给你一棵树,每一个结点一开始都有一个苹果,然后有两个操作,一个是改变一个结点的值。即有苹果变无苹果,无苹果变有苹果。另一个是求一个结点下面的子树有多少个苹果。这个题目,询问次数100000,显然要用树状数组。属于更新点,求区间和。要知道树状数组无非两种类型,更新点求区间,更新区间求点。此外还要会dfs序列。具体见代码里dfs()函数。dfs序列可以自己百度一下是什么。这道题目是最基础的在树的结构里用树状数组统计

#include <iostream>
#include <string.h>
#include <math.h> using namespace std; #define MAX 100010
int head[MAX];
int c[MAX];
int apple[MAX];
int tol;
int n,m;
int tag;
int _right[MAX];
int _left[MAX];
int ans;
int vis[MAX];
struct Node
{
int value;
int next;
}edge[MAX*2];
void modify(int x,int y)
{
edge[tol].value=y;
edge[tol].next=head[x];
head[x]=tol++;
}
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int num)
{
while(x<=n)
{
c[x]+=num;
x+=lowbit(x);
}
}
int sum(int x)
{
int _sum=0;
while(x>0)
{
_sum+=c[x];
x-=lowbit(x);
}
return _sum;
}
void dfs(int x)
{
vis[x]=1;
tag++;
_left[x]=tag;
int a=head[x];
while(a!=-1)
{
if(vis[edge[a].value]==0) dfs(edge[a].value); a=edge[a].next;
}
_right[x]=tag;
}
void init()
{
memset(c,0,sizeof(c));
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
}
int main()
{
int x,y;
char a;
int b;
scanf("%d",&n);
tol=0;
tag=0;
init();
for(int i=1;i<=n;i++)
{
update(i,1);
apple[i]=1;
}
for(int i=0;i<n-1;i++)
{
scanf("%d%d",&x,&y);
modify(x,y);
modify(y,x);
}
dfs(1);
scanf("%d",&m);
for(int i=0;i<m;i++)
{
getchar();
scanf("%c",&a);
scanf("%d",&b);
if(a=='C')
{
if(apple[b]==1)
{
update(_left[b],-1);
apple[b]=0;
}
else
{
update(_left[b],1);
apple[b]=1;
}
}
else
{
ans=sum(_right[b])-sum(_left[b]-1);
printf("%d\n",ans);
} }
return 0;
}

POJ--3321 Apple Tree(树状数组+dfs(序列))的更多相关文章

  1. POJ 3321 Apple Tree (树状数组+dfs序)

    题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...

  2. POJ 3321 Apple Tree 树状数组+DFS

    题意:一棵苹果树有n个结点,编号从1到n,根结点永远是1.该树有n-1条树枝,每条树枝连接两个结点.已知苹果只会结在树的结点处,而且每个结点最多只能结1个苹果.初始时每个结点处都有1个苹果.树的主人接 ...

  3. POJ 3321 Apple Tree(树状数组)

                                                              Apple Tree Time Limit: 2000MS   Memory Lim ...

  4. POJ 3321 Apple Tree 树状数组 第一题

    第一次做树状数组,这个东西还是蛮神奇的,通过一个简单的C数组就可以表示出整个序列的值,并且可以用logN的复杂度进行改值与求和. 这道题目我根本不知道怎么和树状数组扯上的关系,刚开始我想直接按图来遍历 ...

  5. E - Apple Tree(树状数组+DFS序)

    There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. ...

  6. 3321 Apple Tree 树状数组

    LIANJIE:http://poj.org/problem?id=3321 给你一个多叉树,每个叉和叶子节点有一颗苹果.然后给你两个操作,一个是给你C清除某节点上的苹果或者添加(此节点上有苹果则清除 ...

  7. POJ 3321:Apple Tree 树状数组

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22131   Accepted: 6715 Descr ...

  8. POJ3321 Apple Tree(树状数组)

    先做一次dfs求得每个节点为根的子树在树状数组中编号的起始值和结束值,再树状数组做区间查询 与单点更新. #include<cstdio> #include<iostream> ...

  9. POJ 2486 Apple Tree [树状DP]

    题目:一棵树,每个结点上都有一些苹果,且相邻两个结点间的距离为1.一个人从根节点(编号为1)开始走,一共可以走k步,问最多可以吃多少苹果. 思路:这里给出数组的定义: dp[0][x][j] 为从结点 ...

随机推荐

  1. 【scala】 scala xml 处理(⑨)

    1.scala 处理xml 2. 获取属性 3.修改节点 4.遍历 5.模式匹配 6.命名空间 7.文件加载 import scala.xml._ /** * @author xwolf * @sin ...

  2. 教你解锁被锁住的苹果mac电脑的文件跟文件夹,同时也可删除被锁的文件跟文件夹(转)

    在Mac OSX 下无法删除的文件可大概分为下列三种情形 1.档案(夹)被锁定 2.文件正在使用中 3.没有权限的档案(夹) 一.「 为什么档案会被锁定 」 1.个人自行替档案加上 2.在拷贝或是整理 ...

  3. java.lang.Class<T> -- 反射机制及动态代理

    Interface : Person package java_.lang_.component.bean; public interface Person { String area = " ...

  4. ios开发之--解决“Could not insert new outlet connection”的问题。

    在Xcode中,我们能够在StoryBoard编辑界面或者是xib编辑界面中通过“Control键+拖拽“的方式将某个界面元素和相应的代码文件连接起来.在代码文件里创建outlet. 只是.假设你的运 ...

  5. polarssl rsa & aes 加密与解密

    上周折腾加密与解密,用了openssl, crypto++, polarssl, cyassl, 说起真的让人很沮丧,只有openssl & polarssl两个库的RSA & AES ...

  6. underscore.js定义模板遇到问题:Uncaught TypeError: Cannot read property 'replace' of undefined

    代码正确缩进位置如下, extend "layout" block 'content',-> div ->'nihao' script id:"Invoice ...

  7. mysql报错“Starting MySQL...The server quit without updating PID file”处理

    http://blog.csdn.net/lzq123_1/article/details/51354179 注意:要将/usr/bin/mysql_install_db替换成 /usr/bin/my ...

  8. 一句话shell

    作者:NetSeek1.删除0字节文件find -type f -size 0 -exec rm -rf {} \; 2.查看进程按内存从大到小排列ps -e -o "%C : %p : % ...

  9. 如何构建日均千万PV Web站点 (一)

    其实大多数互联网网站起初的网站架构都是(Linux+Apache+MySQL+PHP). 不过随着时代的发展,科技的进步.互联网进入寻常百姓家的生活.所谓的用户的需求,铸就了一个个互联网大牛: htt ...

  10. Flask中向前端传递或者接收Json文件的方法

    1. 利用flask的request.form.get()方法 这一中方法主要利用flask的request.form.get方法,获得前端发送给后台的json文件 Python 端代码: @app. ...