描述

Happy Christmas! Kaka likes apple very much, Santa Claus presents an apple tree for kaka. In this evening, a lot of apples will grow in the 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?

输入

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

输出

For every inquiry, output the correspond answer per line.

样例输入

3

1 2

1 3
3
Q 1
C 2
Q 1

样例输出

3
2

题目大意是有一颗苹果树,以1这个节点为根节点 ,节点连接的话数据都是合理的,不会存在1在后面,必定是后一个节点连接前一个,C操作是改变某节点苹果状态,从无到有,从有到无,Q操作是查询该节点包括当前节点与其分支上苹果的数量总和

 1 #include <iostream>
 2 #include <vector>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6
 7 const int MAX=100005;
 8 struct Tree
 9 {
10     int left,right,data;//left为左区间(本身),right为右区间(边界),表示该点的管理范围在[left,right],data显示有无苹果,1有,0无
11 }tree[MAX];
12 int n,m,side=1;
13 vector<int> vec[MAX];
14 int cnt[MAX];//在管理范围内的苹果数
15
16 void DFS(int num)//为每个节点标记管理范围
17 {
18     tree[num].left=side;
19     int len=vec[num].size();
20     for(int i=0;i<len;i++){
21         side++;
22         DFS(vec[num][i]);
23     }
24     tree[num].right=side;
25 }
26
27 int lowbit(int i)//表示求数组下标二进制的非0最低位所表示的值(树状数组原理)
28 {
29     return i&-i;//或i-(i&(i-1))或(i ^ (i-1))
30 }
31
32 void update(int i,int val)//更新苹果数
33 {
34     while(i<=n){
35         cnt[i]+=val;
36         i+=lowbit(i);
37     }
38 }
39
40 int getsum(int i)//求范围内总和
41 {
42     int sum=0;
43     while(i>=1){
44         sum+=cnt[i];
45         i-=lowbit(i);
46     }
47     return sum;
48 }
49
50 int main()
51 {
52     int x,y;
53     char c;
54     memset(cnt,0,sizeof(cnt));
55     scanf("%d",&n);
56     for(int i=1;i<n;i++){
57         scanf("%d %d",&x,&y);
58         vec[x].push_back(y);
59     }
60     DFS(1);//从1开始递归进行标记
61     for(int i=1;i<=n;i++){
62         tree[i].data=1;//初始时都有苹果
63         update(i,1);
64     }
65     scanf("%d",&m);
66     for(int i=1;i<=m;i++){
67         getchar();
68         scanf("%c %d",&c,&x);
69         if(c=='C'){
70             int val=(tree[x].data==1?-1:1);//有苹果时val=-1,摘下,否则val=1,重新生长
71             update(tree[x].left,val);
72             tree[x].data=1-tree[x].data;//与原状态相反
73         }
74         else{
75             printf("%d\n",getsum(tree[x].right)-getsum(tree[x].left-1));//右边界-左边界的前一个=该区间内总数
76         }
77     }
78     return 0;
79 }

转载于:https://www.cnblogs.com/ChangeG1824/p/9493469.html

Apple Tree(树状数组)相关推荐

  1. Apple Tree树状数组、前向星、DFS序(C语言)

    Apple Tree树状数组.前向星.DFS序(C语言) 题目 输入值 第一行包含一个整数Ñ(Ñ ≤100,000),这是树中的叉的数量. 接下来的N -1行分别包含两个整数u和v,这意味着fork ...

  2. poj 3321 Apple Tree 树状数组

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

  3. 树状数组 | 1057

    用哈希,把push的数x作为下标给hashTable(实则不存在,直接用tree树状数组记录数据)+1,pop则是以最后一个数x作为下标-1 . 树状数组和其原理不再赘述,需要注意的是最后的二分搜索( ...

  4. 【LCT】【树状数组】Matches Are Not a Child‘s Play(luogu CF1137F)

    正题 luogu CF1137F 题目大意 定义一棵树的产出序列为依次删除权值最小的叶子节点的顺序 进行q此操作: 1.把一个点的权值改为当前树中的最大权值+1 2.查询一个点在删除序列中的位置 3. ...

  5. 【POJ - 3321】 Apple Tree(dfs序 + 线段树维护 或 dfs序 + 树状数组维护)

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

  6. Apple Tree(dfs序+树状数组)

    题目(传送门poj3321) Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 40714 Accepted: ...

  7. Apple Tree(树状数组+dfs序+邻接表数组(链式前向星) )

    链接:http://poj.org/problem?id=3321 Description There is an apple tree outside of kaka's house. Every ...

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

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

  9. 【POJ 3321】Apple Tree(树的dfs序+树状数组)

    传送门 Solution: 我们只需要采用和树链剖分近似的思想--把整个树的dfs序整理出来,排成线型. 这样一个节点的子树肯定是连续的一段,于是乎就可以用树状数组维护单点修改+区间查询的任务了. # ...

最新文章

  1. c++ 窗体上输出文字_C/C++经典面试题
  2. 获取并编译linux源码,android获取源代码、编译、命令
  3. http staus汇总
  4. 如何在窗体之间通讯的几种方法 ? VB.NET方案
  5. Qt Creator配置编辑器
  6. 积米浏览器如何阻止弹窗
  7. hadoop 实现序列化
  8. java.sql.Date – Java SQL日期
  9. python:只想在opencv中显示红色通道?
  10. H5 FormData 表单数据对象详解 与 Json 对象相互转换
  11. 关于msn 微软关闭MSN聊天信息超级链接功能
  12. MTK(Android N)设置SDCard为默认存储
  13. 光伏发电量和用电量的概率预测研究综述(3)
  14. 技嘉b365m小雕驱动工具_【黑苹果】技嘉B365M小雕+i5 9400F+RX590EFI分享
  15. python:操作文档——TXT篇
  16. rust纯黑_你可能不知道:黑波斯的黑色毛发其实有6种不同的类型
  17. 谁引爆了手机里的电池?
  18. 分析 crash 报告的方法
  19. 2016 DevOps现状调查报告
  20. 教你制作自己的电脑主题包

热门文章

  1. .deb版本cuda安装。
  2. C11新增关键字:_Generic(泛型)
  3. 暑期实践Vue7.17
  4. npm(四):剖析npm包版本管理机制
  5. 华尔街抢Web3商标!汇丰注册元宇宙虚拟信用卡、富达抢NFT市场
  6. 【深入理解TcaplusDB技术】TcaplusDB备份与回档机制介绍
  7. MySQLdecimal对应java
  8. 自定义 remoteView
  9. DOS下为什么不能直接从C盘切换到D盘的工作路径
  10. java socket 设置 ttl_嵌入式 socket编程设置TTL