poj 3321 单点更新 区间求和
Apple Tree
Time Limit: 2000 MS Memory Limit: 65536 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
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<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 单点更新 区间求和的更多相关文章
- poj 1195 单点更新 区间求和
Mobile phones Time Limit: 5000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Jav ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
- LightOJ 1112 Curious Robin Hood (单点更新+区间求和)
http://lightoj.com/volume_showproblem.php?problem=1112 题目大意: 1 i 将第i个数值输出,并将第i个值清0 2 i v ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和)
HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和) 点我挑战题目 题意分析 根据数据范围和询问次数的规模,应该不难看出是个数据结构题目,题目比较裸.题中包括以下命令: 1.Add(i ...
- hdu 1166线段树 单点更新 区间求和
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu1166(线段树单点更新&区间求和模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...
- hdu 1166 (单点更新+区间求和+裸题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissi ...
随机推荐
- 使用limma、Glimma和edgeR,RNA-seq数据分析易如反掌
使用limma.Glimma和edgeR,RNA-seq数据分析易如反掌 Charity Law1, Monther Alhamdoosh2, Shian Su3, Xueyi Dong3, Luyi ...
- 用VS2010打开VS2012项目
1.修改解决方案文件,即.sln文件: 用记事本打开.sln文件,把其中的 Microsoft Visual Studio Solution File, Format Version 12.00 # ...
- linux安全分析
history 查看历史命令 last | grep -i norco //最后一次登录时间
- so文件相关
2018-08-31 今天尝试了一下编译so文件. 最开始是按照这个博主来操作的https://blog.csdn.net/tianshuai4317618/article/details/79073 ...
- 关于win10系统配置变量时,javac编译不出的原因:没用好百度!
在上班时,用笔记本的win8系统成功配置了java环境,于是想把家里的台式也配置一下,可是配置出来后javac却出不来东东,我很费解,重新下载了2次,还是不行,于是就暂时放弃了.那时是17年12月25 ...
- [vb.net]控制台进度条的示例
Private Sub ConsoleProcessBar() Dim isBreak As Boolean = False Dim colorBack As ConsoleColor = Conso ...
- 数字统计(NOIP2010)
题目链接:数字统计 这题很水. 思路就是:枚举每一个区间内的数,然后对于每一个数,每个位去判断是否为2,就行了. 下面上代码: #include<bits/stdc++.h> using ...
- java.lang.SuppressWarnings的注解简介
简介:java.lang.SuppressWarnings是J2SE 5.0中标准的Annotation之一.可以标注在类.字段.方法.参数.构造方法,以及局部变量上.作用:告诉编译器忽略指定的警告, ...
- javascript捕获页面窗口关闭事件
javascript捕获窗口关闭事件有两种方法 onbeforeunload() ,onUnload() 用法有两种: 1. function window.onbefore ...
- gj13 asyncio并发编程
13.1 事件循环 asyncio 包含各种特定系统实现的模块化事件循环 传输和协议抽象 对TCP.UDP.SSL.子进程.延时调用以及其他的具体支持 模仿futures模块但适用于事件循环使用的Fu ...