题干:

There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree.

The tree has N forks which are connected by branches. Kaka numbers the forks by 1 to N and the root is always numbered by 1. Apples will grow on the forks and two apple won't grow on the same fork. kaka wants to know how many apples are there in a sub-tree, for his study of the produce ability of the apple tree.

The trouble is that a new apple may grow on an empty fork some time and kaka may pick an apple from the tree for his dessert. Can you help kaka?

Input

The first line contains an integer N (N ≤ 100,000) , which is the number of the forks in the tree.
The following N - 1 lines each contain two integers u and v, which means fork u and fork v are connected by a branch.
The next line contains an integer M (M ≤ 100,000).
The following M lines each contain a message which is either
"x" which means the existence of the apple on fork x has been changed. i.e. if there is an apple on the fork, then Kaka pick it; otherwise a new apple has grown on the empty fork.
or
"x" which means an inquiry for the number of apples in the sub-tree above the fork x, including the apple (if exists) on the fork x
Note the tree is full of apples at the beginning

Output

For every inquiry, output the correspond answer per line.

Sample Input

3
1 2
1 3
3
Q 1
C 2
Q 1

Sample Output

3
2

题目大意:

给出一个苹果树,每个节点一开始都有苹果

C X,如果X点有苹果,则拿掉,如果没有,则新长出一个(即:可以用异或处理)

Q X,查询X点与它的所有后代分支一共有几个苹果

解题报告:

dfs序,然后用线段树单点更新+区间查询一下就可以了。(dfs序是专门用来处理一棵树的子树查询的,需要用线段树或树状数组维护查询)

AC代码:(用输入外挂,用时少了200ms左右,不知道rank1的63ms是怎么做到的)

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAX = 100000 + 5;
int n,m;
int cnt,id;
int head[MAX*10];
int q[MAX*4],in[MAX*4],out[MAX*4];
struct Edge {int fr;int to;int ne;
} e[MAX * 4];//因为可能双向边?
struct TREE {int l,r;int val;
} tree[MAX * 8];
void add(int u,int v) {e[++cnt].to = v;e[cnt].fr = u;e[cnt].ne = head[u];head[u] = cnt;
}
void pushup(int cur) {tree[cur].val = tree[cur*2].val + tree[cur*2+1].val;
}
void build(int l,int r,int cur) {tree[cur].l = l;tree[cur].r = r;if(l == r) {tree[cur].val = 1;return ;//又忘写return了。。。 }int m = (l+r)/2;//刚开始没写build递归。。。 build(l,m,cur*2);build(m+1,r,cur*2+1);pushup(cur);
}
void dfs(int cur,int root) //cur为当前点,root为根节点
{q[++id] =cur;in[cur] = id;for(int i = head[cur]; i!=-1;i = e[i].ne) {dfs(e[i].to,cur);}out[cur] = id;
}
void update(int tar,int cur) {if(tree[cur].l == tree[cur].r) {tree[cur].val ^= 1;return ; }if(tar <= tree[cur*2].r) update(tar,cur*2);//刚开始写成tree[cur*2].l了else update(tar,cur*2+1);pushup(cur);
}
int query(int pl,int pr,int cur) {if(pl <= tree[cur].l && pr >= tree[cur].r) {return tree[cur].val;}int res = 0;if(pl <= tree[cur*2].r) res += query(pl,pr,cur*2);if(pr >= tree[cur*2+1].l) res += query(pl,pr,cur*2+1);return res;
}
int read()
{int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;
}
int main()
{cin>>n;char op[10];int x;cnt = id = 0;memset(head,-1,sizeof head);int u,v;for(int i = 1; i<n; i++) {//scanf("%d%d",&u,&v);u = read();v = read();add(u,v);}dfs(1,0);//这个0 是没用的 build(1,n,1);
//  printf("**********\n");
//  for(int i = 1; i<=5; i++) {
//      printf("%d|  ",tree[i].val);
//  }
//  printf("\n****************\n");scanf("%d",&m);while(m--) {scanf("%s",op);if(op[0] == 'C') {//scanf("%d",&x);x = read();update(in[x],1);}else {//scanf("%d",&x);x = read();int ans = query(in[x],out[x],1);printf("%d\n",ans);}} return 0 ;} 

总结:

小错误还是很多啊。。。

【POJ - 3321】 Apple Tree(dfs序 + 线段树维护 或 dfs序 + 树状数组维护)相关推荐

  1. 【线段树_DFS序】POJ 3321 Apple Tree

    POJ 3321 Apple Tree 题意:给出一个N个结点的无根树.但是要将1看作根,也相当于是一个有根树.最初每个结点都有一个苹果.有两种操作,Q x: 以x为根的子树上有多少个苹果:C x: ...

  2. POJ 3321 Apple Tree 题解

    POJ 3321 Apple Tree 题解 POJ 3321 题目 There is an apple tree outside of kaka's house. Every autumn, a l ...

  3. poj 3321 Apple Tree(dfs序+树状数组求和模型)

    题目链接:http://poj.org/problem?id=3321 解题思路: 先dfs求出序列,将子树转化到dfs序列的区间内,接下来就是简单的树状数组求和模型了.水题. #include< ...

  4. poj 3321 Apple Tree 树状数组

    http://poj.org/problem?id=3321 一棵树,开始时每个结点都有一个苹果,输入C x表示更新x结点,若x结点有苹果,把该结点苹果摘掉,若该节点无苹果,在该节点上增加一个新的苹果 ...

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

    题目 There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tr ...

  6. poj 3321 Apple Tree

    树状数组 题意:一个树,以树枝连接两个点的形式给出,固定以1为整棵树的根.苹果长在树的节点上,节点上只可能0或1个苹果,一开始每个节点都有1个苹果 有两种操作,C表示更改某个节点的苹果数,0变1,1变 ...

  7. poj 3321 Apple Trie

    /*poj 3321 Apple Trie这道题的关键是如何将一个树建成一个一维数组利用树状数组来解题!可以利用dfs()来搞定,我们在对一个节点深搜后,所经过的节点的数目就是该节点的子树的数目所以我 ...

  8. BZOJ1103 大都市 DFS序 树状数组维护差分数组

    BZOJ1103 大都市 问题描述 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了. 不过,她经常回忆起以前在乡间漫步的情景.昔日,乡下有依次 ...

  9. 树状数组维护区间和的模型及其拓广的简单总结

    by wyl8899 树状数组的基本知识已经被讲到烂了,我就不多说了,下面直接给出基本操作的代码. 假定原数组为a[1..n],树状数组b[1..n],考虑灵活性的需要,代码使用int *a传数组. ...

最新文章

  1. 大神开发arXiv全新H5版,一步告别公式排版错误,手机也能轻松看文献
  2. 美团某程序员爆料:绩效背c的都要签pip!网友:pip就是变相劝退!
  3. ubuntu 12.04 clang 3.4 安装
  4. AI与医学:AI预测结合医学案例应用——当基因编辑转角遇到AI
  5. android 电话 状态栏,Android透明式状态栏、导航栏实现
  6. 【Blog】Start My Journey In Cnblogs!
  7. ISACA:网络安全人员短缺仍是老大难问题
  8. 《流量的秘密 Google Analytics网站分析与商业实战》一2.2 版本选择的标准
  9. vb 源代码格式化工具,Visual Basic程序源代码格式化工具
  10. 穿越机F4飞控F405代码pcb文件,原理图
  11. 金蝶K3 数据表知识整理(不断完善)
  12. 姓名国别分类代码:PyTorch深度学习实践 - Lecture_13_RNN Classifier
  13. 开宗明义—UEFI介绍 (一)
  14. Win10 时间与Internet时间同步超时
  15. 大学计算机教程内容,大学计算机基础教程(教程).ppt
  16. ssci源刊里有开源期刊吗_2020年SCI期刊影响因子重磅发布!你投过的期刊涨了吗?...
  17. 【Lesson 1】 和弦 Chord
  18. Word里表格跨页时自动断开,表格后留有空白部分,未布满整页,如何操作让表格上下页均匀布满?
  19. 基于srs流媒体服务器搭建gb28181视频平台的微服务系统架构
  20. 如何向centos服务器传文件,【实用】如何快速实现Windows和CentOS文件互传

热门文章

  1. 【数据结构与算法】平衡二叉树、红黑树
  2. java interfaceof,java interface教程
  3. 使用 IPsec 与组策略隔离服务器和域-第 7 章 IPsec 疑难解答
  4. 获取弹出的窗口_Win7系统如何获取设置everyone权限的问题
  5. php 显示当前年月日时分秒,php 获取当前前后年、月、星期、日、时分秒的时间...
  6. linux命令帮助怎么看,Linux命令帮助
  7. android ui状态栏高度,Android--状态栏高度,导航栏高度,Window高度,DecorView高度,heightPixels...
  8. java 6大原则_java 6大设计原则 一:观察者模式
  9. 【Modern OpenGL】前言
  10. 非阻IO与EWOULDBLOCK EAGAIN