Apple Tree(需要预处理的树状数组)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 20335 | Accepted: 6182 |
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cstdlib>
#include <sstream>
using namespace std;
typedef long long LL;
const int INF=0x5fffffff;
const double EXP=1e-;
const int MS=;
int cnt;
vector<vector<int> > edge(MS/);
int flag[MS/];
int L[MS],R[MS];
int c[MS]; void dfs(int cur) // 用dfs区间划分
{
L[cur]=++cnt;
for(int i=;i<edge[cur].size();i++)
dfs(edge[cur][i]);
R[cur]=++cnt;
} int lowbit(int x)
{
return x&(-x);
} void updata(int x,int d)
{
while(x<=cnt)
{
c[x]+=d;
x+=lowbit(x);
}
} int getsum(int x)
{
int ret=;
while(x>)
{
ret+=c[x];
x-=lowbit(x);
}
return ret;
} int main()
{
int N,M,x,y;
scanf("%d",&N);
for(int i=;i<N-;i++)
{
scanf("%d%d",&x,&y);
edge[x].push_back(y);
}
cnt=;
dfs();
memset(c,,sizeof(c));
for(int i=;i<=N;i++)
{
updata(L[i],);
updata(R[i],);
}
scanf("%d",&M);
char cmd[MS];
for(int i=;i<=N;i++)
flag[i]=;
while(M--)
{
scanf("%s%d",cmd,&x);
if(cmd[]=='Q')
{
int t1=getsum(L[x]-);
int t2=getsum(R[x]);
printf("%d\n",(t2-t1)/);
}
else
{
if(flag[x])
{
updata(L[x],-);
updata(R[x],-);
flag[x]=;
}
else
{
updata(L[x],);
updata(R[x],);
flag[x]=;
}
}
}
return ;
}
Apple Tree(需要预处理的树状数组)的更多相关文章
- pku-3321 Apple Tree(dfs序+树状数组)
Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow ...
- POJ 3321:Apple Tree(dfs序+树状数组)
题目大意:对树进行m次操作,有两类操作,一种是改变一个点的权值(将0变为1,1变为0),另一种为查询以x为根节点的子树点权值之和,开始时所有点权值为1. 分析: 对树进行dfs,将树变为序列,记录每个 ...
- POJ 3321 Apple Tree(dfs序树状数组)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=10486 题意:一颗有n个分支的苹果树,根为1,每个分支只有一个苹果,给出n- ...
- poj3321-Apple Tree(DFS序+树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36442 Accepted: 10894 Desc ...
- [bzoj1935][shoi2007]Tree 园丁的烦恼(树状数组+离线)
1935: [Shoi2007]Tree 园丁的烦恼 Time Limit: 15 Sec Memory Limit: 357 MBSubmit: 980 Solved: 450[Submit][ ...
- Codeforces 570D TREE REQUESTS dfs序+树状数组 异或
http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...
- Codeforces 570D TREE REQUESTS dfs序+树状数组
链接 题解链接:点击打开链接 题意: 给定n个点的树.m个询问 以下n-1个数给出每一个点的父节点,1是root 每一个点有一个字母 以下n个小写字母给出每一个点的字母. 以下m行给出询问: 询问形如 ...
- BZOJ 1935 Tree 园丁的烦恼 (树状数组)
题意:中文题. 析:按x排序,然后用树状数组维护 y 即可. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000" ...
- BZOJ1935: [Shoi2007]Tree 园丁的烦恼(树状数组 二维数点)
题意 题目链接 Sol 二维数点板子题 首先把询问拆成四个矩形 然后离散化+树状数组统计就可以了 // luogu-judger-enable-o2 #include<bits/stdc++.h ...
随机推荐
- 错误提示:类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内 .
错误提示:类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内 在做导出数据到EXCEL程序中,出现了错误提示:类型“GridView”的控件“Gr ...
- Javascript函数柯里化(curry)
函数柯里化currying,是函数式编程非常重要的一个标志.它的实现需要满足以下条件,首先就是函数可以作为参数进行传递,然后就是函数可以作为返回值return出去.我们依靠这个特性编写很多优雅酷炫的代 ...
- HD1060Leftmost Digit
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission( ...
- vim之grep
[vim之grep] :vimgrep 用于多文件搜索,如 1):vim[grep] start_stage * 在当前目录下(不包括子目录)搜索 2) :vim[grep] start_sta ...
- PC端 $_SERVER 说明
$_SERVER['PHP_SELF'] #当前正在执行脚本的文件名,与 document root相关. $_SERVER['argv'] #传递给该脚本的参数. $_SERVER['argc'] ...
- ThinkPad X220i 刷白名单BIOS,改装第三方无线网卡
ThinkPad X220i自带的网卡是REALTEK RTL8188CE,这张卡在Mac下目前是无解的.国外网站有该卡liunx.unix内核的驱动,但还是没有高人编译出来. 不等了,这卡没戏.正好 ...
- python用httplib模块发送get和post请求
在python中,模拟http客户端发送get和post请求,主要用httplib模块的功能. 1.python发送GET请求 我在本地建立一个测试环境,test.php的内容就是输出一句话: 1 e ...
- POJ 3616 Milking Time (排序+dp)
题目链接:http://poj.org/problem?id=3616 有头牛产奶n小时(n<=1000000),但必须在m个时间段内取奶,给定每个时间段的起始时间和结束时间以及取奶质量 且两次 ...
- JAVA网站高并发解决方案
一个小型的网站,比如个人网站,可以使用最简单的html静态页面就实现了,配合一些图片达到美化效果,所有的页面均存放在一个目录下,这样的网站对系统架构.性能的要求都很简单,随着互联网业务的不断丰富,网站 ...
- 使用java发送邮件sp自动发送邮件方法
注意:将jar包复制到web-info文件夹下lib: activation.jar mail.jar //发送邮箱 public static String sendEmail(String sen ...