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. WEB项目 后台接收前端数组

    //保存区域选择的设备 $scope.saveDevice = function(){ var device = []; $("input[type='checkbox']:checked& ...

  2. MySQL学习笔记--数据类型

    一.数据类型(内容参考<SQL学习指南>)不完整 1.文本类型 文本类型 最大字节数 tinytext 255 text 65535 varchar 65536 mediumtext 16 ...

  3. Poisson回归模型

    Poisson回归模型也是用来分析列联表和分类数据的一种方法,它实际上也是对数线性模型的一种,不同点是对数线性模型假定频数分布为多项式分布,而泊松回归模型假定频数分布为泊松分布. 首先我们来认识一下泊 ...

  4. Mac环境下 配置Python数据分析环境

    采取的思路主要依据的是这一篇文章,连接: http://www.jb51.net/article/78667.htm 但是当安装brew的时候,可能是网站的问题,一直报错 所以从网上查找资料发现,br ...

  5. toolkit学习笔记

  6. Windows Store App 用户库文件分组

    在Windows应用商店应用程序中浏览用户库中的文件时,可以将文件或者文件夹分组显示,以便于进行分类浏览,这类似于音乐库中的文件可以按照艺术家名称.创建日期或者评级等多种方式进行分类.本节内容将会介绍 ...

  7. BZOJ2733 [HNOI2012]永无乡

    直接平衡树启发式合并就好了...貌似是个很高端的东西.. 貌似可以证明splay的启发式合并是均摊$O(nlogn)$的...而其他平衡树都不行,所以其他的复杂度都是$O(nlog^2n)的$的 所以 ...

  8. 快消零售行业怎么用K2做开关店管理?

    提起迪卡侬,想到的便是它汇聚所有运动于同一个屋檐下的盛况.从来没有一家体育用品零售店可以像迪卡侬一样,涵盖几乎所有级别的运动产品.从入门级到最专业的运动产品,应有尽有,不仅产品质量有保证,价格也平易近 ...

  9. hdu 1041 (OO approach, private constructor to prevent instantiation, sprintf) 分类: hdoj 2015-06-17 15:57 25人阅读 评论(0) 收藏

    a problem where OO seems more natural to me, implementing a utility class not instantiable. how to p ...

  10. 关于MVC

    MVC,或多或少都有听说过.这个模式在客户端程序里面比较常见.以前有人老说mvc是什么设计模式之类.至少我理解的不是.我觉得 MVC是一种模块划分方法.根据它,我们可以快速地划分单独某个模块.比如排行 ...