POJ 3321 Apple Tree 【树状数组+建树】
题目链接:http://poj.org/problem?id=3321
Apple Tree
Time Limit: 2000MS |
Memory Limit: 65536K |
|
Total Submissions: 34812 |
Accepted: 10469 |
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
题意概括:
给一个 N 阶无向图,要求把这个无向图转换为一颗以 1 为根的树后进行两种操作:
C x :x结点上如果有苹果就摘掉,如果没有苹果就长出一个新的来
Q x: 查询 x 结点与它的所有后代分支一共有几个苹果
解题思路:
因为原本苹果之间的关系我们不好处理,所以就给他们这些结点重新编号。
并且为了方便子树的苹果数求和(即区间查询)我们在给结点增加一个左值一个右值表示当前结点管辖的范围。
如何建树?
根据题目一开始给的边与边的关系我们可以通过 dfs 进行建树。
L[ x ] (X的左值): X结点的新编号就是该结点的左值,表示它管辖范围的下限
R[ x ] (X的右值):而子树的编号最大值为该结点的右值,表示它管辖范围的上限。
至于dfs顺序,如下
举个栗子:
N = 6;
1 - 2
1 - 6
2 - 5
2 - 3
6 - 4



到这里感觉有一点点线段树的味道了。。。
但这里要用树状数组对这些区间进行维护和查询。
例如:我要 Q 2;
由上面建好的新树我们可以知道 结点2这颗子树包含了结点2、结点5、结点3,而结点2所管辖的区间【2,4】刚好把它们包括进去了。
如果我们要求 这可子树的苹果数量,其实就是用树状数组求区间【2,4】的苹果总数
也就是右值的前缀和减去左值的前缀和(但不包括左值,因为结点本身也要算进去)即:ans = sum( R[ 2 ] ) - sum( L[ 2 ] - 1);
而C操作其实就是树状数组的单点更新而已。
注意事项:
一开始用 stl 的 vector 存无向图,虽然过了但效率不咋地。想想好久没用静态邻接表了,换了一下,快了好多。
AC code:
///建树+树状数组(stl)8080k 1047ms
/*
#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef vector<int>ve; const int MAXN = 1e5+10;
int t[MAXN], l[MAXN], r[MAXN];
bool vis[MAXN];
int N, M, cnt; vector<ve>node(MAXN); int lowbit(int x)
{
return x&(-x);
}
void add(int x, int value)
{
for(int i = x; i <= N; i+=lowbit(i))
t[i]+=value;
}
int sum(int x)
{
int res = 0;
for(int i = x; i > 0; i-=lowbit(i))
res+=t[i];
return res;
}
void dfs(int k)
{
l[k] = cnt;
for(int i = 0; i < node[k].size(); i++)
{
cnt++;
dfs(node[k][i]);
}
r[k] = cnt;
return;
}
int main()
{
scanf("%d", &N);
int a, b;
for(int i = 1; i <= N; i++)
{
vis[i] = 1;
add(i, 1);
}
for(int i = 1; i < N; i++)
{
scanf("%d%d", &a, &b);
node[a].push_back(b);
}
cnt = 1;
dfs(1);
scanf("%d", &M);
while(M--)
{
char com[3];
int px;
scanf("%s", &com);
if(com[0] == 'C')
{
scanf("%d", &px);
if(vis[px]) add(l[px], -1);
else add(l[px], 1);
vis[px] = !vis[px];
}
else
{
scanf("%d", &px);
int ans = sum(r[px]) - sum(l[px]-1);
printf("%d\n", ans);
}
}
return 0;
}
*/ ///建树+树状数组(静态连接表) 4940k 454ms
#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std; const int MAXN = 1e5+;
const int MAXM = 1e5+; struct node
{
int to, next;
}edge[MAXM<<]; int head[MAXN];
bool vis[MAXN];
int t[MAXN], l[MAXN], r[MAXN];
int N, M, cnt, key; int lowbit(int x)
{
return x&(-x);
}
void add(int x, int value)
{
for(int i = x; i <= N; i+=lowbit(i))
t[i]+=value;
}
int sum(int x)
{
int res = ;
for(int i = x; i > ; i-=lowbit(i))
res+=t[i];
return res;
} void dfs(int k)
{
l[k] = key;
for(int i = head[k]; i != -; i = edge[i].next)
{
if(l[edge[i].to]) continue;
key++;
dfs(edge[i].to);
}
r[k] = key;
} void init()
{
memset(head, -, sizeof(head));
cnt = ;
} void add_e(int u, int v)
{
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt++;
} int main()
{
scanf("%d", &N);
init();
int a, b;
for(int i = ; i <= N; i++)
{
vis[i] = ; //每个节点一开始都有苹果
add(i, ); //初始化树状数组
}
for(int i = ; i < N; i++)
{
scanf("%d%d", &a, &b); ///建无向图
add_e(a, b);
add_e(b, a);
}
key = ; dfs(); //建树
scanf("%d", &M);
int px;
char com[];
while(M--)
{
scanf("%s", &com);
if(com[] == 'C')
{
scanf("%d", &px);
if(vis[px]) add(l[px], -);
else add(l[px], );
vis[px] = !vis[px];
}
else
{
scanf("%d", &px);
printf("%d\n", sum(r[px])-sum(l[px]-));
}
}
return ;
}
POJ 3321 Apple Tree 【树状数组+建树】的更多相关文章
- POJ 3321 Apple Tree(树状数组)
Apple Tree Time Limit: 2000MS Memory Lim ...
- 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] 为从结点 ...
随机推荐
- 0前端 框架 库_千万别去碰js呀 混合APP_webAPP_美工 选有类型的语言,比如TypeScript
常用知识点,技巧 添加库到本地: (举例 element-ui) 用npm命令行把包下载到本地 在电脑里找到资源文件,比如 C:\Users\XiaoCong\AppData\Roaming\npm\ ...
- nfs 问题总结
1. [root@backup read]# touch r01.txt touch: cannot touch `r01.txt': Stale file handle 使用共享目录创建文件 ...
- apply、call、bind区别、用法
apply和call都是为了改变某个函数运行时的上下文而存在的(就是为了改变函数内部this的指向): 如果使用apply或call方法,那么this指向他们的第一个参数,apply的第二个参数是 ...
- JavaScript设计模式(二) - 单例模式
什么是单例模式? 单例模式从字面上的理解是不困难的,js上就是指只有一个对象实例. 为什么需要单例模式? 我们可以将一些成员变量封装在一个单例对象中,每次访问这些变量都只能从这个单例对象进行访问,这样 ...
- .NET平台常用框架整理
基于.NET平台常用的框架整理 转自:http://www.cnblogs.com/hgmyz/p/5313983.html 首先谢谢楼主,以后备用 自从学习.NET以来,优雅的编程风格,极度简单 ...
- Java面试题03-访问权限控制
Java面试题03-访问权限控制 1. Java中的包主要是为了防止类文件命名冲突以及方便进行代码组织和管理,因此采用域名倒置的方式来进行命名: 2. Java解释器的运行过程:首先找到环境变量CLA ...
- 远程SQL Server连接不上
运行 cmd -> 输入 netsh winsock reset重启后 应该可以连接sql了
- TFS2013 设置签出独占锁(转载)
作者:晓菜鸟 出处:http://www.cnblogs.com/52XF/p/4239056.html 在使用TFS进行源代码管理的时候VS默认允许多个签出,但在团队开发中往往需要设置独占锁(排他锁 ...
- java项目升级spring4.3.x 、jdk1.8 、tomcat8.5遇到的坑及解决方案
在将spring3.x 升级为4.3.x,jdk1.7 tomcat7升级到jdk1.8.tomcat8.5过程中,碰到了很多问题,也学习到了很多东西,现将这些问题分享出来,方便大家后续遇到同样问题时 ...
- 进程和程序(Process and Program)
原出处:http://oss.org.cn/kernel-book/ch04/4.1.htm ----------------------------------个人理解分割线------------ ...