POJ3321Apple Tree
Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 39566 Accepted: 11727
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
Source
POJ Monthly--2007.08.05, Huang, Jinsong
中文题意
n个节点的苹果树,初始每个节点都有一个苹果。m个询问和修改,询问某棵子树的苹果树,修改某个节点,若该节点无苹果则长出苹果,有苹果则摘掉
题解
dfs序,记录进入时间和推出时间in[x],out[x],时间戳虽进入点数而增加,推出时不增加
以时间戳为下标,用树状数组维护单点修改,区间查询
查询in[x]到out[x]之间
修改in[x]
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <iostream>
void swap(int &a, int &b){int tmp = a;a = b, b = tmp;}
int max(int a, int b){return a > b ? a : b;}
int min(int a, int b){return a < b ? a : b;}
int lowbit(int x){return x & (-x);}
void read(int &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
}
const int INF = 0x3f3f3f3f;
const int MAXN = 100000 + 10;
struct Edge
{
int u, v, nxt;
Edge(int _u, int _v, int _nxt){u = _u, v = _v, nxt = _nxt;}
Edge(){}
}edge[MAXN << 1];
int head[MAXN], cnt;
void insert(int a, int b)
{
edge[++ cnt] = Edge(a, b, head[a]), head[a] = cnt;
edge[++ cnt] = Edge(b, a, head[b]), head[b] = cnt;
}
int in[MAXN], out[MAXN], rank[MAXN], num[MAXN], t;
int n, m;
void modify(int x, int y)
{
for(;x <= n;x += lowbit(x))
num[x] += y;
}
int ask(int x)
{
int sum = 0;
for(;x;x -= lowbit(x))
sum += num[x];
return sum;
}
void dfs(int x)
{
in[x] = ++ t;
rank[t] = x;
for(int pos = head[x];pos;pos = edge[pos].nxt)
{
int v = edge[pos].v;
if(in[v]) continue;
dfs(v);
}
out[x] = t;
}
int main()
{
freopen("data.txt", "r", stdin);
read(n);
for(int i = 1;i < n;++ i)
{
int tmp1, tmp2;
read(tmp1), read(tmp2);
insert(tmp1, tmp2);
}
read(m);
dfs(1);
for(int i = 1;i <= n;++ i) modify(i, 1);
for(int i = 1;i <= m;++ i)
{
char tmp1;
int tmp2;
scanf("%c", &tmp1);
read(tmp2);
if(tmp1 == 'Q')
printf("%d\n", ask(out[tmp2]) - ask(in[tmp2] - 1));
else if(tmp1 == 'C')
{
int tmp3 = ask(in[tmp2]) - ask(in[tmp2] - 1);
if(tmp3 == 1) modify(in[tmp2], -1);
else modify(in[tmp2], 1);
}
}
return 0;
}
POJ3321Apple Tree的更多相关文章
- POJ3321Apple Tree[树转序列 BIT]
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26995 Accepted: 8007 Descr ...
- POJ3321Apple Tree Dfs序 树状数组
出自——博客园-zhouzhendong ~去博客园看该题解~ 题目 POJ3321 Apple Tree 题意概括 有一颗01树,以结点1为树根,一开始所有的结点权值都是1,有两种操作: 1.改变其 ...
- poj3321-Apple Tree(DFS序+树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36442 Accepted: 10894 Desc ...
- POJ3321Apple Tree【dfs 树状数组】
题目大意:一棵树(不一定是二叉树!!),树的节点上本来都有一个苹果,要求完成以下操作: 1.指定某个节点,如果这个节点原本有苹果则拿去,如果没有苹果则填上一个苹果 2.询问某个节点以及其子树一共有多少 ...
- poj3321Apple Tree(树状数组)
http://poj.org/problem?id=3321 刚一看题以为要建一颗树 看了下讨论说dfs 这里dfs遍历时设的标号很好 一个low一个high 包含了以这一节点为根节点的子树结点的所有 ...
- C++-POJ3321-Apple Tree[数据结构][树状数组]
树上的单点修改+子树查询 用dfn[u]和num[u]可以把任意子树表示成一段连续区间,此时结合树状数组就好了 #include <set> #include <map> #i ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
- 无限分级和tree结构数据增删改【提供Demo下载】
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
随机推荐
- NX二次开发-UFUN获取图层的状态UF_LAYER_ask_status
NX11+VS2013 #include <uf.h> #include <uf_ui.h> #include <uf_layer.h> UF_initialize ...
- project2_login(登录窗口)
该project是在网易云课堂上的公开课<用 python 和 tkinter 做简单的窗口视窗>课程当中学习的,是该课程中的一个结课小项目,项目中的知识点内容涉及该课程中所学习到的大多数 ...
- 20140307 引用赋值、类的初始化、指针数组、数组指针、new
引用不能被赋值http://blog.csdn.net/laixingjun/article/details/9005200 类构造函数两种初始化方法区别,哪种好:http://blog.163.co ...
- webService服务简单实现
首先写一个简单的webservice服务 package com.service.impl; import java.util.Date; import javax.jws.WebService; i ...
- PAT L2-021. 点赞狂魔 /// sort+unique去重
https://www.patest.cn/contests/gplt/L2-021 题目大意: 微博上有个“点赞”功能,你可以为你喜欢的博文点个赞表示支持.每篇博文都有一些刻画其特性的标签,而你点赞 ...
- Spring MVC入门示例(1)
1.新建一个Java Web项目 2.导入jar包 3.在WEB-INF下面建一个hello.jsp页面. 1 <%@ page language="java" import ...
- Dribbble 流行的配色风格是什么?
Dribbble 是众所周知的设计社群网站,在网站中有许多人分享设计作品,互相交流或从其他设计获取灵感.当然也有不少网站应运而生,例如 Freebbble 可免费下载 Dribbble 数千种设计素材 ...
- MySQL 不用 Null 的理由
Null 貌似在哪里都是个头疼的问题,比如 Java 里让人头疼的 NullPointerException,为了避免猝不及防的空指针异常,千百年来程序猿们不得不在代码里小心翼翼的各种 if 判断,麻 ...
- capserjs-prototype(中)
evaluateOrDie() 具体样式: evaluateOrDie(Function fn[, String message, int status]) Evaluates an expressi ...
- 为什么学习JavaScript
为什么学习JavaScript 一.你知道,为什么JavaScript非常值得我们学习吗? 1. 所有主流浏览器都支持JavaScript. 2. 目前,全世界大部分网页都使用JavaScript. ...