题目链接

描述

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?

输入

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

输出

For every inquiry, output the correspond answer per line.

样例输入

3

1 2

1 3

3

Q 1

C 2

Q 1

样例输出

3

2

分析:

有一棵苹果树,刚开始的时候每个节点上都有且仅有一颗苹果,需要进行一系列操作,Q N表示的是要查询N节点下有多少个苹果(包括N节点本身和它下面所有的子节点),C N表示的是如果N节点上有苹果的话,就把这个苹果拿走,否则就往这个节点上放一个苹果。

在查询的时候,要求出这个节点下的所有的苹果树,相当于求的是一个区间和,如果给这棵树的每个节点都给合理的编号,那么就可以按照树状数组来求区间和了。

Step 1:

如下图,可以看到,由于普通的树并不具有区间的性质,所以在考虑使用线段树作为解题思路时,需要对给给定的数据进行转化,首先对这棵树进行一次dfs遍历,记录dfs序下每个点访问起始时间与结束时间,记录起始时间是前序遍历,结束时间是后序遍历,同时对这课树进行重标号。

Step 2:

     如下图,DFS之后,那么树的每个节点就具有了区间的性质。

     那么此时,每个节点对应了一个区间,而且可以看到,每个节点对应的区间正好“管辖”了它子树所有节点的区间,那么对点或子树的操作就转化为了对区间的操作。

Step 3:

     这个时候,每次对节点进行更新或者查询,就是线段树和树状数组最基本的实现了…

代码:

include<stdio.h>

include

include

using namespace std;

vectorv[100005];

int le[100005];

int ri[100005];

int bj[100005];

int e[100005];

int Apple[100005];

int cnt,n;



void dfs(int id)///相当于给树中的每一个节点找到合适的区间

{

int op;

le[id]=++cnt;

bj[id]=1;

for(int i=0;i<v[id].size();i++)

{

op=v[id][i];

if(bj[op]0)

{

dfs(op);

}

}

ri[id]=cnt;

}



int lowBit(int n)

{

return n&(-n);

}



int sum(int n)///求和

{

int num=0;

while(n>0)

{

num+=e[n];

n=n-lowBit(n);

}

return num;

}



void Updata(int num,int a)///更新

{

while(num<=n)

{

e[num]+=a;

num+=lowBit(num);

}

}

int main()

{

int a,b,m;

char ch;

scanf("%d",&n);

for(int i=1;i<n;i++)

{

scanf("%d%d",&a,&b);

v[a].push_back(b);

v[b].push_back(a);

}

cnt=0;

dfs(1);

for(int i=1;i<=n;i++)

{

Apple[i]=1;///刚开始的时候每个节点都有一个苹果

Updata(i,1);///初始化的时候树中的每个节点都要更新

}

scanf("%d",&m);

while(m--)

{

scanf(" %c%d",&ch,&a);

if(ch'Q')

{

printf("%d\n",sum(ri[a])-sum(le[a]-1));

}

if(ch'C')

{

if(Apple[a]1)

{

Apple[a]=0;

Updata(le[a],-1);///节点上的苹果去掉,涉及它的每个区间上都要减掉一个

}

else

{

Apple[a]=1;

Updata(le[a],1);///节点上添加一个新的苹果,涉及它的每个区间上也要加上一个苹果

}

}

}

return 0;

}

NYOJ 231 Apple Tree (树状数组)的更多相关文章

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

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

  2. POJ--3321 Apple Tree(树状数组+dfs(序列))

    Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Descripti ...

  3. POJ 3321:Apple Tree 树状数组

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22131   Accepted: 6715 Descr ...

  4. 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. ...

  5. POJ 3321 Apple Tree 树状数组+DFS

    题意:一棵苹果树有n个结点,编号从1到n,根结点永远是1.该树有n-1条树枝,每条树枝连接两个结点.已知苹果只会结在树的结点处,而且每个结点最多只能结1个苹果.初始时每个结点处都有1个苹果.树的主人接 ...

  6. POJ3321 Apple Tree(树状数组)

    先做一次dfs求得每个节点为根的子树在树状数组中编号的起始值和结束值,再树状数组做区间查询 与单点更新. #include<cstdio> #include<iostream> ...

  7. POJ 3321 Apple Tree (树状数组+dfs序)

    题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...

  8. POJ 3321 Apple Tree 树状数组 第一题

    第一次做树状数组,这个东西还是蛮神奇的,通过一个简单的C数组就可以表示出整个序列的值,并且可以用logN的复杂度进行改值与求和. 这道题目我根本不知道怎么和树状数组扯上的关系,刚开始我想直接按图来遍历 ...

  9. 3321 Apple Tree 树状数组

    LIANJIE:http://poj.org/problem?id=3321 给你一个多叉树,每个叉和叶子节点有一颗苹果.然后给你两个操作,一个是给你C清除某节点上的苹果或者添加(此节点上有苹果则清除 ...

随机推荐

  1. Matlab中TCP通讯-实现外部程序提供优化目标函数解

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Matlab中TCP通讯-实现外部程序提供优化目标函数解     本文地址:http://te ...

  2. mysql 慢查询,查询缓存,索引,备份,水平分割

    1.开启慢查询 在mysql的配置文件my.ini最后增加如下命令 [mysqld]port=3306slow_query_log =1long_query_time = 1 2.查看慢查询记录 默认 ...

  3. 图解用HTML5的popstate如何玩转浏览器历史记录

    一.popstate用来做什么的?简而言之就是HTML5新增的用来控制浏览器历史记录的api. 二.过去如何操纵浏览器历史记录? window.history对象,该对象上包含有length和stat ...

  4. .net MVC中使用angularJs刷新页面数据列表

    使用angularjs的双向绑定功能,定时刷新页面上数据列表(不是刷新网页,通过ajax请求只刷新数据列表部分页面),实例如下: @{ Layout = null; } <!DOCTYPE ht ...

  5. MVC SignalR Hub初学

    具体有关SignalR的说明查网上,我这里简单列举一个例子 1.新建MVC,添加SignalR引用(NuGet安装). 2.添加OWIN START类 public class Startup { p ...

  6. POJ2374_Fence Obstacle Course

    题意是描述是这样的,给你n个围栏,对于每个围栏你必须走到其边上才可以往下跳,现在问你从初始最高位置的n个围栏,到原点,水平走过的路程最少是多少? 其实我可可以这样来考虑问题.由于每次都是从板子的左右两 ...

  7. P1065 作业调度方案

    题目描述 我们现在要利用m台机器加工n个工件,每个工件都有m道工序,每道工序都在不同的指定的机器上完成.每个工件的每道工序都有指定的加工时间. 每个工件的每个工序称为一个操作,我们用记号j−k表示一个 ...

  8. 51nod 1292 字符串中的最大值V2(后缀自动机)

    题意: 有一个字符串T.字符串S的F函数值可以如下计算:F(S) = L * S在T中出现的次数(L为字符串S的长度).求所有T的子串S中,函数F(S)的最大值. 题解: 求T的后缀自动机,然后所有每 ...

  9. [cogs1065]绿豆蛙的归宿

    1065. [Nescafe19] 绿豆蛙的归宿 [题目描述] 给出一个有向无环的连通图,起点为1终点为N,每条边都有一个长度.绿豆蛙从起点出发,走向终点.到达每一个顶点时,如果有K条离开该点的道路, ...

  10. Ubuntu 10.04 配置TQ2440交叉编译环境

    一.解压交叉编译开发工具包  EABI_4.3.3_EmbedSky_20100610.tar.bz2 $ sudo mkdir /opt/EmbedSky/     $ sudo cp -r /ho ...