树状数组 || POJ 3321 Apple Tree
一道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的更多相关文章
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)
id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...
- 树状数组(Binary Indexed Tree,BIT)
树状数组(Binary Indexed Tree) 前面几篇文章我们分享的都是关于区间求和问题的几种解决方案,同时也介绍了线段树这样的数据结构,我们从中可以体会到合理解决方案带来的便利,对于大部分区间 ...
- 树状数组(Binary Indexed Tree)
树状数组(Binary Indexed Tree,BIT) 是能够完成下述操作的数据结构. 给一个初始值全为 0 的数列 a1, a2, ..., an (1)给定 i,计算 a1+a2+...+ai ...
- POJ 3321 Apple Tree 【树状数组+建树】
题目链接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...
- poj 3321:Apple Tree(树状数组,提高题)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18623 Accepted: 5629 Descr ...
- (简单) POJ 3321 Apple Tree,树链剖分+树状数组。
Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow ...
- POJ 3321 Apple Tree(树状数组)
Apple Tree Time Limit: 2000MS Memory Lim ...
- POJ 3321 Apple Tree (树状数组+dfs序)
题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...
随机推荐
- pl/sql 远程连接oracl服务器方法
在Oracle/network/admin中的tnsnames.ora中添加对应的如下代码: LISTENER_ORCL = (DESCRIPTION = (ADDRESS = (PROTOCOL = ...
- 看鸟哥的Linux私房菜的一些命令自我总结(一)
-显示目前所支持的语言 echo &LANG -修改语言成为英文系统 LANG=en_US -显示日历的命令 cal [[month] year] -惯用关机命令 shutdown 参数: ...
- jqGrid 编辑完数据后能返回到当前位置的方法
jqGrid 是一个js的jquery组件,虽然不轻便,但功能还是蛮强大的,也比较方便使用.在数据加载后,经常需要对其中的记录进行编辑,修改完后再返回时需要看到修改后的数据,一般采取重新加载的方法re ...
- XTU1266:Parentheses(贪心+优先队列)
传送门 题意 从左到右有n个连续的组,每一组有Li个括号,要么全是左括号,要么全是右括号,以及该组的每一个左括号翻成右括号, 或者右括号翻成左括号的花费Di.可以对这n个组的括号进行翻转,每一个括号都 ...
- poj3669【bfs】
题意: 有个**要看流星雨,可是流星雨会砸死他的... 给出n个流星雨下落的坐标,时间,如果那个**在下落坐标或是上下左右就会gg,问你他最短跑到流星雨打不到的地方的时间. 思路: ①:预处理出一个矩 ...
- hadoop wordcount程序缺陷
在wordcount 程序的main函数中,没有读取运行环境中的各种参数的值,全靠hadoop系统的默认参数跑起来,这样做是有风险的,最突出的就是OOM错误. 自己在刚刚学习hadoop编程时,就是模 ...
- memcache操作
1 格式(telnet) <command name> <key> <flags> <exptime> <bytes> a) <com ...
- C# 对象复制
/// <summary> /// 把DataTable对象转成List<T>对象 /// </summary> /// <typeparam name=&q ...
- Apache Kylin Cube 的存储
不多说,直接上干货! 简单的说Cuboid的维度会映射为HBase的Rowkey,Cuboid的指标会映射为HBase的Value. Cube映射成HBase存储 如上图原始表所示:Hive表有两个维 ...
- nginx缓存配置及开启gzip压缩
阅读目录 一:nginx缓存配置 二:nginx开启gzip 回到顶部 一:nginx缓存配置 在前一篇文章,我们理解过http缓存相关的知识点, 请看这篇文章. 今天我们来学习下使用nginx服务来 ...