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 ...
随机推荐
- ArrayList,LinkedListd等容器使用时注意点:
1.对这两个List(包括其他的类似容器),如果向里面加入一个元素(引用数据类型),那么这个List里面保存的是这个对象的引用: 如果想要避免这种现象可以这样:在加入新的元素时不直接压,将已有的对象复 ...
- 使用 TUN 设备实现一个简单的 UDP 代理隧道
若要实现在 Linux 下的代理程序,方法有很多,比如看着 RFC 1928 来实现一个 socks5 代理并自行设置程序经过 socks5 代理等方式,下文是使用 Linux 提供的 tun/tap ...
- spring jar包
org.springframework.aop- 3.0.0.RELEASE--------------------Spring的面向切面编程,提供AOP(面向切面编程)实现 org.springfr ...
- Nexus 私有仓库搭建与 Maven 集成
Nexus 私有仓库搭建与 Maven 集成 |作者:RexFang |出处:http://www.cnblogs.com/rexfang/ |关于作者:Java 程序员一枚 |版权:本文版权归作者和 ...
- Training little cats poj3735
Training little cats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9299 Accepted: 2 ...
- SUM游戏
题意:就是有一个长度为n的整数序列,两个游戏者A和B轮流取数,A先取.每次玩家只能从左端或者右端取任意数量个数,但不能两端都取. 所有数都被取走后游戏结束,然后统计每个人取走的所有数之和,作为各自的得 ...
- S2_SQL_第一章
第一章:数据库的设计 1.1:为什么需要规范数据库的设计 1.1.1:什么是数据库设计 数据库设计就是将数据中的数据实体及这些数据实体之间的关系,进行规范和结构的过程. 1.1.2:数据库设计非常重要 ...
- NDK各个版本链接
ndk_r15c (July 2017) Windows 32-bit : https://dl.google.com/android/repository/android-ndk-r15c-wind ...
- vue2购物车ch3-(过滤器使用 单件商品金额计算 全选全不选 总金额计算 删除商品功能)
1 index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- ubuntu系统如何屏幕截图
我们知道,windows下有很多截图的软件和插件,那么在ubuntu系统下我们该怎样截图呢? 下面就让小编来告诉你几种简单的方法吧. 工具/原料 ubuntu系统电脑 方法一: 1.也许很多朋友都知道 ...