Apple Tree

Time Limit: 2000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

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<cstdio>
#include<cstring>
using namespace std;
#define MAX 100005 int edge[MAX<<1];//表示第i条边的终点
int next[MAX<<1];//与第i条边同起点的下一条边的位置
int head[MAX<<1];//以i为起点的第一条边的储存位置
int low[MAX],high[MAX],c[MAX];
bool visit[MAX];
int cnt,n; int lowbit(int x)
{
return x&-x;
} void update(int pos,int value) //更新pos的值
{
int x=pos;
while(x<=n)
{
c[x]+=value;
x+=lowbit(x);
}
} int get_sum(int pos)//求1到pos位置的和
{
int x=pos,sum=0;
while(x>0)
{
sum+=c[x];
x-=lowbit(x);
} return sum;
} void insert(int i,int a,int b)//a起点,b终点
{
edge[i]=b;
next[i]=head[a];
head[a]=i;
} void dfs(int x,int pre)
{
low[x]=(++cnt);
for(int i=head[x]; i!=-1; i=next[i])
if(edge[i]!=pre)
dfs(edge[i],x);
high[x]=cnt;
} int main()
{
int m;
int a,b;
char op[5];
for(; ~scanf("%d",&n);)
{
cnt=0;
memset(head,-1,sizeof(head));
memset(next,-1,sizeof(next));
memset(c,0,sizeof(c));
for(int i=1,j=1; i<n; ++i)
{
scanf("%d%d",&a,&b);
insert(j++,a,b);
insert(j++,b,a);
}
dfs(1,-1);//遍历树
//初始化
for(int i=1; i<=n; ++i)
update(i,1);
memset(visit,false,sizeof(visit));
scanf("%d",&m);
for(int i=0; i<m; ++i)
{
scanf("%s%d",op,&a);
if(op[0]=='C')//更新
{
if(visit[a])
{
update(low[a],1);
visit[a]=false;
}
else
{
update(low[a],-1);
visit[a]=true;
}
}
else//查询
printf("%d\n",gets_um(high[a])-get_sum(low[a]-1));
}
}
return 0;
}*/

poj 的 judge error  还不知道哪里错了  明天继续看~~~~~

poj 3321 单点更新 区间求和的更多相关文章

  1. poj 1195 单点更新 区间求和

    Mobile phones Time Limit: 5000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Jav ...

  2. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  3. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  4. LightOJ 1112 Curious Robin Hood (单点更新+区间求和)

    http://lightoj.com/volume_showproblem.php?problem=1112 题目大意: 1 i        将第i个数值输出,并将第i个值清0 2 i v     ...

  5. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  6. HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和)

    HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和) 点我挑战题目 题意分析 根据数据范围和询问次数的规模,应该不难看出是个数据结构题目,题目比较裸.题中包括以下命令: 1.Add(i ...

  7. hdu 1166线段树 单点更新 区间求和

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  8. hdu1166(线段树单点更新&区间求和模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...

  9. hdu 1166 (单点更新+区间求和+裸题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissi ...

随机推荐

  1. VirtualBox中安装Fedora9及其ARM开发环境配置

    因为要学习Tiny4412开发板的嵌入式编程,需要用到Fedora9系统(和手册对应),我就在VirtualBox虚拟机(此虚拟机安装在Ubuntu12.04上)上安装了Fedora9,下面就讲解一下 ...

  2. [Jmeter] 用xsltproc生成html格式的报告

    1.下载xsltproc 下载地址:ftp://ftp.zlatkovic.com/libxml/libxslt-1.1.26.win32.zip 其中包含我们所需要的xsltproc可执行文件:xs ...

  3. 高级设计总监的设计方法论——5W1H需求分析法 KANO模型分析法

    本期开始进入设计方法论的学习,大湿自己也是边学边分享,算是巩固一遍吧: 另外这些理论基本都是交叉结合来应用于工作中,我们学习理论但不要拘泥于理论的框架中,掌握后要灵活运用一点- 这些理论一部分来自于我 ...

  4. Windows 平台 (UWP)应用设计

    Make Your Apps Cooperate with Cross-App Communication :  https://rewards.msdn.microsoft.com/Challeng ...

  5. c++中的log函数

    引入#include<cmath> 以e为底:log(exp(n)) 以10为底:log10(n) 以m为底:log(n)/log(m)

  6. RBAC简介

    1.RBAC 中文名称:基于角色的访问控制 1.1 作用:实现访问控制 1.2 核心:角色 2.英文名称:(Role-Based Access Control) 3.解释:一种思想,根据RBAC思想进 ...

  7. 2018.12.15 spoj1812 Longest Common Substring(后缀自动机)

    传送门 后缀自动机模板题. 题意简述:求两个字串的最长公共子串长度. 对其中一个构建后缀自动机,用另外一个在上面跑即可. 代码: #include<bits/stdc++.h> #defi ...

  8. CAS 单点登录 服务器整合

    概述 现在企业内部的系统越来越多,如果各个应用都有自己的用户系统,那么用户将不得不要记住不同系统的用户名密码,因此独立的用户系统应运而生,各个系统之间通过单点登录的方式,这样内部只需要记住一个用户名和 ...

  9. 计算误差函数的积分--erf(x)

    注意exp(-x^2-y^2) 可以拆成exp(-x^2) * exp(-y^2) 对 dx积分时,另外一个可以当常数提出到积分号外,另外 -b到b的积分看做常数提取到积分号外

  10. 利用cglib包实现Spring中aop的<aop:advisor>功能

    一:前言 还有<aop:before>/<aop:after>/<aop:around>的没有实现,不过根<aop:advisor>是差不多的,就是要额 ...