一道dfs序+树状数组的题

因为并没有get到dfs序以及对树状数组也不熟练卡了很久orz

dfs序:

in和out是时间戳

dfs序可以将树转化成为一个序列,满足区间 -> 子树

然后就可以用树状数组之类的维护序列的东东来维护了

int idx = ;
void dfs(int u, int fa)
{
seq[++idx] = u;
in[u] = idx;
for(int i = head[u];i;i = nxt[i])
{
int v = l[i];
if(v != fa)
{
dfs(v, u);
}
}
out[u] = idx;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
const int SZ = ;
const int INF = 1e9+;
int head[SZ], nxt[SZ], l[SZ], tot = ;
int in[SZ], out[SZ], seq[SZ], a[SZ];
void build(int f, int t)
{
l[++tot] = t;
nxt[tot] = head[f];
head[f] = tot;
}
int idx = ;
void dfs(int u, int fa)
{
seq[++idx] = u;
in[u] = idx;
for(int i = head[u];i;i = nxt[i])
{
int v = l[i];
if(v != fa)
{
dfs(v, u);
}
}
out[u] = idx;
}
int d[SZ], n;
void add(int i, int x)
{
for(; i <= n; i += (i & -i))
d[i] += x;
}
int get(int i)
{
int ans = ;
for(;i;i -= (i & -i))
ans += d[i];
return ans;
}
int query(int l, int r)
{
return get(r) - get(l - );
}
int main()
{
scanf("%d", &n);
for(int i = ; i < n-; i++)
{
int x, y;
scanf("%d %d", &x, &y);
build(x, y);
build(y, x);
}
dfs(, );
/*for(int i = 0; i < 11; i++) printf("%d ", in[i]); printf("\n");
for(int i = 0; i < 11; i++) printf("%d ", out[i]); printf("\n");
for(int i = 0; i < 25; i++) printf("%d ", seq[i]); printf("\n");*/
int m;
scanf("%d", &m);
for(int i = ; i <= n; i++)
add(i, ), a[i] = ;
while(m--)
{
char op;
int x;
scanf("%c", &op);
scanf("%c", &op);
scanf("%d", &x);
if(op == 'Q')
printf("%d\n", query(in[x], out[x]));
if(op == 'C')
{
if(a[in[x]]) {a[in[x]] = ; add(in[x], -); }
else {a[in[x]] = ; add(in[x], ); }
}
}
return ;
}

树状数组 || POJ 3321 Apple Tree的更多相关文章

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

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

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

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

  3. 树状数组(Binary Indexed Tree,BIT)

    树状数组(Binary Indexed Tree) 前面几篇文章我们分享的都是关于区间求和问题的几种解决方案,同时也介绍了线段树这样的数据结构,我们从中可以体会到合理解决方案带来的便利,对于大部分区间 ...

  4. 树状数组(Binary Indexed Tree)

    树状数组(Binary Indexed Tree,BIT) 是能够完成下述操作的数据结构. 给一个初始值全为 0 的数列 a1, a2, ..., an (1)给定 i,计算 a1+a2+...+ai ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 通过链接将JSP页面中一变量传到另一JSP界面中

    A.jsp 发送 <a herf="B.jsp?name=<%=name%>">传递到B页面</a> B.jsp  接收 <%String ...

  2. Java中手动提交事务

    项目中遇到一个问题,就是在程序的执行过程中需要不断地更新某个信息,但是在springmvc中好像是默认不可以的,那么就需要手动提交 // 从spring容器对象中获取DataSourceTransac ...

  3. Ubuntu16.04 : 用友善提供的4.5.1解压后,运行/opt/FriendlyARM/toolschain/4.5.1/bin/arm-linux-gcc -v出错

    通过查阅百度和谷歌,以下解决方法: The problem has been solved, because I installed the amd64.iso linux system,so fir ...

  4. 洛谷P3825 [NOI2017]游戏(2-SAT)

    传送门 果然图论的题永远建图最麻烦……看着题解代码的建图过程真的很珂怕…… 先不考虑地图$x$,那么每一个地图都只能用两种赛车,于是我们可以用2-SAT来搞,用$i$表示这个地图能用的第一辆车,$i' ...

  5. Luogu P3946 ことりのおやつ(小鸟的点心) 【最短路】By cellur925

    题目传送门 日本的冬天经常下雪.不幸的是,今天也是这样,每秒钟雪的厚度会增加q毫米. 秋叶原共有n个地点,编号从1到n.每个地点在开始的时候的积雪高度为hi. 有m条双向道路连接这些地点,它们的长度分 ...

  6. 最小公倍数的最小和(Minimum Sum LCM )

    #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> u ...

  7. iOS UITextView自适应高度UITextContainerView抖动问题

    在打造一个类似于微信朋友圈评论输入框的时候,需要动态调整输入框的高度, 但是,在调整了UITextView的高度之后,继续输入会导致内容(UITextContainerView里的文字)抖动. scr ...

  8. SpringBoot | 问题 | 注解方式下无法发现Bean

    在排除注解的问题后,考虑扫描类的位置, [SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描! “Application类”是指SpringBoot项 ...

  9. 在服务器上执行hbase的jar包

    hadoop命令执行hbase应用jar包时的环境变量加载问题 Apache HBase ™ Reference Guide HBase, MapReduce, and the CLASSPATH

  10. _bzoj1911 [Apio2010]特别行动队【斜率优化dp】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1911 裸的斜率优化dp. #include <cstdio> const int ...