[poj3321]Apple Tree(dfs序+树状数组)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 26762 | Accepted: 7947 |
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
Source
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int ap[],bit[];
int bg[],ed[],cnt=;
int n;
typedef struct{
int to,nxt;
}edge;
edge gra[];
int head[],num=;
int add(int frm,int to){
gra[++num].nxt=head[frm];
gra[num].to=to;
head[frm]=num;
return ;
}
int dfs(int u,int fa){
bg[u]=++cnt;
int j;
for(j=head[u];j;j=gra[j].nxt){
if(gra[j].to!=fa)dfs(gra[j].to,u);
}
ed[u]=cnt;
return ;
}
int lb(int x){
return x&(-x);
}
int c(int x){
int num=ap[x];
x=bg[x];
while(x<=n){
bit[x]+=num;
x+=lb(x);
}
return ;
}
int q(int x){
int a1=,a2=;
int e=ed[x];
while(e){
a1+=bit[e];
e-=lb(e);
}
int b=bg[x]-;
while(b){
a2+=bit[b];
b-=lb(b);
}
return a1-a2;
}
int main(){
scanf("%d",&n);
for(int i=;i<n;i++){
int x,y;
scanf("%d %d",&x,&y);
add(x,y);
add(y,x);
}
dfs(,);
for(int i=;i<=n;i++){
ap[i]=;
c(i);
}
int m;
scanf("%d",&m);
for(int i=;i<=m;i++){
char in[];
int x;
scanf("%s %d",in,&x);
if(in[]=='C'){
ap[x]*=-;
c(x);
}
else printf("%d\n",q(x));
}
return ;
}
睡觉
[poj3321]Apple Tree(dfs序+树状数组)的更多相关文章
- POJ 3321 Apple Tree DFS序 + 树状数组
多次修改一棵树节点的值,或者询问当前这个节点的子树所有节点权值总和. 首先预处理出DFS序L[i]和R[i] 把问题转化为区间查询总和问题.单点修改,区间查询,树状数组即可. 注意修改的时候也要按照d ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- POJ3321Apple Tree Dfs序 树状数组
出自——博客园-zhouzhendong ~去博客园看该题解~ 题目 POJ3321 Apple Tree 题意概括 有一颗01树,以结点1为树根,一开始所有的结点权值都是1,有两种操作: 1.改变其 ...
- [Split The Tree][dfs序+树状数组求区间数的种数]
Split The Tree 时间限制: 1 Sec 内存限制: 128 MB提交: 46 解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 You are given ...
- Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs序+树状数组
D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...
- HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...
- HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...
随机推荐
- eclipse工程加入jquery.min.js报错:missing semicolon
1,注释修改项目目录下的.project文件 <?xml version="1.0" encoding="UTF-8"?> <projectD ...
- PHP获取POST数据的几种方法汇总
一.PHP获取POST数据的几种方法 方法1.最常见的方法是:$_POST['fieldname']; 说明:只能接收Content-Type: application/x-www-form-urle ...
- Java Debug调试简单方法--static使用
Public class Debug { public Debug() { } static void debugPrint(String src) { //System.out.print(src) ...
- do put in ruby
apikey: XO.apikeys.cms, data: { favoriteItems: [{ UserId: SaveToFavoriteVar.content.FavoriteItem.Use ...
- JS中构造函数与函数
//构造函数中,如果返回的是一个对 象,那么就保留原意. 如果返回的是非对象,比如数字.布尔和字符串,那么就返回 this,如果没有 return 语句,那么也返回this. var myFun1 = ...
- 《Linux内核分析》第六周 读书笔记
<Linux内核设计与实现>CHAPTER3阅读梳理 [学习时间:3hours] [学习内容:进程的描述:进程的生命周期(包括创建.终结)] 一.进程(任务)描述 1.进程是处于执行期的程 ...
- c#大文件分割过程
需求: 在项目开发中,我们会遇到单个文件大小超过1TB的文件,这样的文件只能进行单文件读取,往往会造成读取完成耗时过长,导致客户在使用体验过程中不满意. 为了解决提升大文件的解析速度,我想到了先分割大 ...
- java操作MySQL数据库(插入、删除、修改、查询、获取所有行数)
插播一段广告哈:我之前共享了两个自己写的小应用,见这篇博客百度地图开发的两个应用源码共享(Android版),没 想到有人找我来做毕设了,年前交付,时间不是很紧,大概了解了下就接下了,主要用到的就是和 ...
- Latex 学习
Latex 学习 @(05.2 Latex)[latex][基础教程] 这篇文章是关于latex基础教程.pdf的一个总结. 基本知识 基础 1. 空格 多个空格会被视为一个空格 单个回车会被视为一个 ...
- 【java开发系列】—— struts2简单入门示例
前言 最近正好有时间总结一下,过去的知识历程,虽说东西都是入门级的,高手肯定是不屑一顾了,但是对于初次涉猎的小白们,还是可以提供点参考的. struts2其实就是为我们封装了servlet,简化了js ...