题意:

给你一棵树,初始各节点有一个苹果,给出两种操作,x 表示若x节点有苹果拿掉,无苹果就长一个。

x查询以x为根的子树中有多少个苹果。

分析:

开始这个题无从下手,祖先由孩子的标号不能确定,就想能不能重新编号

,对与一棵树我们以先根序进行编号这就保证了一个子树在一个连续的区间内,然后就是BIT了。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define N 100010
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
struct edge{
int v,next;
}e[N*];
int used[N],bit[N],l[N],r[N],id,f[N],n,head[N*];
int len;
void add_edge(int u,int v){
e[len].v=v;
e[len].next=head[u];
head[u]=len++;
}
void add(int x,int d){
while(x<=n){
bit[x]+=d;
x+=x&(-x);
}
}
int sum(int x){
int num=;
while(x>){
num+=bit[x];
x-=x&(-x);
}
return num;
}
void dfs(int i){
l[i]=++id;//根节点编号就是子树左边界
used[i]=;
for(int j=head[i];j!=-;j=e[j].next){
if(!used[e[j].v]){
dfs(e[j].v);
}
}
r[i]=id;//最后一个子孙的编号右边界
}
int main()
{
while(~scanf("%d",&n)){
memset(bit,,sizeof(bit));
memset(f,,sizeof(f)); memset(used,,sizeof(used));
memset(head,-,sizeof(head));
int u,v;
len=;
for(int i=;i<n-;++i){
scanf("%d%d",&u,&v);
add_edge(u,v);
add_edge(v,u);
}
id=;
dfs();
for(int i=;i<=n;++i)
add(i,);
int m,fork;
char op[];
scanf("%d",&m);
while(m--){
scanf("%s%d",op,&fork);
if(op[]=='C'){
if(f[fork]){
add(l[fork],);
f[fork]=;
}
else{
add(l[fork],-);
f[fork]=;
}
}
else if(op[]=='Q'){
printf("%d\n",sum(r[fork])-sum(l[fork]-));
}
}
}
return ;
}

POJ 3321- Apple Tree(标号+BIT)的更多相关文章

  1. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  2. POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)

    id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...

  3. POJ 3321 Apple Tree 【树状数组+建树】

    题目链接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...

  4. POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25904   Accepted: 7682 Descr ...

  5. poj 3321:Apple Tree(树状数组,提高题)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 5629 Descr ...

  6. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

  7. (简单) POJ 3321 Apple Tree,树链剖分+树状数组。

    Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow ...

  8. #5 DIV2 A POJ 3321 Apple Tree 摘苹果 构建线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25232   Accepted: 7503 Descr ...

  9. POJ 3321 Apple Tree(树状数组)

                                                              Apple Tree Time Limit: 2000MS   Memory Lim ...

  10. POJ 3321 Apple Tree dfs+二叉索引树

    题目:http://poj.org/problem?id=3321 动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建 ...

随机推荐

  1. POJ1046Color Me Less

    http://poj.org/problem?id=1046 据说这个题是个水题,但我还是WA了好几次,最后才改对了 #include<cstdio> #include<cstrin ...

  2. Ubuntu环境下手动配置ElasticSearch0.90.5

    1 下载elasticsearch-0.90.5 2 修改配置(可选) 修改内存:(可选) bin/elasticsearch.in.sh中: ES_MIN_MEM ES_MAX_MEM 修改搜索引擎 ...

  3. P1023 奶牛的锻炼

    P1023 奶牛的锻炼 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 USACO 描述 奶牛Bessie有N分钟时间跑步,每分钟她可以跑步或者休息.若她在第 ...

  4. lintcode 中等题:Submatrix sum is 0 和为零的子矩阵

    和为零的子矩阵 给定一个整数矩阵,请找出一个子矩阵,使得其数字之和等于0.输出答案时,请返回左上数字和右下数字的坐标. 样例 给定矩阵 [ [1 ,5 ,7], [3 ,7 ,-8], [4 ,-8 ...

  5. WCF 简单示例

    WCF(Windows Communication Foundation,WCF)是基于Windows平台下开发和部署服务的软件开发包(Software Development Kit,SDK).WC ...

  6. UVa 11524 - InCircle

    推公式 #include <cstdio> #include <cmath> double Cal( double a, double b, double c ) { retu ...

  7. python 字符串换行的三种方式

    if __name__ == '__main__': #第一种: 三个单引号 print ''' aaaaaaaaaaaaaaaa         bbbbbbbbbbbbbb''' #第二种: 三个 ...

  8. 面试题_66_to_75_Java IO 和 NIO 的面试题

    IO 是 Java 面试中一个非常重要的点.你应该很好掌握 Java IO,NIO,NIO2 以及与操作系统,磁盘 IO 相关的基础知识.下面是 Java IO 中经常问的问题. 66)在我 Java ...

  9. poj 3790 Recursively Palindromic Partitions (递推)

    题目 题意:求输入的数字的递归回文. 思路:答案等于这个数字一半之前的所有的 之和. #include <iostream> #include <cstdio> #includ ...

  10. Android之界面刷新(invalidate和postInvalidate使用)

    Android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用. Android提供了Inva ...