题目:http://poj.org/problem?id=3321

动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建好,只要重新标号成连续的就行了。

感觉重新标号是这个题最难的地方,否则就是个纯水题了。。。

重新标号是看的别人的。。。用dfs遍历多叉树标号。

 #include <stdio.h>
#include <stdlib.h>
#include <string.h> int item = ;
int c[], n;
int start[], end[]; struct Tree
{
int num;
struct Tree *next;
Tree()
{
next = NULL;
num = ;
}
}tree[]; int lowbit(int x)
{
return x & (-x);
} void add(int x, int y)
{
while(x <= n)
{
c[x] += y;
x += lowbit(x);
}
} int sum(int x)
{
int ret = ;
while(x)
{
ret += c[x];
x -= lowbit(x);
}
return ret;
} int query(int x, int y)
{
return sum(y) - sum(x-);
} void dfs(int x)
{
start[x] = ++item;
struct Tree *p = tree[x].next;
while(p != NULL)
{
if(start[p->num] == )
dfs(p->num);
p = p->next;
}
end[x] = item;
} int main()
{
int u, v;
scanf("%d", &n);
memset(tree, , sizeof(tree));
memset(start, , sizeof(start));
memset(c, , sizeof(c));
for(int i = ; i < n; i++)
{
scanf("%d %d", &u, &v);
struct Tree *p = new Tree;
p->num = v;
p->next = tree[u].next;
tree[u].next = p;
}
dfs();
for(int i = ; i <= n; i++)
{
add(i, );
}
int q, z;
char cmd[];
scanf("%d", &q);
while(q--)
{
scanf("%s %d", cmd, &z);
if(cmd[] == 'Q')
{
printf("%d\n", query(start[z], end[z]));
}
else
{
if(sum(start[z]) - sum(start[z]-) == )
add(start[z], -);
else add(start[z], );
}
}
return ;
}

POJ 3321 Apple Tree dfs+二叉索引树的更多相关文章

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

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

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

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

  3. #5 DIV2 A POJ 3321 Apple Tree 摘苹果 构建线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25232   Accepted: 7503 Descr ...

  4. POJ 3321 Apple Tree DFS序+fenwick

    题目大意:有一颗长满苹果的苹果树,有两个操作. 1.询问以一个点为根的子树中有多少个苹果. 2.看看一个点有没有苹果,假设没有苹果.那么那里就立即长出一个苹果(= =!):否则就把那个苹果摘下来. 思 ...

  5. POJ 3321 Apple Tree (DFS + 树状数组)

    题意: 一棵苹果树有N个分叉,编号1---N(根的编号为1),每个分叉只能有一颗苹果或者没有苹果. 现在有两种操作: 1.某个分叉上的苹果从有变无或者从无边有. 2.需要统计以某个分叉为根节点时,它的 ...

  6. POJ 3321 Apple Tree DFS序 + 树状数组

    多次修改一棵树节点的值,或者询问当前这个节点的子树所有节点权值总和. 首先预处理出DFS序L[i]和R[i] 把问题转化为区间查询总和问题.单点修改,区间查询,树状数组即可. 注意修改的时候也要按照d ...

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

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

  8. 二叉索引树BIT

    定义     二叉索引树,binary index tree,又名树状数组,或Fenwick Tree,因为本算法由Fenwick创造.     对于数组A,定义Query(i,j) = Ai +Ai ...

  9. HDU 1166 敌兵布阵(线段树 or 二叉索引树)

    http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N<=50000),表示敌人有 ...

随机推荐

  1. ios7自带的晃动效果

    ios7自带的晃动效果 by 伍雪颖 - (void)registerEffectForView:(UIView *)aView depth:(CGFloat)depth; { UIInterpola ...

  2. 阿里云ONS而微软Azure Service Bus体系结构和功能比较

    阿里云ONS而微软Azure Service bus体系结构和功能比较 版权所有所有,转载请注明出处http://blog.csdn.net/yangzhenping.谢谢! 阿里云的开放消息服务: ...

  3. css图片垂直居中

    css图片垂直居中一.style代码 .case-pic{ height: 125px; position: relative; text-align: center } .case-pic span ...

  4. Docker中开启sshd服务

    ssh服务安装 安装ssh服务 #yum install openssh-server -y 安装passwd(修改密码需要) #yum install passwd -y 修改sshd_config ...

  5. webrtc学习——mediaStream和MediaStreamTrack

    This is an experimental technologyBecause this technology's specification has not stabilized, check ...

  6. tomcat启动

    http://jingyan.baidu.com/article/c33e3f48a42352ea15cbb5d4.html

  7. Servlet单实例多线程模式

    http://kakajw.iteye.com/blog/920839 前言:Servlet/JSP技术和ASP.PHP等相比,由于其多线程运行而具有很高的执行效率.由于Servlet/JSP默认是以 ...

  8. Jquery Ajax调用aspx页面实例

    目前,我会的几种asp.net界面与后台代码交互方式有几种: 1.webform+服务器控件交互: 2.webform+jquery+ajax+一般处理程序交互: 3.webform+jquery+a ...

  9. Object-C Dealloc

    上一篇为Object-C Init dealloc方法在一个对象从内存中删除时被调用.通常在这个方法里面释放所有对象里的实例变量 -(void)dealloc { [super dealloc]; [ ...

  10. ios专题 -KVO , KVC

    KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知. addObserver:  forKeyPath: options: conte ...