题目链接: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
"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 阶无向图,要求把这个无向图转换为一颗以 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 【树状数组+建树】的更多相关文章

  1. POJ 3321 Apple Tree(树状数组)

                                                              Apple Tree Time Limit: 2000MS   Memory Lim ...

  2. POJ 3321 Apple Tree (树状数组+dfs序)

    题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...

  3. POJ 3321 Apple Tree 树状数组+DFS

    题意:一棵苹果树有n个结点,编号从1到n,根结点永远是1.该树有n-1条树枝,每条树枝连接两个结点.已知苹果只会结在树的结点处,而且每个结点最多只能结1个苹果.初始时每个结点处都有1个苹果.树的主人接 ...

  4. POJ 3321 Apple Tree 树状数组 第一题

    第一次做树状数组,这个东西还是蛮神奇的,通过一个简单的C数组就可以表示出整个序列的值,并且可以用logN的复杂度进行改值与求和. 这道题目我根本不知道怎么和树状数组扯上的关系,刚开始我想直接按图来遍历 ...

  5. 3321 Apple Tree 树状数组

    LIANJIE:http://poj.org/problem?id=3321 给你一个多叉树,每个叉和叶子节点有一颗苹果.然后给你两个操作,一个是给你C清除某节点上的苹果或者添加(此节点上有苹果则清除 ...

  6. POJ 3321:Apple Tree 树状数组

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22131   Accepted: 6715 Descr ...

  7. POJ--3321 Apple Tree(树状数组+dfs(序列))

    Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Descripti ...

  8. 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. ...

  9. POJ3321 Apple Tree(树状数组)

    先做一次dfs求得每个节点为根的子树在树状数组中编号的起始值和结束值,再树状数组做区间查询 与单点更新. #include<cstdio> #include<iostream> ...

  10. POJ 2486 Apple Tree [树状DP]

    题目:一棵树,每个结点上都有一些苹果,且相邻两个结点间的距离为1.一个人从根节点(编号为1)开始走,一共可以走k步,问最多可以吃多少苹果. 思路:这里给出数组的定义: dp[0][x][j] 为从结点 ...

随机推荐

  1. TeamCity 持续集成工具

    https://www.jetbrains.com/teamcity/ null

  2. Java基础26-对象初始化过程

    /* 1.因为new Test1()用到了Test1类,所以会把它从硬盘上加载进入内存 2.如果有static静态代码块就会随着类的加载而执行,还有静态成员和普通方法也会随着类的加载而被加载 3.在堆 ...

  3. Oracle 数据类型详解

    数据类型(datatype)是列(column)或存储过程中的一个属性. Oracle支持的数据类型可以分为三个基本种类:字符数据类型.数字数据类型以及表示其它数据的数据类型. 字符数据类型 CHAR ...

  4. AtCoder Grand Contest 023 C - Painting Machines

    Description 一个长度为 \(n\) 的序列,初始都为 \(0\),你需要求出一个长度为 \(n-1\) 的排列 \(P\), 按照 \(1\) 到 \(n\) 的顺序,每次把 \(P_i\ ...

  5. js录制视频并保存

    使用webAPI录制视频 经测试, 只在谷歌和火狐浏览器里起效. 代码: const streamVideo = document.querySelector('.stream') const pla ...

  6. 人民币金额大小写Js转换

    /** * 数字转中文 * @param dValue * @returns */ function chineseNumber(dValue) { var maxDec = 2; // 验证输入金额 ...

  7. mysql无法连接Can't create a new thread (errno 11)

    问题描述: 今天本地navicat连接服务器mysql出错 ,提示ERROR 1135: Can't create a new thread (errno 11); if you are not ou ...

  8. python高阶函数sorted

    原文 排序也是在程序中经常用到的算法.无论使用冒泡排序还是快速排序,排序的核心是比较两个元素的大小.如果是数字,我们可以直接比较,但如果是字符串或者两个dict呢?直接比较数学上的大小是没有意义的,因 ...

  9. Windows 10:开机显示C:\WINDOWS\system32\config\systemprofile\Desktop不可用 的解决方法

    今晨起来开机,开完机一看,弹出啦一个框框上面写着“C:\WINDOWS\system32\config\systemprofile\Desktop不可用...” 当我点击了确定之后,发现屏幕一片黑,只 ...

  10. 封装微信jssdk自定义分享代码

    var protocol = window.location.protocol; //获取协议 var host = window.location.host; //获取域名 var posuDoma ...