POJ 3321:Apple Tree 树状数组
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 22131 | Accepted: 6715 |
Description
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
"C 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
"Q 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
Sample Input
3
1 2
1 3
3
Q 1
C 2
Q 1
Sample Output
3
2
题意是有一个树,树根始终是1。最开始每一个节点上都上有苹果,然后就开始折腾了。
操作为Q时是询问,该分叉上现在一共有多少个苹果。
操作为C时,如果当前该节点上有苹果,摘掉。如果当前该节点没有苹果,长出一个来。
先dfs出每一个节点的起始时间和结束时间,然后根据起始时间和结束时间使用树状数组求和的思想就能得到结果。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; #define MY_MAX 220000
int C[MY_MAX];
vector<vector<int>> G(MY_MAX/2);
int HasApple[MY_MAX/2];
int sta[MY_MAX];
int en[MY_MAX];
int ncount=0;
int n,m; void dfs(int v)
{
sta[v] = ++ncount;
for(int i=0;i<G[v].size();i++)
{
dfs(G[v][i]);
}
en[v] = ++ncount;
} int lowbit(int x)
{
return x&(-x);
} void add(int x,int val)
{
while(x<=en[1]+1)
{
C[x] = C[x] + val;
x=x+lowbit(x);
}
} int sum(int x)
{
int res=0;
while(x>0)
{
res+=C[x];
x=x-lowbit(x);
}
return res;
}
int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int i,temp1,temp2;
char oper[5]; scanf("%d",&n);
for(i=1;i<=n-1;i++)
{
scanf("%d%d",&temp1,&temp2);
G[temp1].push_back(temp2);
HasApple[i]=1;
}
HasApple[n]=1;
dfs(1);
scanf("%d",&m);
memset(C,0,sizeof(C));
for(i=1;i<=en[1]+1;i++)
{
add(i,1);
}
for(i=1;i<=m;i++)
{
scanf("%s%d",oper,&temp1);
if(oper[0]=='C')
{
if(HasApple[temp1])
{
HasApple[temp1]=0;
add(sta[temp1]+1,-1);
add(en[temp1]+1,-1);
}
else
{
HasApple[temp1]=1;
add(sta[temp1]+1,1);
add(en[temp1]+1,1);
}
}
else if(oper[0]=='Q')
{
printf("%d\n",(sum(en[temp1])-sum(sta[temp1]))/2+HasApple[temp1]);
}
} //system("pause");
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 3321:Apple Tree 树状数组的更多相关文章
- 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节点以及它子树的值之和.初 ...
- POJ 3321 Apple Tree 树状数组+DFS
题意:一棵苹果树有n个结点,编号从1到n,根结点永远是1.该树有n-1条树枝,每条树枝连接两个结点.已知苹果只会结在树的结点处,而且每个结点最多只能结1个苹果.初始时每个结点处都有1个苹果.树的主人接 ...
- POJ 3321 Apple Tree 树状数组 第一题
第一次做树状数组,这个东西还是蛮神奇的,通过一个简单的C数组就可以表示出整个序列的值,并且可以用logN的复杂度进行改值与求和. 这道题目我根本不知道怎么和树状数组扯上的关系,刚开始我想直接按图来遍历 ...
- 3321 Apple Tree 树状数组
LIANJIE:http://poj.org/problem?id=3321 给你一个多叉树,每个叉和叶子节点有一颗苹果.然后给你两个操作,一个是给你C清除某节点上的苹果或者添加(此节点上有苹果则清除 ...
- POJ--3321 Apple Tree(树状数组+dfs(序列))
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Descripti ...
- E - Apple Tree(树状数组+DFS序)
There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. ...
- POJ3321 Apple Tree(树状数组)
先做一次dfs求得每个节点为根的子树在树状数组中编号的起始值和结束值,再树状数组做区间查询 与单点更新. #include<cstdio> #include<iostream> ...
- POJ 2486 Apple Tree [树状DP]
题目:一棵树,每个结点上都有一些苹果,且相邻两个结点间的距离为1.一个人从根节点(编号为1)开始走,一共可以走k步,问最多可以吃多少苹果. 思路:这里给出数组的定义: dp[0][x][j] 为从结点 ...
随机推荐
- Intellij Idea 下包建包,无论怎么建都在同一级,已解决(附图)
1.很多新手,刚开始使用Intellij Idea的时候,项目建包都出现所建的包都在用一级. 2.这是因为,刚开始建项目的时候,Hide Empty Middle Packages是默认勾选的,只要去 ...
- error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 解决方法
在VS2012中生成时出错:error C4430: missing type specifier - int assumed. Note: C++ does not support default- ...
- Codeforces1307B. Cow and Friend
本题的难点是可以在y轴正轴动,但也是突破点,知道x轴都是整数,那么对于任意长度来说,能到达的最短是1,最长是本身长度,那么我们就选择最长的距离,跳到一个点,使这个点为再跳就超过终点,那么就可以用2次跳 ...
- 「luogu2617」Dynamic Rankings
「luogu2617」Dynamic Rankings 传送门 树套树直接上树状数组套主席树,常数很大就是了. 树套树参考代码: /*-------------------------------- ...
- CSP-S 2019 初赛游记
Day 0 上午考了一套毒瘤的数据结构题,考的我心态爆炸SB出题人 晚上考了一套初赛模拟,只考1h,然后我91分,感觉初赛完全没问题? 回寝室后一直在忙活,整理东西什么的,居然将近12点睡? Day ...
- Centos7 nginx的负载均衡概念与配置
一.负载均衡概念 负载均衡(Server Load Balancer)是将访问流量根据转发策略分发到后端多台 ECS 的流量分发控制服务.负载均衡可以通过流量分发扩展应用系统对外的服务能力,通过消除单 ...
- phpcms安装与使用
安装完wamp(或phpstudy)之后,将phpcms的安装文件复制入C:\wamp\www(或C:\phpStudy\PHPTutorial\WWW)文件夹下: 打开phpcms_v9_UTF8\ ...
- pycharm安装报错Non-zero exit co?
pycharm安装第三方库时报错Non-zero exit co? 原因:版本找不到 解决思路: 1.用pip install pandas报错,尝试用python -m pip install -- ...
- Visual Studio Code 格式化ESlint 的方法
既然要格式化ESlint,就不得不先介绍一下什么是ESlint.后面再介绍格式化的方法 1.ESlint ESLint 是在 ECMAScript/JavaScript 代码中识别和报告模式匹配的工具 ...
- detectron2 配置记录
1. RuntimeError: Not compiled with GPU support (ROIAlign_forward at /home/oliver/PycharmProjects/det ...