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. HD2046骨牌铺方格

    骨牌铺方格 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...

  2. unigui MessageDlg方法调用例子

    procedure TfrmEmployee.btnDeleteClick(Sender: TObject);var aBool: Boolean;begin inherited; MessageDl ...

  3. Educational Codeforces Round 5 E. Sum of Remainders (思维题)

    题目链接:http://codeforces.com/problemset/problem/616/E 题意很简单就不说了. 因为n % x = n - n / x * x 所以答案就等于 n * m ...

  4. HDU 5705 Clock (精度控制,暴力)

    题意:给定一个开始时间和一个角度,问你下一个时刻时针和分针形成这个角度是几点. 析:反正数量很小,就可以考虑暴力了,从第一秒开始暴力,直到那个角度即可,不会超时的,数目很少,不过要注意精度. 代码如下 ...

  5. jpa 支持(sql)JDBC标准语句

    =====================dao================================ package com.jb.xs.Dao.impl; import java.uti ...

  6. bmp格式解析

    最近一直在写图像处理的作业,好多啊 bmp格式简介 a.格式组成 1:位图头文件数据结构,它包含BMP图像文件的类型.显示内容等信息: 2:位图信息数据结构,它包含有BMP图像的宽.高.压缩方法,以及 ...

  7. Odoo的Domain (一)

    Odoo 的Domain:多个条件的列表. 条件:(字段名,操作符,值)三元式(列表或者元组) 字段名:当前模型的字段或者是通过点操作符访问的Many2one/Many2Many对象,当是Many2M ...

  8. winform 发布应用程序 提示 “未能注册模块(程序路径)\ieframe.dll”

    程序安装的时候出现未能注册模块(程序路径)\ieframe.dll提示 这种情况的出现,是因为引用的shdocvw.dll,目前发现了一个折中的解决方法,在安装程序里面,可以看到ieframe.dll ...

  9. OC:属性的内部实现原理、dealloc内释放实例变量、便利构造器方法的实现原理、collection的内存管理

    代码: // // main.m #import <Foundation/Foundation.h> #import "Person.h" #import " ...

  10. UVA 12906 Maximum Score 排列组合

    Maximum Score Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/vie ...