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

For every inquiry, output the correspond answer per line.

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(需要预处理的树状数组)的更多相关文章

  1. pku-3321 Apple Tree(dfs序+树状数组)

    Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow ...

  2. POJ 3321:Apple Tree(dfs序+树状数组)

    题目大意:对树进行m次操作,有两类操作,一种是改变一个点的权值(将0变为1,1变为0),另一种为查询以x为根节点的子树点权值之和,开始时所有点权值为1. 分析: 对树进行dfs,将树变为序列,记录每个 ...

  3. POJ 3321 Apple Tree(dfs序树状数组)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=10486 题意:一颗有n个分支的苹果树,根为1,每个分支只有一个苹果,给出n- ...

  4. poj3321-Apple Tree(DFS序+树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36442   Accepted: 10894 Desc ...

  5. [bzoj1935][shoi2007]Tree 园丁的烦恼(树状数组+离线)

    1935: [Shoi2007]Tree 园丁的烦恼 Time Limit: 15 Sec  Memory Limit: 357 MBSubmit: 980  Solved: 450[Submit][ ...

  6. Codeforces 570D TREE REQUESTS dfs序+树状数组 异或

    http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...

  7. Codeforces 570D TREE REQUESTS dfs序+树状数组

    链接 题解链接:点击打开链接 题意: 给定n个点的树.m个询问 以下n-1个数给出每一个点的父节点,1是root 每一个点有一个字母 以下n个小写字母给出每一个点的字母. 以下m行给出询问: 询问形如 ...

  8. BZOJ 1935 Tree 园丁的烦恼 (树状数组)

    题意:中文题. 析:按x排序,然后用树状数组维护 y 即可. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000" ...

  9. BZOJ1935: [Shoi2007]Tree 园丁的烦恼(树状数组 二维数点)

    题意 题目链接 Sol 二维数点板子题 首先把询问拆成四个矩形 然后离散化+树状数组统计就可以了 // luogu-judger-enable-o2 #include<bits/stdc++.h ...

随机推荐

  1. Jtemplates 基本语法

    jTemplates是一个基于JQuery的模板引擎插件,功能强大,有了他你就再不用为使用JS绑定数据集时发愁了. 首先送上jTtemplates的官网地址:http://jtemplates.tpy ...

  2. tomcat 粗略笔记

    GlobalNamingResources 存在于server.xml中,定义全局公共数据源,如果host中有大量引用相同的数据源,那么可以都配在这里 <GlobalNamingResource ...

  3. windows下php cli模式,提示出错

    搞一下php cli,一直提示,找不到php_pdo_mssql.dll模块 原来是在php.ini加载了该模块,但ext下又没有该模块 即便下载了一个php_pdo_mssql.dll,但还是会提示 ...

  4. jgroups 入门

    官网地址:http://www.jgroups.org/ 聊天室示例:http://www.jgroups.org/tutorial/html/ch02.html 2.1. JGroups overv ...

  5. 备份Xcode6的配色主题以及代码模板

    ~/Library/Developer/Xcode/UserData/FontAndColorThemes ~/Library/Developer/Xcode/UserData/CodeSnippet ...

  6. jQuery实现等比例缩放大图片让大图片自适应页面布局

    通常我们处理缩略图是使用后台代码(PHP..net.Java等)根据大图片生成一定尺寸的缩略图,来供前台页面调用,当然也有使用前台javascript脚本将加载后的大图强行缩放,变成所谓的缩略图,这种 ...

  7. 服务器资源共享--IIS站点/虚拟目录中访问共享目录(UNC)

    本文重点描述如何使用IIS访问共享资源来架设站点或执行 ASP.Net 等脚本. 通常情况下,拥有多台服务器的朋友在使用IIS建立站点的时候,会遇到如何把多台服务器的资源合并到一起的问题.如何让A服务 ...

  8. C#完成超酷的图像效果 (附demo)

    如果您觉得C#制作的艺术字比较好玩, 但是还觉得没看够,不过瘾,那么我今天就让您一饱眼福, 看看C#如何制作的效果超酷的图像. (注: 我之前曾写过类似的文章, 但没有原理说明, 代码注释不够详细, ...

  9. 可配置多功能门 SN74LVC1G57, 1G58, 1G97, 1G98, 1G99

    Configurable Multiple-Function Gate  SN74LVC1G57 SN74LVC1G58 SN74LVC1G97 SN74LVC1G98 SN74LVC1G99

  10. 计算Excel中的Sheet个数

    $strpath="d:\ee.xlsx"$excel=new-object -comobject excel.application$WorkBook = $excel.Work ...