#5 DIV2 A POJ 3321 Apple Tree 摘苹果 构建线段树
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 25232 | Accepted: 7503 |
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf =0x7f7f7f7f;
const double pi=acos(-1);
const int maxn=100000+10; int dfs_clock=0,vis[4*maxn],s[4*maxn],e[4*maxn];
vector<vector<int> > G(4*maxn);
struct node{
int l,r,val,flag;
int mid(){
return (l+r)>>1;
}
}tree[4*maxn]; void push_up(int k)
{
tree[k].val=tree[2*k].val+tree[2*k+1].val;
} void build(int k,int l,int r)
{
tree[k].l=l;tree[k].r=r;
if(l==r) {tree[k].val=1;return;}
build(2*k,l,(l+r)/2);
build(2*k+1,(l+r)/2+1,r);
push_up(k);
} void dfs(int u)
{
dfs_clock++;
vis[u]=1;
s[u]=dfs_clock;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(vis[v]) continue;
dfs(v);
}
e[u]=dfs_clock;
} void update(int k,int pos)
{
if(tree[k].l==tree[k].r) {tree[k].val^=1;return;}
if(pos<=tree[k].mid()) update(2*k,pos);
else update(2*k+1,pos);
push_up(k);
} int query(int k,int l,int r)
{
if(l<=tree[k].l&&tree[k].r<=r)
return tree[k].val;
else if(tree[k].r>=l&&tree[k].l<=r)
{
int res=0;
res+=query(2*k,l,r);
res+=query(2*k+1,l,r);
return res;
}
else return 0;
} int main()
{
int n,m;
while(~scanf("%d",&n))
{
MM(vis,0);
for(int i=1;i<=n;i++) G[i].clear();
for(int i=1;i<=n-1;i++)
{
int u,v;
scanf("%d %d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs_clock=0;
dfs(1);
build(1,1,n); scanf("%d",&m);
for(int i=1;i<=m;i++)
{
char a[3];int x;
scanf("%s %d",a,&x);
if(a[0]=='Q') printf("%d\n",query(1,s[x],e[x]));
else update(1,s[x]);
}
}
return 0;
}
分析:线段树很好的一道题,比赛的时候想到了单点更新维护区间和,所以应该要线段树或BIT,但是
这棵树连二叉树都不是,就不知道怎么维护了,,,,这也是这道题的精华所在,首先dfs一次,依据
时间戳,得出每个节点维护的区间,这样子节点维护的区间必然被父节点包含,然后就是常规的线段树了,
单点更新时就只要更新该点所在时间戳的那个点,这样包含该点的区间的值也会相应修改,查询某点
及其子树的值,就只要查询这个点对应的区间就好,神奇的一题
#5 DIV2 A POJ 3321 Apple Tree 摘苹果 构建线段树的更多相关文章
- POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25904 Accepted: 7682 Descr ...
- POJ 3321 Apple Tree dfs+二叉索引树
题目:http://poj.org/problem?id=3321 动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建 ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)
id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...
- POJ 3321 Apple Tree 【树状数组+建树】
题目链接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...
- ACM学习历程——POJ3321 Apple Tree(搜索,线段树)
Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will ...
- poj 3321:Apple Tree(树状数组,提高题)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18623 Accepted: 5629 Descr ...
- poj 3321 Apple Tree dfs序+线段树
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Description There is an apple tree outsid ...
- (简单) POJ 3321 Apple Tree,树链剖分+树状数组。
Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow ...
随机推荐
- 2. zookeeper介绍及集群搭建
ZooKeeper 概述 Zookeeper 是一个分布式协调服务的开源框架. 主要用来解决分布式集群中 应用系统的一致性问题,例如怎样避免同时操作同一数据造成脏读的问题. ZooKeeper 本质上 ...
- python面向对象的多态-类相关内置函数-类内置魔法函数-迭代器协议-上下文管理-04
多态 一种事物具备不同的形态 例如:水 --> 固态.液态.气态 多态:# 多个不同对象可以相应同一个对象,产生不同的结果 首先强调,多态不是一种特殊的语法,而是一种状态,特性(多个不同对象可以 ...
- mysql update join
随手记录一下 UPDATE information f1 LEFT JOIN topic f2 ON f1.id = f2.id SET f1.img_url = f2.img_url
- 获取iframe中的tree
window.frames["iframe_name"].document.getElementById("..."); 或者 window.frames['i ...
- 第一章 T-SQL查询和编程基础 T-SQL语言基础(2)
T-SQL查询和编程基础 (2) 1.3 创建表和定义数据完整性 注意:表是属于架构,而架构又是属于数据库的 -- Create a database called testdb IF DB_ID(' ...
- 关于redis的几件小事(八)缓存与数据库双写时的数据一致性
1.Cache aside pattern 这是最经典的 缓存+数据库 读写模式,操作如下: ①读的时候,先读缓存,缓存没有就读数据库,然后将取出的数据放到缓存,同时返回请求响应. ②更新的时候,先删 ...
- SpringBoot + Vue + nginx项目部署(零基础带你部署)
一.环境.工具 jdk1.8 maven spring-boot idea VSVode vue 百度网盘(vue+springboot+nginx源码): 链接:https://pan.baidu. ...
- 免费FQ工具
这里使用免费的`梯子` 下载赛风 选择Windows下载 下载好之后,直接点击打开,它会自动连接,什么也不要设置,下载的就是一个exe文件,连接成功会打开一个网页,不用管
- hadoop安装zookeeper-3.4.12
在安装hbase的时候,需要安装zookeeper,当然也可以用hbase自己管理的zookeeper,在这里我们独立安装zookeeper-3.4.12. 下载地址:https://mirrors. ...
- 解决xshell小键盘输入时串码(干货!!)
点击文件——属性 点击终端,修改为Linux即可