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] 为从结点 ...
随机推荐
- java各种面试问题
二.Java多线程相关 线程池的原理,为什么要创建线程池?创建线程池的方式: 线程的生命周期,什么时候会出现僵死进程: 说说线程安全问题,什么实现线程安全,如何实现线程安全: 创建线程池有哪几个核心参 ...
- python的沙盒环境--virtualenv
VirtualEnv用于在一台机器上创建多个独立的python运行环境,VirtualEnvWrapper为前者提供了一些便利的命令行上的封装. 使用 VirtualEnv 的理由: 隔离项目之间 ...
- Ideone:在线多语言编程执行器工具
Ideone:在线多语言编程执行器工具此网站提供40种编程语言以上, 能在线直接做编译和执行的动作,该工具是一款简易的编程测试工具,虽然不能替代专业版的工具,但是其功能非常全面. Ideone,一款在 ...
- Kubernetes 深入学习(一) —— 入门和集群安装部署
一.简介 1.Kubernetes 是什么 Kubernetes 是一个全新的基于容器技术的分布式架构解决方案,是 Google 开源的一个容器集群管理系统,Kubernetes 简称 K8S. Ku ...
- 一 SpringMvc概述&入门配置
SpringMVC: 类似Struts2的MVC框架,属于SpringFrameWork的后续产品. 与Struts2的区别: 参数传递: Struts2通过模型驱动,属性设置set方法,值栈.类级 ...
- Burp Suite Pro1.7.36破解版
百度网盘下载(H大会一直更新):链接: https://pan.baidu.com/s/1brjPKM7 密码: 9v4r 爱盘下载:https://down.52pojie.cn/Tools/Net ...
- [蓝桥杯2015决赛]四阶幻方(DFS + 剪枝)
题目描述 把1~16的数字填入4x4的方格中,使得行.列以及两个对角线的和都相等,满足这样的特征时称为:四阶幻方. 四阶幻方可能有很多方案.如果固定左上角为1,请计算一共有多少种方案. 比如: 1 ...
- C语言入门书籍知识点记录
1. 数据在内存中的存储(二进制存储) 内存条:电路的电压有两种状态:0V或者5V,对应的一个元器件有2种状态:0 或者1. 一般情况下我们不一个一个的使用元器件,而是将8个元器件看做一个单位. 一个 ...
- Adroid ViewPage+GridView实现每页6个元素,三页滑动切换
//}//public class MainActivity extends Activity {// private static final float APP_PAGE_SIZE = 16.0f ...
- Vue - @import css 加载第三方css
@import '~@/assets/css/style.css' CSS loader 会把把非根路径的url解释为相对路径, 加~前缀才会解释成模块路径.