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 ...
随机推荐
- 学生成绩管理系统 1.0(Java+MySql)
真难…… 数据库建立不会,中文编码不会,插入数据不会,删除不会…… Java读入数据不会……数据库连接不会…… 你也好意思说自己是学计算机的啊魂淡…… 我会慢慢写2.0,3.0版的……噗…… src/ ...
- 【下载】支持中文的 jspSmartUpload jar 包
http://www.blogjava.net/hijackwust/archive/2007/08/22/138598.html —————————————————————————————————— ...
- Oracle用户的单张表的读写权限控制
在oracle数据库的用户下,一张表需要做读写控制,只能读和写,不能删除和修改.开发人员开始想从用户权限上去实现. 经过一番讨论,判读从权限上去实现该需求是不合适的. 这个用户下很多表,根本不会被一个 ...
- HDU 2196 Computer (树dp)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 给你n个点,n-1条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...
- POJ 3169 Layout (差分约束)
题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少. 析:以前只是听说过个算法,从来没用过,差分约束. 对于第 i 个母牛和第 i+1 个, ...
- C#图解教程读书笔记(第1章 C#和.net框架)
C#中的主要需要记住的基础概念 CLR公共语言运行库 CIL中间语言,所有的代码都会编译成中间语言. CLI公共语言基础结构 C#的优点 C#有自动垃圾回收机制
- IC各元器件封装形式图解
CDIP-----Ceramic Dual In-Line Package CLCC-----Ceramic Leaded Chip Carrier CQFP-----Ceramic Quad Fla ...
- MFC 构建、消亡 顺序 (二)--多文档 (MDI)
MFC 构建.消亡 顺序 (二)--多文档 (MDI) by:http://www.cnblogs.com/vranger/ (一)MDI 生成顺序 (二)打开文档-“Open” (三)新建文档-“N ...
- css3 过度效果之物体向上冒出
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"/> <meta nam ...
- 关于extjs中动态添加TabPanel的tab项并以iframe显示的整理
近来的项目中用到了Extjs 的TabPanel,这也是Extjs最基本,最常用的组件了 网上或者书上的例子里大都是把tab项渲染到一个div中, 这对于在每个Tab页里加载一个页面的情况就不适合了 ...