题意:

一棵苹果树有N个分叉,编号1---N(根的编号为1),每个分叉只能有一颗苹果或者没有苹果。 现在有两种操作:

1.某个分叉上的苹果从有变无或者从无边有。

2.需要统计以某个分叉为根节点时,它的子树上(包括该分叉)共有多少苹果。

分析: 有两种操作,基本就是使用数据结构维护的题目了。开始想了很久,不懂如何将分叉转化为一维线性的树状数组维护。 看了下discuss,有人说了时间戳三字。想了想,发现如果按照节点遍历的顺序可以制造出时间上的线性关系。 例如:

连接情况为:1---->2     1--->3    3--->4        3--->5

以1为根节点开始dfs,则遍历到每个点的时间可以为       1---->1     3--->2    4--->3    5---->4     2--->5

用两个数组begin,end统计以节点i为根时,遍历的第一个点的时间,和遍历最后一个点的时间

所以:begin[1] = 1 ;  end[1] = 5;   begin[2] = 5 ; end[2] = 5;   begin[3] = 2; end[3] = 4;........................................................

改变节点i的状态,时间戳小于i的会受影响;求和时,只需求(begin[i],end[i])的和了............................这样就变成了单点更新,区间求和的问题了。

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <climits>//形如INT_MAX一类的
#define MAX 100005
#define INF 0x7FFFFFFF
#define REP(i,s,t) for(int i=(s);i<=(t);++i)
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define mp(a,b) make_pair(a,b)
#define L(x) x<<1
#define R(x) x<<1|1
# define eps 1e-5
//#pragma comment(linker, "/STACK:36777216") ///传说中的外挂
using namespace std; int begin[MAX],end[MAX],cnt[MAX],vis[MAX];
int c[MAX];
int n,m,step;
char op;
struct node {
int s,e,next;
} ed[MAX];
int head[MAX],num; void init() {
memset(head,-1,sizeof(head));
num = 0;
step = 1;
memset(c,0,sizeof(c));
memset(cnt,1,sizeof(cnt));
memset(vis,0,sizeof(vis));
} void addedge(int s,int e) {
ed[num].s = s;
ed[num].e = e;
ed[num].next = head[s];
head[s] = num ++;
} int lowbit(int x) {
return x & (-x);
} void update(int x,int va) {
while(x > 0) {
c[x] += va;
x -= lowbit(x);
}
} int query(int x) {
int sum = 0;
while(x <= n) {
sum += c[x];
x += lowbit(x);
}
return sum;
} void dfs(int v0) {
vis[v0] = 1;
begin[v0] = step;
for(int i=head[v0]; i != -1; i = ed[i].next) {
int e = ed[i].e;
if(vis[e] == 0) {
step ++;
dfs(e);
}
}
end[v0] = step;
} int main() {
init();
scanf("%d",&n);
int x,y;
for(int i=0; i<n-1; i++) {
scanf("%d%d",&x,&y);
addedge(x,y);
}
dfs(1);
for(int i=1; i<=n; i++) update(i,1);
scanf("%d",&m);
for(int i=0; i<m; i++) {
getchar();
scanf("%c%d",&op,&x);
if(op == 'Q') {
printf("%d\n",query(begin[x]) - query(end[x] + 1));
}
if(op == 'C') {
cnt[x] ++;
if(cnt[x] % 2 == 0) update(begin[x],-1);
else update(begin[x],1);
}
}
return 0;
}

POJ 3321 Apple Tree (DFS + 树状数组)的更多相关文章

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

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

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

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

  3. poj 3321 Apple Tree(一维树状数组)

    题目:http://poj.org/problem?id=3321 题意: 苹果树上n个分叉,Q是询问,C是改变状态.... 开始的处理比较难,参考了一下大神的思路,构图成邻接表 并 用DFS编号 白 ...

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

    点我看题目  题意 : 大概是说一颗树有n个分岔,然后给你n-1对关系,标明分岔u和分岔v是有边连着的,然后给你两个指令,让你在Q出现的时候按照要求输出. 思路 :典型的树状数组.但是因为没有弄好数组 ...

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

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

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

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

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

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

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

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

  9. POJ3321 Apple Tree (树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16180   Accepted: 4836 Descr ...

随机推荐

  1. 360WIFI下使用Fiddler抓取手机APP流量

    过程其实很简单,仅仅做个记录.过程适用Android和ios手机 fidder的原理如下: 下面开始步骤: 1.在fidder中设置代理:Tools--->Fidder Options,设置端口 ...

  2. rnqoj-49-加分二叉树-(区域动归+记忆化)

    区域动归的问题 #include<stdio.h> #include<string.h> #include<iostream> #include<algori ...

  3. formidable上传图片

    function uploadfiles(res, req){ var form = new formidable.IncomingForm(); form.parse(req,function(er ...

  4. 一款超出你想象的代码审阅软件understand

    看源码人们一般会想到source insight这款软件可是这款软件目前只支持windows平台,那如果我想在Linux平台上审阅代码呢, 没关系还有一款强大的软件understand,这款软件能够生 ...

  5. yarn状态机的可视化

    YARN为了实现多个状态机的对象,控制ResourceManager中间RMAppImpl.RMApp-AttemptImpl.RMContainerImpl和RMNodeImpl,NodeManag ...

  6. C++实现20个设计模式

    http://c.chinaitlab.com/special/sjms/Index.html 一个月下来,把常见的20个设计模式好好复习并且逐个用C++实现了一遍,收获还是很大的,很多东西看上去明白 ...

  7. Visual Studio常用的快捷键

      每次在网上搜关于VS有哪些常用快捷键的时候,出来的永远是一串长的不能再长的列表,完全没体现出“常用”二字,每次看完前面几个就看不下去了,相信大家都 有这种感觉.其实我们平时用的真的只有很少的一部分 ...

  8. 谈谈javascript的函数表达式及其应用

    我们都知道定义函数的方式有两种,一种是函数声明,另外一种就是函数表达式. 函数声明 语法为:function关键字后跟函数名.例如: function functionName(arg0) { //函 ...

  9. css3动画学习的例子来源

    1.这里面有不同的鼠标经过图片效果,图片变大变小,出现文字,向左移动等等 http://dinolatoga.com/demo/webkit-image-hover-effects/ 2.有一篇博客, ...

  10. jsp include 乱码问题的解决

    jsp include 乱码问题的解决 博客分类: Java JSPWeb浏览器IESpring  jsp include 乱码问题的解决 jsp include 的文件有时候会出现乱码,经过测试发现 ...