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

  1. 3
  2. 1 2
  3. 1 3
  4. 3
  5. Q 1
  6. C 2
  7. Q 1

Sample Output

  1. 3
  2. 2
  3.  
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <string>
  7. #include <vector>
  8. #include <stack>
  9. #include <queue>
  10. #include <set>
  11. #include <map>
  12. #include <list>
  13. #include <iomanip>
  14. #include <cstdlib>
  15. #include <sstream>
  16. using namespace std;
  17. typedef long long LL;
  18. const int INF=0x5fffffff;
  19. const double EXP=1e-;
  20. const int MS=;
  21. int cnt;
  22. vector<vector<int> > edge(MS/);
  23. int flag[MS/];
  24. int L[MS],R[MS];
  25. int c[MS];
  26.  
  27. void dfs(int cur) // 用dfs区间划分
  28. {
  29. L[cur]=++cnt;
  30. for(int i=;i<edge[cur].size();i++)
  31. dfs(edge[cur][i]);
  32. R[cur]=++cnt;
  33. }
  34.  
  35. int lowbit(int x)
  36. {
  37. return x&(-x);
  38. }
  39.  
  40. void updata(int x,int d)
  41. {
  42. while(x<=cnt)
  43. {
  44. c[x]+=d;
  45. x+=lowbit(x);
  46. }
  47. }
  48.  
  49. int getsum(int x)
  50. {
  51. int ret=;
  52. while(x>)
  53. {
  54. ret+=c[x];
  55. x-=lowbit(x);
  56. }
  57. return ret;
  58. }
  59.  
  60. int main()
  61. {
  62. int N,M,x,y;
  63. scanf("%d",&N);
  64. for(int i=;i<N-;i++)
  65. {
  66. scanf("%d%d",&x,&y);
  67. edge[x].push_back(y);
  68. }
  69. cnt=;
  70. dfs();
  71. memset(c,,sizeof(c));
  72. for(int i=;i<=N;i++)
  73. {
  74. updata(L[i],);
  75. updata(R[i],);
  76. }
  77. scanf("%d",&M);
  78. char cmd[MS];
  79. for(int i=;i<=N;i++)
  80. flag[i]=;
  81. while(M--)
  82. {
  83. scanf("%s%d",cmd,&x);
  84. if(cmd[]=='Q')
  85. {
  86. int t1=getsum(L[x]-);
  87. int t2=getsum(R[x]);
  88. printf("%d\n",(t2-t1)/);
  89. }
  90. else
  91. {
  92. if(flag[x])
  93. {
  94. updata(L[x],-);
  95. updata(R[x],-);
  96. flag[x]=;
  97. }
  98. else
  99. {
  100. updata(L[x],);
  101. updata(R[x],);
  102. flag[x]=;
  103. }
  104. }
  105. }
  106. return ;
  107. }

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. Dagger2学习资源

    文章 Jack Wharton关于Dagger的幻灯片 代码 用Dagger2改写Jack Wharton的U+2020 我自己写的,包含了dagger2和单元测试 chiuki写的,包含了dagge ...

  2. AJAX小练习,防止以后忘记

    <div id="content"> <input id="btnShow" type="button" value=&q ...

  3. [置顶] 我的设计模式学习笔记------>Java设计模式总概况

    设计模式的概念最早起源于建筑设计大师Alexander的<建筑的永恒方法>一书,尽管Alexander的著作是针对建筑领域的,但是他的观点实际上用用于所有的工程设计领域,其中也包括软件设计 ...

  4. mahout算法源码分析之Itembased Collaborative Filtering(四)共生矩阵乘法

    Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 经过了SimilarityJob的计算共生矩阵后,就可以开始下面一个过程了,这个过程主要是共生矩阵的乘法 ...

  5. 【转】2D动画:view的Matrix

    Matrix,中文里叫矩阵,高等数学里有介绍,在图像处理方面,主要是用于平面的缩放.平移.旋转等操作. 首先介绍一下矩阵运算.加法和减法就不用说了,太简单了,对应位相加就好.图像处理,主要用到的是乘法 ...

  6. C:内存分配、内存中五大区

     1.内存的划分  (从高到低依次是: 栈区 . 堆区 .全局静态区 . 常量区 . 代码区 )栈区是系统自动回收,堆区是我们手动回收  2. 栈区   在函数内部定义的局部变量和数组.都存放在栈区, ...

  7. urlrewritingnet 域名http状态302 问题(转)

    UrlRewritingNet is an Url rewriting tool for ASP .Net and Elmahis a module for logging unhandled err ...

  8. Git版本管理:Windows下Git配置与使用指南

    简要介绍:Git是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理. 一.安装 软件:msysGit-fullinstall-1.8.1.2 打开之后设置安装路径,默认为 ...

  9. hibernate的配置文件

  10. PageValidate 类

    转载:http://www.cnblogs.com/sufei/archive/2010/01/14/1648028.html using System.Text.RegularExpressions ...