树状数组。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
#define N 100010 int vis[N],have[N];
int low[N],high[N];
int c[N];
vector< vector<int> > edge(N);
int n,now; int lowbit(int x)
{
return x&(-x);
} void init()
{
int i;
for(i=;i<N;i++)
{
have[i] = ;
c[i] = lowbit(i);
}
memset(vis,,sizeof(vis));
now = ;
return;
} void modify(int x)
{
int val;
if(have[x])
{
have[x] = ;
val = -;
}
else
{
have[x] = ;
val = ;
}
while(x<=n)
{
c[x] += val;
x += lowbit(x);
}
} void dfs(int v)
{
vis[v] = ;
low[v] = now;
for(int i=;i<edge[v].size();i++)
{
if(!vis[edge[v][i]])
{
dfs(edge[v][i]);
}
}
high[v] = now;
now++;
} int sum(int x)
{
int res = ;
while(x>)
{
res += c[x];
x -= lowbit(x);
}
return res;
} int main()
{
int i,a,b,q,v;
char ss[];
init();
scanf("%d",&n);
for(i=;i<=n-;i++)
{
scanf("%d%d",&a,&b);
edge[a].push_back(b);
edge[b].push_back(a);
}
dfs();
scanf("%d",&q);
while(q--)
{
scanf("%s",ss);
if(ss[] == 'Q')
{
scanf("%d",&v);
printf("%d\n",sum(high[v])-sum(low[v]-));
}
else
{
scanf("%d",&v);
modify(high[v]);
}
}
return ;
}

POJ 3321 Apple Tree的更多相关文章

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

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

  2. POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)

    id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...

  3. POJ 3321 Apple Tree 【树状数组+建树】

    题目链接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...

  4. POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25904   Accepted: 7682 Descr ...

  5. poj 3321:Apple Tree(树状数组,提高题)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 5629 Descr ...

  6. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

  7. (简单) POJ 3321 Apple Tree,树链剖分+树状数组。

    Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow ...

  8. #5 DIV2 A POJ 3321 Apple Tree 摘苹果 构建线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25232   Accepted: 7503 Descr ...

  9. POJ 3321 Apple Tree(树状数组)

                                                              Apple Tree Time Limit: 2000MS   Memory Lim ...

  10. POJ 3321 Apple Tree dfs+二叉索引树

    题目:http://poj.org/problem?id=3321 动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建 ...

随机推荐

  1. ssh 客户端远程vi文本文件中文乱码(亲测)

    由于是生产环境,且非笔者控制,为了避免影响系统全局,仅对本session有效 export LANG="zh_CN.UTF-8"export LANG="zh_CN.GB ...

  2. MessageBox的Buttons和三级联动

    一.MessageBox的Buttons MessageBox.Show可以出现有按钮的对话框 例如: DialogResult dr = MessageBox.Show("是否要继续吗?& ...

  3. SQLSERVER数据库表各种同步技术

    1 --SQLSERVER数据库表各种同步技术 减少SQLServer中每次的同步数据量 2 3 --说到数据库,我就不由地想到同步数据,如何尽可能地减少每次的同步数据量,以此来提高同步效率,降低对网 ...

  4. andriod GridView

    <?xml version="1.0" encoding="UTF-8"?> <GridView xmlns:android="ht ...

  5. 错误:升级为xcode8之后无法上网的解决方法

    主要是在info.list中增加以下的节点,因为XCode开始所有的http都转为https来联网了. 添加类型为Dictionary的:NSAppTransportSecurity 再添加节点为:N ...

  6. Windows7下Blend for Visual Studio 2012使用问题

    目前开发的系统里很多控件样式和动画比较复杂,应该是之前同事用Blend做的,这种神器不用太浪费了,自己也准备试试. 系统环境Windows7+Visual Studio 2012 1.Windows7 ...

  7. nim2 取石头youxi

    a先把石头分堆,然后bababa的顺序取石头,只能取其中一堆中的若干颗(不能不取) 这种问题先考虑 先取者的胜态问题 (1,1)先取者必败, 所以(1,x),当x>1时可以转换为(1,1)使后取 ...

  8. 编译hadoop遇到maven timeout

      在编译hadoop的过程中,使用ant jar进行编译时,提示maven版本库连接超时的问题,通过搜索发现,在如下文件的位置中有repo2的版本库地址,这个地址在国内,目前不能正常的访问:   将 ...

  9. CSRF 攻击原理和防御方法

    1. CSRF攻击原理 CSRF(Cross site request forgery),即跨站请求伪造.我们知道XSS是跨站脚本攻击,就是在用户的浏览器中执行攻击者的脚本,来获得其cookie等信息 ...

  10. 你所不知道的SQL Server数据库启动过程(用户数据库加载过程的疑难杂症)

    前言 本篇主要是上一篇文章的补充篇,上一篇我们介绍了SQL Server服务启动过程所遇到的一些问题和解决方法,可点击查看,我们此篇主要介绍的是SQL Server启动过程中关于用户数据库加载的流程, ...