Apple Tree
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 27470   Accepted: 8140

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
【分析】题意很简单,这里就不多说了。做法是先dfs编号,找出每个节点所包含的节点编号区间,然后就是树状数组的事了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 2e9
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = 2e5+;
const int M = 4e5+;
int n,m,tot=,cnt=;
int head[N],x[N],y[N],r[N];
int tree[N];
struct EDG{
int to,next;
}edg[M];
inline void addedg(int u,int v){
edg[tot].to=v;edg[tot].next=head[u];head[u]=tot++;
}
void add(int k,int num){
while(k<=n){
tree[k]+=num;
//printf("####%lld\n",tree[k]);
k+=k&(-k);
}
}
int Sum(int k){
int sum=;
while(k>){
sum+=tree[k];
k-=k&(-k);
}
return sum;
}
void dfs(int u){
int v;
x[u]=++cnt;
for(int i=head[u];i!=-;i=edg[i].next){
v=edg[i].to;
if(!x[v])dfs(v);
}
y[u]=cnt;
return;
}
int main() {
met(tree,);met(head,-);met(x,);
int apple[N];
for(int i=;i<N;i++)apple[i]=;
int u,v;
char str[];
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
addedg(u,v);addedg(v,u);
}
dfs();
for(int i=;i<=n;i++)add(i,);
scanf("%d",&m);
while(m--){
scanf("%s",str);
scanf("%d",&u);
if(str[]=='C'){
int l=x[u],rr=y[u];
if(apple[l]){
apple[l]=;
add(l,-);
}else {
apple[l]=;
add(l,);
}
}else {
int ans=Sum(y[u])-Sum(x[u]-);
printf("%d\n",ans);
}
}
return ;
}

POJ 3321 Apple Tree(树状数组)的更多相关文章

  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 树状数组 第一题

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

  4. 3321 Apple Tree 树状数组

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

  5. POJ 3321:Apple Tree 树状数组

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

  6. POJ--3321 Apple Tree(树状数组+dfs(序列))

    Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Descripti ...

  7. 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. ...

  8. POJ3321 Apple Tree(树状数组)

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

  9. POJ 2486 Apple Tree [树状DP]

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

随机推荐

  1. 解决服务器时间Local time和RTC time不一致的问题

    今天遇到了一个问题,发现程序读取的时间比实际的时间快了几分钟. 执行 timedatectl status 发现 Local time比实际的快,但是RTC time是准确的. 各种google也没搞 ...

  2. HTML基础(1)

    1.前端开发语言介绍 HTML(Hypertext Markup Language)—— 结构—— 超文本标记语言 CSS(Cascading Style Sheets)—— 样式—— 层叠样式表 J ...

  3. 在caffe中使用hdf5的数据

    caffe默认使用的数据格式为lmdb文件格式,它提供了把图片转为lmdb文件格式的小程序,但是呢,我的数据为一维的数据,我也要分类啊,那我怎么办?肯定有办法可以转为lmdb文件格式的,我也看了一些源 ...

  4. spring aop搭建redis缓存

    SpringAOP与Redis搭建缓存 近期项目查询数据库太慢,持久层也没有开启二级缓存,现希望采用Redis作为缓存.为了不改写原来代码,在此采用AOP+Redis实现. 目前由于项目需要,只需要做 ...

  5. C语言输出字符串

    在VS2012中,使用gets_s()方法,其中第二个参数可以用sizeof(...)代替.例子代码如下: #include <stdio.h> int main( ) { ]; gets ...

  6. C语言->实验室->指针数组

    一 分析 讨论指针数组要从三个层面来考虑: 1)指针数组本身是什么 2)指针数组作为参数时的表现 3)指针数组作为返回值时的表现 二 指针数组是什么 1)指针数组--指针的集合 数组是若干元素的集合, ...

  7. 编程之美--2. Trie树 (Trie图)

    #1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助, ...

  8. 学习笔记Jmail收发邮件

    一.利用Jmail发送邮件  1/// <summary> 2/// 利用Jmail发送邮件 3/// </summary> 4private void SendMailByJ ...

  9. 【堆栈应用一】一个数divided=几个最小质因数的乘积(时间复杂度On)

    此算法由LQD提供

  10. postgres 查询数据库所有表名

    select relname as TABLE_NAME ,col_description(c.oid, 0) as COMMENTS from pg_class cwhere relkind = ' ...