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] 为从结点 ...
随机推荐
- 从Facebook、苹果到外卖平台,“阴谋论”推动巨头企业不断蜕变
不可否认的是,在互联网向前加速推进的过程中,巨头企业和独角兽扮演着重要角色.它们以多元创意和深厚技术.资金实力,一步步改造着大众的互联网生活.而在此前,人们对巨头企业.独角兽的态度是颇为依赖的.但自从 ...
- Reversing-x64Elf-100----攻防世界
题目来源:攻防世界 环境:win10 软件:pycharm.64位的ida 常规的操作ida打开查看,看到了main函数,
- uniGUI之MainModule(12)
1]必须设置. 一个 user 一个, 在此放数据库控件是各 user 独立 2]常用属性: 应用 MainModule 正确的方法是将连接组件放置在 MainModule 上, 并将数据集放在窗体 ...
- 2020年digitalocean最新优惠码100美元奖励
欧美免备案vps服务器digitalocean我用了四年,创建一台vps速度非常快. 由于中国用户扎堆购买Vultr和Linode线路,导致digitalocean中国用户少,反而更稳定.digita ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:"text-info" 类的文本样式
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 2017 青岛现场赛 Suffix
Consider n given non-empty strings denoted by s1 , s2 , · · · , sn . Now for each of them, you need ...
- 【转载】XShell 连接 VirtualBox CentOS7
1.安装 XShell 网址:http://sw.bos.baidu.com/sw-search-sp/software/07a1d9cec0638/Xshell-5.0.1339.exe 尽量不要安 ...
- cgpwn2-嫖来的wp
本想练习pwn的题目活跃下思维,但是接触后发现完全不懂,gg 然后就多方搜集,弄来了一些工具(IDA pro.pwntool)结果自己还是不会用,又是一番刷视频,结果看完又是一脸懵. 只记得一个快捷键 ...
- Linux-使用之vim出现的问题
参考来源: https://stackoverflow.com/questions/47667119/ycm-error-the-ycmd-server-shut-down-restart-wit-t ...
- JS - 数组转字符串
var arr =['h','e','l','l','o'] var s = arr.join(""); console.log(s);