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

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
"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
"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

Source

POJ Monthly--2007.08.05, Huang, Jinsong

  树状数组,提高题

  有些难度,要转一下弯。

  题意

  一开始给你n-1个边,构成一颗苹果树,默认这个时候每个分叉上都有苹果。接下来有q个操作,这些操作分两种,要不有苹果摘走苹果,没苹果长出苹果,要不求某一分叉上所有的苹果个数。

  思路

  关键是在每一个分叉处记录一个开始时间,和结束时间,表示dfs时经过这个分叉的时间和回到这个分叉的时间。这里的时间其实是dfs遍历次序的定位。这样就相当于有了一个区间,用树状数组就可以求出这个分叉点上的所有苹果的个数。

  注意

  给你的n-1条边,是双向的,即a指向b,b也指向a,是无向图。

  测试数据(来自poj讨论版)


Q
C
Q
Q
C
Q
C
Q
C
Q
ans:

  代码

 #include <iostream>
#include <stdio.h>
using namespace std; #define MAXN 100010 int N;
int cnt=;
int c[MAXN];
int start[MAXN];
int end[MAXN]; struct Node{
int num;
Node* next; //孩子节点
Node()
{
next = NULL;
}
}tree[MAXN]; //临界表 int lowbit(int x)
{
return x & (-x);
} void add(int d,int x)
{
while(d<=N){
c[d] += x;
d += lowbit(d);
}
} int sum(int d)
{
int ans =;
while(d>=){
ans += c[d];
d -= lowbit(d);
}
return ans;
} void dfs(int v) //以r为根节点进行dfs遍历,返整个遍历之后的时间
{
start[v] = ++cnt;
Node* p = tree[v].next;
while(p){
if(start[p->num]==)
dfs(p->num);
p = p->next;
}
end[v] = cnt;
} void addedge(int a,int b) //在苹果树上加分支
{
Node* p = new Node;
p->num = b;
p->next = tree[a].next;
tree[a].next = p;
} int main()
{
int i,q;
scanf("%d",&N);
for(i=;i<N;i++){
int a,b;
scanf("%d%d",&a,&b);
addedge(a,b);
addedge(b,a);
}
dfs(); for(i=;i<=N;i++) //初始化c[]
add(i,); scanf("%d",&q);
while(q--){ //q次操作
char cmd[];
int d;
scanf("%s%d",cmd,&d);
if(cmd[]=='C'){
if(sum(start[d])-sum(start[d]-)==)
add(start[d],-);
else
add(start[d],);
}
else if(cmd[]=='Q'){
printf("%d\n",sum(end[d])-sum(start[d]-));
}
}
return ;
}

Freecode : www.cnblogs.com/yym2013

poj 3321:Apple Tree(树状数组,提高题)的更多相关文章

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

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

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

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

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

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

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

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

  5. 3321 Apple Tree 树状数组

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

  6. POJ 3321:Apple Tree 树状数组

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

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

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

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

  9. POJ3321 Apple Tree(树状数组)

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

  10. POJ 3928 Ping pong 树状数组模板题

    開始用瓜神说的方法撸了一发线段树.早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring> ...

随机推荐

  1. Qt5 程序启动画面动图效果

    2333终于实现动图,先弄了一个窗口去掉标题栏假装就是启动画面了,还是那只萌萌的猫这次会动了! 基类用的是QWidget  类名称MainView #ifndef MAINVIEW_H #define ...

  2. 51Nod 1378 夹克老爷的愤怒

    Description 一棵树,可以进行染色,被染色的点可以控制与它距离不超过 \(k\) 的所有点,问控制整棵树最少需要染几个点. Sol 贪心. 记录一下最深的未染色点和最浅的染色点,判断一下能否 ...

  3. BZOJ 1068: [SCOI2007]压缩

    Sol 区间DP.这个区间DP需要三维, \(f[i][j][k]\) 表示\([i,j]\) 这个区间中是否存在 \(M\) . 转移有两种,一种是这个区间存在 \(M\) ,那么直接枚举 \(M\ ...

  4. 可复用View的PagerAdapter

    用PagerAdapter的时候会反复调用instantiateItem和destroyItem来创建和销毁View,没有复用性.这里封装了一个可复用View的PagerAdapter,给实现类留下的 ...

  5. centos7 python3.5 下安装paramiko

    centos7 python3.5 下安装paramiko 安装开发包 yum install openssl openssl-devel python-dev -y 安装pip前需要前置安装setu ...

  6. java去除字符串中的空格、回车、换行符、制表符

    import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author chzeze * 2016-11-07 */ ...

  7. 2016-03-12 Leanning Plan

    1,Python 2,Java 3,Html+Css 4,PHP 5,Crawl 6,WetChat Platform

  8. peewee 字段属性help_text的支持问题

    至少在__version__ = '2.6.0'的时候,给字段添加help_text的时候,在数据库的ddl语句里面是没有comment的. 看了下源码,顺藤摸瓜,最终定位到了字段(Field类)的_ ...

  9. css3 transition effect(其它效果)

    http://blog.csdn.net/jerryvon/article/details/8755548 整理了一些其它动画,用的模板为flip模板,只不过CSS3不同 /************* ...

  10. How do I get ASP.NET Web API to return JSON instead of XML using Chrome

    public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Ro ...