poj 3321Apple Tree
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
Sample Input
3
1 2
1 3
3
Q 1
C 2
Q 1
Sample Output
3
2
/*
题意:给你一棵二叉树,root节点是1,初始的时候每个节点都有一个苹果,两种操作:
C:x,对x节点的苹果数量进行异或
Q:x,查询以x节点为根节点的子树的苹果的数量 初步思路:实际上就是建图,然后dfs跑一遍,记录下每个节点的左右区间,注意标记的时候将一个节点表示的区间内的点表示成连续的点,
然后记录一下区间内点的区间长度,然后用树状数组进行区间求和单点更新 #超时:可能是初始化的时候超时
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
const int N=;
int n,m;
char str[];
int Count=;//用于表示重新标记的节点
int Start[N];//用于标记节点区间的开始位置
int End[N];//用于标记节点区间的结束位置
int lowbit[N];
int val[N];
int u,v;
int c[N];
typedef vector<int> VCT_INT;
vector<VCT_INT>edge(N/);//用于构建图(树)
void dfs(int x){
Start[x]=++Count;
for(int i=;i<edge[x].size();i++){
dfs(edge[x][i]);
}
End[x]=++Count;
}
void add(int x,int val){
while(x<=Count){
c[x]+=val;
x+=lowbit[x];
}
}
int sum(int x){
int res=;
while(x>){
res+=c[x];
x-=lowbit[x];
}
return res;
}
int main(){
// freopen("in.txt","r",stdin);
scanf("%d",&n);
for(int i=;i<n-;i++){
scanf("%d%d",&u,&v);
edge[u].push_back(v);//建图
}
Count=;
dfs();//从加点1开始搜索标记 for(int i=;i<=n;i++){
val[i]=;
}
//如果用add(i,1)的话会超时的,只能求出来每个的值,直接初始化
// c[i]=i-(i-(lowbit(i)) );
// 就是从1到i的苹果数减去距离i点最近的左侧的树上苹果的数量
for(int i=;i<=Count;i++){
lowbit[i]=i&(i^(i-));
}
for(int i=;i<=Count;i++){
c[i]=i-(i-lowbit[i]);
}//初始状态下每个节点都有一个苹果
scanf("%d",&m);
for(int i=;i<m;i++){
scanf("%s%d",str,&u);
if(str[]=='C'){
if(val[u]){
add(Start[u],-);
add(End[u],-);
val[u]=;
}else{
add(Start[u],);
add(End[u],);
val[u]=;
}
}else{
int x1=sum(End[u]);
int x2=sum(Start[u]);
printf("%d\n",(x1-x2)/+val[u]);
}
}
return ;
}
poj 3321Apple Tree的更多相关文章
- POJ 3321- Apple Tree(标号+BIT)
题意: 给你一棵树,初始各节点有一个苹果,给出两种操作,C x 表示若x节点有苹果拿掉,无苹果就长一个. Q x查询以x为根的子树中有多少个苹果. 分析: 开始这个题无从下手,祖先由孩子的标号不能确定 ...
- poj 3237 Tree [LCA] (树链剖分)
poj 3237 tree inline : 1. inline 定义的类的内联函数,函数的代码被放入符号表中,在使用时直接进行替换,(像宏一样展开),没有了调用的开销,效率也很高. 2. 很明显,类 ...
- poj 3237 Tree(树链拆分)
题目链接:poj 3237 Tree 题目大意:给定一棵树,三种操作: CHANGE i v:将i节点权值变为v NEGATE a b:将ab路径上全部节点的权值变为相反数 QUERY a b:查询a ...
- POJ 1741 Tree 求树上路径小于k的点对个数)
POJ 174 ...
- POJ 2378 Tree Cutting 3140 Contestants Division (简单树形dp)
POJ 2378 Tree Cutting:题意 求删除哪些单点后产生的森林中的每一棵树的大小都小于等于原树大小的一半 #include<cstdio> #include<cstri ...
- poj 1741 Tree(树的点分治)
poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...
- POJ 3723 Tree(树链剖分)
POJ 3237 Tree 题目链接 就多一个取负操作,所以线段树结点就把最大和最小值存下来,每次取负的时候,最大和最小值取负后.交换就可以 代码: #include <cstdio> # ...
- POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量
POJ 1741. Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 34141 Accepted: 11420 ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
随机推荐
- Java9新特性之——JShell
java9已经在北京时间9月22日正式发布,开发者可以在oracle jdk官网上下载到最新的jdk9.jdk9和jdk8中的新特性不同:jdk8中的stream和lambda表达式能够让开发者非常快 ...
- AngularJS概念概述和第一个使用例子
点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ 概念概述 本节使用一个简单的例子简要介绍了AngularJS的重要组成部分. 概念 描述 ...
- snsapi_base和snsapi_userinfo
1.以snsapi_base为scope发起的网页授权,是用来获取进入页面的用户的openid的,并且是静默授权并自动跳转到回调页的.用户感知的就是直接进入了回调页(往往是业务页面) 2.以snsap ...
- iOS Storyboard约束详解
链接:http://www.jianshu.com/p/b88c65ffc3eb 约束,就是指--此处略去1万字--都懂的,就不说了.直接进入实战环节. 本文的控件约束都是围绕着UITableView ...
- iOS连续dismiss几个ViewController的方法
原文链接:http://blog.csdn.net/longshihua/article/details/51282388 presentViewController是经常会用到的展现ViewCont ...
- java集合系列——Map之TreeMap介绍(九)
一.TreeMap的简介 TreeMap是一个有序的key-value集合,基于红黑树(Red-Black tree)的 NavigableMap实现.该映射根据其键的自然顺序进行排序,或者根据创建映 ...
- Intellij idea史上最简单的教程之Linux下安装与破解Intellij idea2017
一.前言 这一节我们介绍在Linux下如何安装与破解Intellij idea2017.现在有很多公司开发环境都是Linux,所以掌握在Linux环境下使用Idea办公也是咱们必须得掌握的技能. 记住 ...
- pgpool-II主备流复制的架设
1.环境 OS: CentOS release 6.4 (Final) DB: postgresql 9.3.6 pgpool服务器: pgpool 172.16.0.240 数据库主服务器:mast ...
- SVN初体验
呐,部门领导要求今后项目部分内容要实行版本控制,因此有机会深入接触下SVN这门功课 ---------------------------------------------------------- ...
- 简单Elixir游戏服设计- 创建项目
反正是写到哪算哪. 创建umbrella项目 mix new simple_game --umbrella 创建model项目 cd simple_game\apps mix new model 创建 ...