POJ 3321 Apple Tree(树状数组)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 27470 | Accepted: 8140 |
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
【分析】题意很简单,这里就不多说了。做法是先dfs编号,找出每个节点所包含的节点编号区间,然后就是树状数组的事了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 2e9
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = 2e5+;
const int M = 4e5+;
int n,m,tot=,cnt=;
int head[N],x[N],y[N],r[N];
int tree[N];
struct EDG{
int to,next;
}edg[M];
inline void addedg(int u,int v){
edg[tot].to=v;edg[tot].next=head[u];head[u]=tot++;
}
void add(int k,int num){
while(k<=n){
tree[k]+=num;
//printf("####%lld\n",tree[k]);
k+=k&(-k);
}
}
int Sum(int k){
int sum=;
while(k>){
sum+=tree[k];
k-=k&(-k);
}
return sum;
}
void dfs(int u){
int v;
x[u]=++cnt;
for(int i=head[u];i!=-;i=edg[i].next){
v=edg[i].to;
if(!x[v])dfs(v);
}
y[u]=cnt;
return;
}
int main() {
met(tree,);met(head,-);met(x,);
int apple[N];
for(int i=;i<N;i++)apple[i]=;
int u,v;
char str[];
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
addedg(u,v);addedg(v,u);
}
dfs();
for(int i=;i<=n;i++)add(i,);
scanf("%d",&m);
while(m--){
scanf("%s",str);
scanf("%d",&u);
if(str[]=='C'){
int l=x[u],rr=y[u];
if(apple[l]){
apple[l]=;
add(l,-);
}else {
apple[l]=;
add(l,);
}
}else {
int ans=Sum(y[u])-Sum(x[u]-);
printf("%d\n",ans);
}
}
return ;
}
POJ 3321 Apple Tree(树状数组)的更多相关文章
- 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 树状数组
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22131 Accepted: 6715 Descr ...
- 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自动下载导入框架包
忽然发现intellij尽然可以自动导入 框架所需的包,而且可以选择jar包版本,瞬间发现Maven,gradle管理jar包还得写配置文件弱爆了. 以Hibernate为例: 1.ProjectSt ...
- CSS3中的2D转换
通过 CSS3 转换,我们能够对元素进行移动.缩放.转动.拉长或拉伸. 转换是使元素改变形状.尺寸和位置的一种效果. 注:Internet Explorer 10.Firefox 以及 Opera 支 ...
- hdu4389 X mod f(x)
链接 这个题因为总和加起来是比较小的9*9 = 81 这样可以保留前面枚举的数对所有的可能出现的和的余数,然后依次向下找. #include <iostream> #include< ...
- hdu3228Island Explorer
链接 给你两条线及两条线上的点,求最小生成树. 可以挨个枚举一条线上的点,三分出另一条线上离他最近的点进行连边. 注意N.M可能为0 debug了1天半,至今不知道原始二分版本错在哪里.. #incl ...
- Decorator
1 意图:动态地给一个对象添加一些额外的职责.就增加功能来说,Decorator模式相比生成子类更灵活. 2 别名:包装器Wrapper 3 动机:将组件嵌入到另一个对象中,由这个对象添加边框.嵌入的 ...
- Linux服务器
/*** cloud_sum_server ***/void cloud_sum(int sockfd) { ssize_t n; char buf[MAXLINE]; , b = ; again: ...
- 计算机上没有找到was服务
控制面板->程序->打开或关闭windows功能,勾选Microsoft .net framework下的两项.
- EasyUI-panel 内嵌页面上的js无法被执行
声明: http://www.jeasyuicn.com/post-49.html 本文引用自GodSon的杰作 http://www.jeasyuicn.com/post-49.html,除修正了个 ...
- 如果重新设计网络,有没有可能合并IP地址跟MAC地址?
前阵子看网络基础相关的书籍,冒过一个疑问,为什么要有MAC地址跟IP地址?两者可否合二为一? 现在的逻辑是这样子:在数据传输过程中,路由器查看这个数据包的IP地址,跟路由表中记录的“IP集合:下一 ...
- 无任何网络提供程序接受指定的网络路径(系统服务里没有workstation服务)
今天同事访问公司服务器时,提示“无任何网络提供程序接受指定的网络路径”,网络ping正常,把防火墙关掉,再次尝试问题如故. 于是上网搜索: 1.服务停止:一般有workstation,server,c ...