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二次开发-创建CSYS坐标系UF_CSYS_create_csys
NX9+VS2012 #include <uf.h> #include <uf_csys.h> #include <uf_mtx.h> UF_initialize( ...
- NX二次开发-UFUN工程图表格注释section转tag函数UF_TABNOT_ask_tabular_note_of_section
NX9+VS2012 #include <uf.h> #include <uf_tabnot.h> #include <NXOpen/Part.hxx> #incl ...
- NX二次开发-NXOpen中Point3d类型转换成point类型
NX9+VS2012 #include <NXOpen/NXObject.hxx> #include <NXOpen/Part.hxx> #include <NXOpen ...
- 笨办法学Python记录--习题15-17 开始读写文件啦
习题15 - 17 打开并读流程: from sys import argv script,filename = argv txt = open(filename) print "Here' ...
- Spring-Security (学习记录五)--配置登录时,密码采用md5加密,以及获取登录信息属性监听同步自己想要的登录信息
目录 1. PasswordEncoder 采用密码加密 2. 获取当前的用户信息 1. PasswordEncoder 采用密码加密 使用前面的例子.可以看出我们数据库密码是采用明文的,我们在登录的 ...
- C#-Api:身份证识别
ylbtech-C#-Api:身份证识别 通过POST上传base64格式的图片内容,可识别二代身份证.驾照.行驶证.军官证.中华人民共和国往来港澳通行证.台湾居民往来大陆通行证.大陆居民往来台湾通行 ...
- 12-MySQL-Ubuntu-数据表的查询-数据准备和基本查询(一)
一,数据准备 创建数据库.数据表 -- 创建数据库 create database python_test_1 charset=utf8; -- 使用数据库 use python_test_1; -- ...
- Apache Spark 2.2.0 中文文档 - Spark RDD(Resilient Distributed Datasets)
Spark RDD(Resilient Distributed Datasets)论文 概要 1: 介绍 2: Resilient Distributed Datasets(RDDs) 2.1 RDD ...
- flutter 底部bottomNavigationBar凸起效果
概要 最近在做flutter 的时候,之前看到想实现 底部导航栏中间按钮 凸起效果, 最近想做又突然找不到方案了,因此记录下这里的实现方式. 预览效果 代码 主要使用 BottomAppBar 组建, ...
- npm ERR! Failed at the gff@1.0.0 start script.
code ELIFECYCLE npm ERR! errno 1 npm ERR! gff@1.0.0 start: `node build/dev-server.js` npm ERR! Exit ...