ACM学习历程——POJ3321 Apple Tree(搜索,线段树)
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
这道题的关键是如何把这题转换成一个区间操作的问题。
由于每次查询的是子树中苹果的个数,说明查询的区间是子树的中节点的所在区间,即子树中所有节点应在查区间内。即要创建一种映射,使得,子树中节点的标号在查询区间内。
于是一种映射便是,对每个结点赋予两个值lt和rt,lt表示以此点为根节点的子树中序号最小的,rt表示当前节点的序号。
这种映射可以通过Dfs生成。然后题目就转换成了区间操作的问题。
不过对于存初始图的问题,我一开始使用了STL里的vector和list都超时了。最后用了链式前向星。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <queue>
#include <string>
#define inf 0xfffffff
#define eps 1e-10
#define N 1000000007 using namespace std; //线段树
//区间每点增值,求区间和
const int maxn = ;
struct node
{
int lt, rt;
int val;
}tree[*maxn]; //向上更新
void PushUp(int id)
{
tree[id].val = tree[id<<].val + tree[id<<|].val;
} //建立线段树
void Build(int lt, int rt, int id)
{
tree[id].lt = lt;
tree[id].rt = rt;
tree[id].val = ;//每段的初值,根据题目要求
if (lt == rt)
{
tree[id].val = ;
return;
}
int mid = (lt + rt) >> ;
Build(lt, mid, id<<);
Build(mid + , rt, id<<|);
PushUp(id);
} //增加区间内每个点固定的值
void Add(int lt, int rt, int id, int pls)
{
if (lt <= tree[id].lt && rt >= tree[id].rt)
{
if (tree[id].val)
pls = -pls;
tree[id].val += pls * (tree[id].rt-tree[id].lt+);
return;
}
int mid = (tree[id].lt + tree[id].rt) >> ;
if (lt <= mid)
Add(lt, rt, id<<, pls);
if (rt > mid)
Add(lt, rt, id<<|, pls);
PushUp(id);
} //查询某段区间内的和
int Query(int lt, int rt, int id)
{
if (lt <= tree[id].lt && rt >= tree[id].rt)
return tree[id].val;
int mid = (tree[id].lt + tree[id].rt) >> ;
int ans = ;
if (lt <= mid)
ans += Query(lt, rt, id<<);
if (rt > mid)
ans += Query(lt, rt, id<<|);
return ans;
} //链式前向星
struct Edge
{
int to, next;
}edge[]; int head[], cnt; void AddEdge(int u, int v)
{
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt;
cnt++;
} void InitEdge()
{
memset(head, -, sizeof(head));
cnt = ;
} struct
{
int lt, rt;
}id[]; int n, m, now; void Dfs(int k)
{
if (id[k].lt != )
return;
id[k].lt = now;
for (int i = head[k]; i != -; i = edge[i].next)
{
Dfs(edge[i].to);
}
id[k].rt = now;
now++;
} void Init()
{
memset(id, , sizeof(id));
int u, v;
InitEdge();
for (int i = ; i < n; ++i)
{
scanf("%d%d", &u, &v);
AddEdge(u, v);
AddEdge(v, u);
}
now = ;
Dfs();
Build(, n, );
} void Work()
{
char op[];
int v;
for (int i = ; i < m; ++i)
{
scanf("%s%d", op, &v);
if (op[] == 'C')
{
Add(id[v].rt, id[v].rt, , );
}
else
{
printf("%d\n", Query(id[v].lt, id[v].rt, ));
}
}
} int main()
{
//freopen("test.in", "r", stdin);
while (scanf("%d", &n) != EOF && n)
{
Init();
scanf("%d", &m);
Work();
}
return ;
}
ACM学习历程——POJ3321 Apple Tree(搜索,线段树)的更多相关文章
- ACM学习历程——HDU3333 Turing Tree(线段树 && 离线操作)
Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems abou ...
- ACM学习历程—POJ1151 Atlantis(扫描线 && 线段树)
Description There are several ancient Greek texts that contain descriptions of the fabled island Atl ...
- [poj3321]Apple Tree(dfs序+树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26762 Accepted: 7947 Descr ...
- POJ 题目3321 Apple Tree(线段树)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21566 Accepted: 6548 Descr ...
- ACM学习历程——POJ3295 Tautology(搜索,二叉树)
Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some ...
- ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)
Description We all use cell phone today. And we must be familiar with the intelligent English input ...
- ACM学习历程—HDU2222 Keywords Search(字典树)
Keywords Search Description In the modern time, Search engine came into the life of everybody like G ...
- ACM学习历程—SNNUOJ 1239 Counting Star Time(树状数组 && 动态规划 && 数论)
http://219.244.176.199/JudgeOnline/problem.php?id=1239 这是这次陕西省赛的G题,题目大意是一个n*n的点阵,点坐标从(1, 1)到(n, n),每 ...
- hdu 5274 Dylans loves tree(LCA + 线段树)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
随机推荐
- PowerDesigner将PDM导出生成WORD文档(转)
今天的温习老知识,是如何将一个PD设计的PDM来导出WORD文档,这是一个非常实用的功能,可以在软件过程的数据库设计文档编写中节省N多时间, 那不废话了,我们就开始今天的讲解吧! 第一步,点击Repo ...
- 世界更清晰,搜狐新闻客户端集成HUAWEI HiAI 亮相荣耀Play发布会!
6月6日,搭载有“很吓人”技术的荣耀Play正式发布,来自各个领域的大咖纷纷为新机搭载的惊艳技术站台打call,其中,搜狐公司董事局主席兼首席执行官张朝阳揭秘:华为和搜狐新闻客户端在硬件AI方面做 ...
- JAVA进阶-多线程(2)
堵塞队列: 1)BlockingQueue该接口提供了: add()/remove() 假设当队列没有数据,从队列中取数据;或者队列中数据已满, 向队列中加入数据;则会抛出异常. put()/take ...
- mongodb的IO测试工具 mongoperf
之前没发现mongoperf这个工具,测试IO的状态用的是iostat来进行观察. mongoperf < myjsonconfigfile echo "{nThreads:2,fi ...
- linux环境tomcat配置及hadoop 2.6伪分布模式安装配置
一.ubuntu 15.04.openjdk1.7.tomcat7环境配置 1. 配置openjdk1.7,输入命令: -jdk 2. 查看java是否安装成功,输入命令: envjava -vers ...
- rpm包查看和解压(转)
From:http://www.51testing.com/html/57/28557-205195.html 查看rpm包内容: rpm -qpl *.rpm 解压rpm包: rpm2cpio *. ...
- opensearch空查询
query子句不支持为空的查询,可以使用filter子句:filter=area="" 或者 filter=filedlen(area)=0 可以使用相关性函数实现:https ...
- OpenStack 使用Ceph 配置指导
概述 Ceph 作为分布式文件系统,不但具有高可靠性.高扩展性.高性能. 也是统一存储系统.支持对象存储.块存储.文件存储,本文介绍怎样使用Ceph 块存储作为OpenStack的Glance.Nov ...
- tomcat+java 占cpu 调试【top命令应用】
原文出处:http://www.blogjava.net/hankchen 现象: 在tomcat中部署java的web应用程序,过一段时间后出现tomcat的java进程持续占用cpu高达100%, ...
- 【BZOJ4568】[Scoi2016]幸运数字 倍增+线性基
[BZOJ4568][Scoi2016]幸运数字 Description A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个幸运数字,以纪念 ...