POJ--3321 Apple Tree(树状数组+dfs(序列))
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 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
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
题意: 就是给你一棵树,每一个结点一开始都有一个苹果,然后有两个操作,一个是改变一个结点的值。即有苹果变无苹果,无苹果变有苹果。另一个是求一个结点下面的子树有多少个苹果。这个题目,询问次数100000,显然要用树状数组。属于更新点,求区间和。要知道树状数组无非两种类型,更新点求区间,更新区间求点。此外还要会dfs序列。具体见代码里dfs()函数。dfs序列可以自己百度一下是什么。这道题目是最基础的在树的结构里用树状数组统计
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;
#define MAX 100010
int head[MAX];
int c[MAX];
int apple[MAX];
int tol;
int n,m;
int tag;
int _right[MAX];
int _left[MAX];
int ans;
int vis[MAX];
struct Node
{
int value;
int next;
}edge[MAX*2];
void modify(int x,int y)
{
edge[tol].value=y;
edge[tol].next=head[x];
head[x]=tol++;
}
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int num)
{
while(x<=n)
{
c[x]+=num;
x+=lowbit(x);
}
}
int sum(int x)
{
int _sum=0;
while(x>0)
{
_sum+=c[x];
x-=lowbit(x);
}
return _sum;
}
void dfs(int x)
{
vis[x]=1;
tag++;
_left[x]=tag;
int a=head[x];
while(a!=-1)
{
if(vis[edge[a].value]==0)
dfs(edge[a].value);
a=edge[a].next;
}
_right[x]=tag;
}
void init()
{
memset(c,0,sizeof(c));
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
}
int main()
{
int x,y;
char a;
int b;
scanf("%d",&n);
tol=0;
tag=0;
init();
for(int i=1;i<=n;i++)
{
update(i,1);
apple[i]=1;
}
for(int i=0;i<n-1;i++)
{
scanf("%d%d",&x,&y);
modify(x,y);
modify(y,x);
}
dfs(1);
scanf("%d",&m);
for(int i=0;i<m;i++)
{
getchar();
scanf("%c",&a);
scanf("%d",&b);
if(a=='C')
{
if(apple[b]==1)
{
update(_left[b],-1);
apple[b]=0;
}
else
{
update(_left[b],1);
apple[b]=1;
}
}
else
{
ans=sum(_right[b])-sum(_left[b]-1);
printf("%dn",ans);
}
}
return 0;
}
- Unity Container中的几种注册方式与示例1.实例注册2.简单类型注册
- 使用Unity创建依赖注入依赖注入生命周期:注册、解析、销毁 注册解析销毁
- 使用WCF进行跨平台开发之三(JAVA调用WCF服务)1.开发必备2.生成WCF客户端3.开发程序4.结束语
- 使用WCF进行跨平台开发之二(IIS托管WCF服务并使用php平台调用)1.系统必备2.在IIS中托管WCF服务3.使用PHP调用托管在IIS中的WCF服务
- 使用WCF进行跨平台开发之一(WCF的实现、控制台托管与.net平台的调用)1.创建项目结构2.契约的设计3.实现服务4.控制台托管服务5.在.net平台中调用WCF
- 使用GUI工具高效构建你自己的Nuget包丰富包的基础信息添加要包含的文件The end
- 网页结构与表现原则
- CSS3动画功能
- Angular开发者手册重点翻译之指令(一)文本和属性绑定ngAttr属性绑定
- CSS3中的变形处理
- WCF中操作的分界于调用顺序和会话的释放操作分界实例停止
- WCF中数据契约之已知类型的几种公开方式代码中定义配置中定义宿主端使用解析器
- 有趣的算法(一)——n阶层尾部有几个0
- Singal Page App:使用Knockout和RequireJS创建高度模块化的单页应用引擎背景知识文档结构服务端API准备Require配置与系统配置模块中的工作模块间的工作烂图赏鉴代码送上
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Dart常用的数据类型
- Vue版本更新清除浏览器缓存
- maven:Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenC
- maven整合struts: Compilation error org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException
- Dart判断运算符
- 机器学习之朴素贝叶斯算法原理与代码实现
- jquery中ajax的使用(java)
- mybatis框架org.apache.ibatis.exceptions.PersistenceException:
- Spring源码学习笔记(3)——容器的功能扩展
- Dart数组的常规操作
- 机器学习之logistic回归算法与代码实现原理
- spring整合中application.xml配置
- RocketMQ详解(10)——Consumer详解
- RocketMQ详解(12)——RocketMQ的重试机制
- RocketMQ详解(13)——RocketMQ的消息模式