POJ 3321 Apple Tree(树状数组)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 27470 | Accepted: 8140 |
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
【分析】题意很简单,这里就不多说了。做法是先dfs编号,找出每个节点所包含的节点编号区间,然后就是树状数组的事了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 2e9
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = 2e5+;
const int M = 4e5+;
int n,m,tot=,cnt=;
int head[N],x[N],y[N],r[N];
int tree[N];
struct EDG{
int to,next;
}edg[M];
inline void addedg(int u,int v){
edg[tot].to=v;edg[tot].next=head[u];head[u]=tot++;
}
void add(int k,int num){
while(k<=n){
tree[k]+=num;
//printf("####%lld\n",tree[k]);
k+=k&(-k);
}
}
int Sum(int k){
int sum=;
while(k>){
sum+=tree[k];
k-=k&(-k);
}
return sum;
}
void dfs(int u){
int v;
x[u]=++cnt;
for(int i=head[u];i!=-;i=edg[i].next){
v=edg[i].to;
if(!x[v])dfs(v);
}
y[u]=cnt;
return;
}
int main() {
met(tree,);met(head,-);met(x,);
int apple[N];
for(int i=;i<N;i++)apple[i]=;
int u,v;
char str[];
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
addedg(u,v);addedg(v,u);
}
dfs();
for(int i=;i<=n;i++)add(i,);
scanf("%d",&m);
while(m--){
scanf("%s",str);
scanf("%d",&u);
if(str[]=='C'){
int l=x[u],rr=y[u];
if(apple[l]){
apple[l]=;
add(l,-);
}else {
apple[l]=;
add(l,);
}
}else {
int ans=Sum(y[u])-Sum(x[u]-);
printf("%d\n",ans);
}
}
return ;
}
POJ 3321 Apple Tree(树状数组)的更多相关文章
- POJ 3321 Apple Tree (树状数组+dfs序)
题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...
- POJ 3321 Apple Tree 树状数组+DFS
题意:一棵苹果树有n个结点,编号从1到n,根结点永远是1.该树有n-1条树枝,每条树枝连接两个结点.已知苹果只会结在树的结点处,而且每个结点最多只能结1个苹果.初始时每个结点处都有1个苹果.树的主人接 ...
- POJ 3321 Apple Tree 树状数组 第一题
第一次做树状数组,这个东西还是蛮神奇的,通过一个简单的C数组就可以表示出整个序列的值,并且可以用logN的复杂度进行改值与求和. 这道题目我根本不知道怎么和树状数组扯上的关系,刚开始我想直接按图来遍历 ...
- 3321 Apple Tree 树状数组
LIANJIE:http://poj.org/problem?id=3321 给你一个多叉树,每个叉和叶子节点有一颗苹果.然后给你两个操作,一个是给你C清除某节点上的苹果或者添加(此节点上有苹果则清除 ...
- POJ 3321:Apple Tree 树状数组
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22131 Accepted: 6715 Descr ...
- POJ--3321 Apple Tree(树状数组+dfs(序列))
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Descripti ...
- 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. ...
- POJ3321 Apple Tree(树状数组)
先做一次dfs求得每个节点为根的子树在树状数组中编号的起始值和结束值,再树状数组做区间查询 与单点更新. #include<cstdio> #include<iostream> ...
- POJ 2486 Apple Tree [树状DP]
题目:一棵树,每个结点上都有一些苹果,且相邻两个结点间的距离为1.一个人从根节点(编号为1)开始走,一共可以走k步,问最多可以吃多少苹果. 思路:这里给出数组的定义: dp[0][x][j] 为从结点 ...
随机推荐
- 深入浅出设计模式——中介者模式(Mediator Pattern)
模式动机 在用户与用户直接聊天的设计方案中,用户对象之间存在很强的关联性,将导致系统出现如下问题: 系统结构复杂:对象之间存在大量的相互关联和调用,若有一个对象发生变化,则需要跟踪和该对象关联的其他 ...
- Hello World for U
题目描述: Given any ) characters, you are asked to form the characters into the shape of U. For example, ...
- Scrum Meeting 1-20151201
任务安排 姓名 今日任务 明日任务 困难 董元财 学习下拉刷新的实现 完成下拉刷新的实现 手机的点击动作长按和下拉有类似的地方,比较难解决 胡亚坤 学习圆形头像代码设计 完成圆形头像代码设计 无 刘猛 ...
- java高薪之路__005_IO流
参考地址: 1. http://blog.csdn.net/yczz/article/details/38761237 File类 ObjectInputStream && Objec ...
- spring+mybatis
---恢复内容开始--- 使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地 ...
- get a new level 25 battle pet in about an hour
If you have 2 level 25 pets and any level 1 pet, obviously start with him in your lineup. Defeat all ...
- oc程序代码
// // main.m // aa // // Created by rcfwzx on 15/11/20. // Copyright (c) 2015年 rcfwzx. All right ...
- awakeFromNib与viewDidLoad的区别
当一个nib文件对应两个类,File's Owner的class为XXXViewController,Objects下的View对应的为XXXView时: awakeFromNib:在XXXView. ...
- CentOS Mysql 5.1.73 主从配置
---------------------------------------------- 1 修改my.cnf ------------------------------------------ ...
- 转发自AstralWind的博客(python正则表达式)
原文地址:http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html 1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python ...