Apple Tree

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

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
"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
"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
/*
题意:给你一棵二叉树,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的更多相关文章

  1. POJ 3321- Apple Tree(标号+BIT)

    题意: 给你一棵树,初始各节点有一个苹果,给出两种操作,C x 表示若x节点有苹果拿掉,无苹果就长一个. Q x查询以x为根的子树中有多少个苹果. 分析: 开始这个题无从下手,祖先由孩子的标号不能确定 ...

  2. poj 3237 Tree [LCA] (树链剖分)

    poj 3237 tree inline : 1. inline 定义的类的内联函数,函数的代码被放入符号表中,在使用时直接进行替换,(像宏一样展开),没有了调用的开销,效率也很高. 2. 很明显,类 ...

  3. poj 3237 Tree(树链拆分)

    题目链接:poj 3237 Tree 题目大意:给定一棵树,三种操作: CHANGE i v:将i节点权值变为v NEGATE a b:将ab路径上全部节点的权值变为相反数 QUERY a b:查询a ...

  4. POJ 1741 Tree 求树上路径小于k的点对个数)

                                                                                                 POJ 174 ...

  5. POJ 2378 Tree Cutting 3140 Contestants Division (简单树形dp)

    POJ 2378 Tree Cutting:题意 求删除哪些单点后产生的森林中的每一棵树的大小都小于等于原树大小的一半 #include<cstdio> #include<cstri ...

  6. poj 1741 Tree(树的点分治)

    poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...

  7. POJ 3723 Tree(树链剖分)

    POJ 3237 Tree 题目链接 就多一个取负操作,所以线段树结点就把最大和最小值存下来,每次取负的时候,最大和最小值取负后.交换就可以 代码: #include <cstdio> # ...

  8. POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量

    POJ 1741. Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 34141   Accepted: 11420 ...

  9. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

随机推荐

  1. Angular JS 基础应用--第一篇

      前  言          Android应用开发中,有一些功能虽然能够使用原生JS来实现,但是会比较的复杂,因此一些相应的框架应运而生了.框架相对于原生JS而言,从主观上来说,最大的改变就是代码 ...

  2. Matlab入门学习(程序设计)

    一.循环(for,while) for循环: for i=begin:step:end ...... end while循环: while condition ...... end 二.分枝(if,i ...

  3. angular directive知识

    一般来讲 directive名字遵循一下规则: 1.忽略以x-和data-为元素/属性的前缀 2.转化“:”,“-”,“_”命名为驼峰命名 如下所示 <div ng-controller=&qu ...

  4. snmp4j 之 ArgumentParser

    ArgumentParser ArgumentParser命令行解析器 将数组转换成Java对象 根据预定计划选项和参数,以及相应的命令行选项联合每个对象 ArgumentParser argumen ...

  5. Codeforces 845 C. Two TVs 思路:简单贪心算法

    题目: 题目原文链接:http://codeforces.com/contest/845/problem/C 题意:现在我们有一个电视清单,有两个电视,电视清单上有每一个节目的开始时间和结束时间. 电 ...

  6. Power Sum 竟然用原根来求

    Power Sum Time Limit: 20000/10000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitS ...

  7. Count Color 线段树

    Count Color Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit  ...

  8. 推荐系统相关算法(1):SVD

    假如要预测Zero君对一部电影M的评分,而手上只有Zero君对若干部电影的评分和风炎君对若干部电影的评分(包含M的评分).那么能预测出Zero君对M的评分吗?答案显然是能.最简单的方法就是直接将预测分 ...

  9. 重学C语言---01概述

    1.什么是C语言 C语言是一种计算机程序设计语言,它既具有高级语言的特点,又具有汇编语言的特点.计算机语言是从第二次世界大战以后,经历了戏剧性的发展过程.从机器语言到汇编语言和高级语言.C语言是与硬件 ...

  10. centos7下安装tesseract-ocr进行验证码识别

    摘要: centos7安装依赖库 tesseract配置 代码例子 centos7安装依赖库 安装centos系统依赖 yum install -y automake autoconf libtool ...