poj3321Apple Tree(树状数组)
http://poj.org/problem?id=3321
刚一看题以为要建一颗树 看了下讨论说dfs
这里dfs遍历时设的标号很好 一个low一个high 包含了以这一节点为根节点的子树结点的所有标号
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define N 100010
#define lowbit(x) (x&(-x))
struct node
{
int u,v,next;
}ed[N<<];
int head[N],t,low[N],g,high[N],vis[N],re[N],f[N];
void init()
{
t = ;
memset(head,-,sizeof(head));
}
void add(int u,int v)
{
ed[t].u = u;
ed[t].v = v;
ed[t].next = head[u];
head[u] = t;
t++;
}
void dfs(int u)
{
low[u] = ++g;
vis[u] = ;
int i;
for(i = head[u] ; i != - ; i = ed[i].next)
{
int v = ed[i].v;
if(!vis[v])
dfs(v);
}
high[u] = g;
}
void add1(int i,int da)
{
while(i<=g)
{
re[i]+=da;
i+=lowbit(i);
}
}
int getsum(int i)
{
int sum = ;
while(i)
{
sum+=re[i];
i-=lowbit(i);
}
return sum;
}
int main()
{
int i,j,k,n,m,a,b;
char s[];
init();
scanf("%d",&n);
for(i = ; i < n ; i++)
{
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
dfs();
for(i = ; i <= g ; i++)
{
f[i] = -;
add1(i,);
}
scanf("%d",&m);
while(m--)
{
scanf("%s %d",s,&k);
if(s[]=='Q')
{
printf("%d\n",getsum(high[k])-getsum(low[k]-));
}
else
{
add1(low[k],f[k]);
f[k] = f[k]*-;
}
}
return ;
}
poj3321Apple Tree(树状数组)的更多相关文章
- HDU3333 Turing Tree 树状数组+离线处理
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- POJ 3321 Apple Tree(树状数组)
Apple Tree Time Limit: 2000MS Memory Lim ...
- HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)
题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...
- POJ--3321 Apple Tree(树状数组+dfs(序列))
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Descripti ...
- gym 100589A queries on the Tree 树状数组 + 分块
题目传送门 题目大意: 给定一颗根节点为1的树,有两种操作,第一种操作是将与根节点距离为L的节点权值全部加上val,第二个操作是查询以x为根节点的子树的权重. 思路: 思考后发现,以dfs序建立树状数 ...
- POJ 3321:Apple Tree 树状数组
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22131 Accepted: 6715 Descr ...
- 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> ...
- HDU4836 The Query on the Tree(树状数组&&LCA)
由于智力的问题,百度之星完全lu不动..开场看第一题根据题目给的条件我觉得一定是可以构造出来的,题目给的意思颇有鸽巢原理的感觉,于是觉得开场第一题应该就是智力构造题了,想了半个小时,发现完全想不动,于 ...
- HDU 3333 Turing Tree (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...
随机推荐
- 配置ADB 工具 (Win7_64)
ADB (Android Debut Bridge) ADB这个工具, 让我们可以用电脑来操纵手机 Android studio 安装好之后在SDK 中就有ADB 但是我们想使用它还需要配置它的环境变 ...
- [OpenXml] Read/Write row/cell from excel
public static void test(){ using (SpreadsheetDocument document = SpreadsheetDocument.Open("test ...
- Nginx Location配置总结及基础最佳实践
参考来源: http://blog.zol.com.cn/1067/article_1066186.html,http://flandycheng.blog.51cto.com/855176/2801 ...
- Linked List Sorting (链表)
Linked List Sorting (链表) A linked list consists of a series of structures, which are not necessari ...
- NET Reflector 8 使用
一,把杀毒软件停掉 二,把原机器上的 Reflector 文件删除 三,找到C:\Users\Administrator\AppData\Local\Red Gate这个目录,将里面的东西删除 四,v ...
- 统一iOS客户端和服务器端认证
最近公司的同事业余时间搞了一个内部的类about.me(https://about.me/)的网站Ocelots,想来是一个很洋气的注意,以后跟客户介绍公司的时候,直接登录该网站,谈到谁的时候,就打开 ...
- Asp.net MVC 如何向webform一样在IIS里添加虚拟目录
相信很多用webform的程序猿都习惯性的使用虚拟目录的形式来对一个程序添加新的功能,那么在mvc下该如何来弄呢? 首先得有一个项目基层的项目,然后我们在这个项目的基础上新增一个功能模块,例如信息发布 ...
- Xcode免证书打包ipa
1,创建证书 打开“钥匙串访问”创建证书 填写好内容后点击继续,之后的步骤什么都不用改,一路点击“确定”和“继续”,最后完成这个向导就可以了. 我们创建的证书是不被信任的,右键点击证书选择“显示简介” ...
- python学习笔记27(python中sys模块的使用)
sys.argv 命令行参数List,第一个元素是程序本身路径 sys.modules.keys() 返回所有已经导入的模块列表 sys.exc_info() 获取当前正在 ...
- Linux必学的60个命令
inux必学的60个命令Linux提供了大量的命令,利用它可以有效地完成大量的工作,如磁盘操作.文件存取.目录操作.进程管理.文件权限设定等.所以,在Linux系统上工作离不开使用系统提供的命令.要想 ...