POJ 3321 Apple Tree DFS序+fenwick
题目大意:有一颗长满苹果的苹果树,有两个操作。
1.询问以一个点为根的子树中有多少个苹果。
2.看看一个点有没有苹果,假设没有苹果。那么那里就立即长出一个苹果(= =!);否则就把那个苹果摘下来。
思路:进行一次深搜,将每一个节点最開始出现的时间和最后出现的时间记在一个数组里,那么这两点之间的点就是它以及它的子树的二倍,然后就用树状数组来维护区间和即可了。
CODE:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 200010
using namespace std; pair<int,int> pos[MAX]; int points,asks;
int head[MAX],total;
int next[MAX << 1],aim[MAX << 1];
int cnt;
bool src[MAX];
int fenwick[MAX]; char c[10]; inline void Add(int x,int y);
void DFS(int x,int last); inline void Fix(int x,int c);
inline int GetSum(int x); int main()
{
cin >> points;
for(int x,y,i = 1;i < points; ++i) {
scanf("%d%d",&x,&y);
Add(x,y),Add(y,x);
}
DFS(1,-1);
memset(src,true,sizeof(src));
for(int i = 1;i <= points; ++i)
Fix(pos[i].first,1),Fix(pos[i].second,1);
cin >> asks;
for(int x,i = 1;i <= asks; ++i) {
scanf("%s%d",c,&x);
if(c[0] == 'Q')
printf("%d\n",(GetSum(pos[x].second) - GetSum(pos[x].first - 1)) >> 1);
else {
if(src[x]) {
Fix(pos[x].first,-1);
Fix(pos[x].second,-1);
src[x] = false;
}
else {
Fix(pos[x].first,1);
Fix(pos[x].second,1);
src[x] = true;
}
}
}
return 0;
} inline void Add(int x,int y)
{
next[++total] = head[x];
aim[total] = y;
head[x] = total;
} void DFS(int x,int last)
{
pos[x].first = ++cnt;
for(int i = head[x];i;i = next[i]) {
if(aim[i] == last) continue;
DFS(aim[i],x);
}
pos[x].second = ++cnt;
} inline void Fix(int x,int c)
{
for(;x <= (points << 1);x += x&-x)
fenwick[x] += c;
} inline int GetSum(int x)
{
int re = 0;
for(;x;x -= x&-x)
re += fenwick[x];
return re;
}
POJ 3321 Apple Tree DFS序+fenwick的更多相关文章
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- poj 3321 Apple Tree dfs序+线段树
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Description There is an apple tree outsid ...
- POJ 3321 Apple Tree DFS序 + 树状数组
多次修改一棵树节点的值,或者询问当前这个节点的子树所有节点权值总和. 首先预处理出DFS序L[i]和R[i] 把问题转化为区间查询总和问题.单点修改,区间查询,树状数组即可. 注意修改的时候也要按照d ...
- POJ 3321 Apple Tree dfs+二叉索引树
题目:http://poj.org/problem?id=3321 动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建 ...
- POJ 3321 Apple Tree (DFS + 树状数组)
题意: 一棵苹果树有N个分叉,编号1---N(根的编号为1),每个分叉只能有一颗苹果或者没有苹果. 现在有两种操作: 1.某个分叉上的苹果从有变无或者从无边有. 2.需要统计以某个分叉为根节点时,它的 ...
- POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)
id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...
- POJ 3321 Apple Tree 【树状数组+建树】
题目链接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...
- POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25904 Accepted: 7682 Descr ...
- POJ 3321 Apple Tree (树状数组+dfs序)
题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...
随机推荐
- 互联网创业十问?good(快速迭代、把握核心用户应对抄袭,不需要把商业模式考虑完备,4种失败的信号,失败者没资格说趁着年轻...)
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:曹政链接:https://www.zhihu.com/question/20264499/answer/28168079来源: ...
- SOA,不看你永远不知道的事
你买不来SOA,只能设计自己的SOA. SOA不是新东西 SOA没有引入新概念,它是个把现有概念和实践放到一起,用于特定需求集的范式.你甚至可以说SOA别的什么都 不是,就是将实用主义和头脑风暴运用到 ...
- 保护DNS服务器3大方法
保护DNS服务器3大方法 DNS全称DomainNameSystem域名解析系统,通俗地说,DNS就是帮助用户在Internet上寻找名称与IP对应的解析服务.为了更方便使用网络资源,DN ...
- C++11 thread::detach(2)
原文地址:http://www.cplusplus.com/reference/thread/thread/detach/ public member function <thread> ...
- 问题在哪?动态菜单条-------Day86
今天做了一个动态菜单条,先上图片,简单说一下我想实现的效果: 就是以下这个地方,随着鼠标指到哪,它就划到哪,并有一个惯性的幅度,并且滑动距离越远,停住的时候惯性越大,摆动幅度越大,这就是我大概想实现的 ...
- Java 判断多级路径是否存在,不存在就创建
第一种方案: /** * 是否创建目录 * @param path * @return */ public boolean isexitsPath(String path)throws Interru ...
- CentOS 如何安装git server + Gitolite 【配置不成功需要再测试2015-8-20】
安装git 关于安装git 可以参考 http://gitolite.com/gitolite/install.html 里面有官方的介绍 1. Git 的工作需要调用 curl,zlib,open ...
- linux下编译.so 和.a 可能出现的问题 ?
1. 静态函数库 这类库的名字一般是libxxx.a:利用静态函数库编译成的文件比较大,因为整个 函数库的所有数据都会被整合进目标代码中,他的优点就显而易见了,即编译后的执行程序不需要外部的函数库支持 ...
- Spring笔记 - Bean xml装配
命名空间表 aop Provides elements for declaring aspects and for automatically proxying @AspectJannotated c ...
- linux工具:ssh---未完
ssh server_ip 或者 ssh username@server_ip 或者 ssh username@server_name , 再按提示输入密码. ____________________ ...