树状数组 || 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节点以及它子树的值之和.初 ...
随机推荐
- VPS 安全措施(CentOS 6)
新到手一台VPS,要做的第一件事大概是做好安全措施. 下面针对CentOS 6随便写点,我目前做的几步是: 修改root密码 SSH-key登录 配置iptable 安装fail2ban 1.修改ro ...
- 【旧文章搬运】扩展一下ProcessNotify~~
原文发表于百度空间,2009-01-08 看雪论坛地址:https://bbs.pediy.com/thread-80109.htm DebugMan论坛地址:http://www.debugman. ...
- oracle 查看表是否存在、包含某字段的表、表是否包含字段
表是否存在: select count(*) from user_tables where table_name = #{tablename} 包含某个字段的表 select * from user_ ...
- 使用CompletableFuture+ExecutorService+Logback的多线程测试
1. 环境 Java: jdk1.8.0_144 2. 背景 Java多线程执行任务时,Logback输出的主线程和各个子线程的业务日志需要区分时,可以根据线程池和执行的线程来区分,但若要把它们联系起 ...
- 2.25-2.26 MapReduce执行流程Shuffle讲解
原文链接:https://langyu.iteye.com/blog/992916 Shuffle过程是MapReduce的核心,也被称为奇迹发生的地方.要想理解MapReduce, Shuffle是 ...
- 斯坦福CS231n—深度学习与计算机视觉----学习笔记 课时1
课时1 计算机视觉历史回顾与介绍上 CS231n:这一一门关于计算机视觉的课程,基于一种专用的模型架构,叫做神经网络(更细一点说,是卷积神经网络CNN).计算机视觉是人工智能领域中发展最为迅猛的一个分 ...
- C++开发工程师面试题库 50~100道
51. New delete 与malloc free 的联系与区别?答案:都是在堆(heap)上进行动态的内存操作.用malloc函数需要指定内存分配的字节数并且不能初始化对象,new 会自动调用对 ...
- HDU 5101
hdoj5101 lower_bound函数: 题意: 从两个不同集合拿出两个数,加的和大于k的可行的方案数 思路: 答案=从所有数中选择的两个加和大于k的数的方案数-在同一个集合中选择的两个加和大于 ...
- poj 2960 S-Nim【SG函数】
预处理出SG函数,然后像普通nim一样做即可 #include<iostream> #include<cstdio> using namespace std; const in ...
- Spring Security 使用Ajax登陆无法跳转页面解决方法
使用Security的朋友都知道,使用Security后,不再需要我们自己过多的(还需要写少量代码)写登陆的逻辑,只需要自己在html的登陆表单上面定义好输入框name为:username和passw ...