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
"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
"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次修改,每次修改使得某个节点的苹果改变,有变成没有,没有变成有。询问的是某个节点及其子树的苹果数目。
#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 摘苹果 构建线段树的更多相关文章

  1. POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25904   Accepted: 7682 Descr ...

  2. POJ 3321 Apple Tree dfs+二叉索引树

    题目:http://poj.org/problem?id=3321 动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建 ...

  3. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  4. POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)

    id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...

  5. POJ 3321 Apple Tree 【树状数组+建树】

    题目链接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...

  6. ACM学习历程——POJ3321 Apple Tree(搜索,线段树)

          Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will ...

  7. poj 3321:Apple Tree(树状数组,提高题)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 5629 Descr ...

  8. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

  9. (简单) POJ 3321 Apple Tree,树链剖分+树状数组。

    Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow ...

随机推荐

  1. (5.12)mysql高可用系列——复制中的在线切换GTID模式/增加节点/删除节点

    目录 [0]需求 前提,已经假设好基于传统异步复制的主库和从库1. [0.1]传统异步切换成基于GTID的无损模式 [0.2]增加特殊要求的从库 [1]操作环境 [2]构建 复制->半同步复制 ...

  2. sql server不同排序规则的数据库间字段的比较

    不同的排序规则的字段是不能直接比较的.会提示:无法解决 equal to 操作的排序规则冲突.可以把字段强制转换一个排序规则,这样就能比较了.示例: ------------------------- ...

  3. Spring使用注解实现AOP

    一.AspectJ概述 AspectJ是一个面向切面的框架,它扩展了Java语言.定义了AOP语法,能够在编译期提供代码的织入,它提供了一个专门的编译期用来生成遵守字节编码规范的Class文件. @A ...

  4. 2019中山纪念中学夏令营-Day20[JZOJ] T1旅游详解

    2019中山纪念中学夏令营-Day20[JZOJ] 提高组B组 Team_B组 T1 旅游 Time Limits: 2000 ms  Memory Limits: 262144 KB Descrip ...

  5. CSS3面包屑菜单导航

    在线演示 本地下载

  6. redis 学习(9)-- redis 客户端 -- redis-py

    redis 客户端 -- redis-py 简介 关于 redis 的各种客户端,我们可以在官网上寻找并使用,比如我这里的 python 客户端,可以在官网上找到:redis-client . 获取 ...

  7. jquery ready load

    jq 加载三种写法 $(document).ready(function() { // ...代码... }) //document ready 简写 $(function() { // ...代码. ...

  8. 安装Anaconda3-201812详解

    Anaconda指的是一个开源的Python发行版本,其包含了conda.Python等180多个科学包及其依赖项.  因为包含了大量的科学包,Anaconda 的下载文件比较大(约 531 MB), ...

  9. react 在新窗口 打开页面

    遇到这个需求 首先通过 Link a去尝试直接跳转.发现2个问题 1.Link跳转 会自动进行登录校验,我设想是路由没有匹配到,去验证后大致排除了. 因为这个链接 直接粘贴到浏览器 是可以访问到的. ...

  10. Java构造二叉树、树形结构先序遍历、中序遍历、后序遍历

    package com.example.demo; public class BTree { public int data; public BTree left; public BTree rigt ...