POJ 3321 Apple Tree
树状数组。
代码:
#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的更多相关文章
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)
id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...
- POJ 3321 Apple Tree 【树状数组+建树】
题目链接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...
- POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25904 Accepted: 7682 Descr ...
- poj 3321:Apple Tree(树状数组,提高题)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18623 Accepted: 5629 Descr ...
- poj 3321 Apple Tree dfs序+线段树
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Description There is an apple tree outsid ...
- (简单) POJ 3321 Apple Tree,树链剖分+树状数组。
Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow ...
- #5 DIV2 A POJ 3321 Apple Tree 摘苹果 构建线段树
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25232 Accepted: 7503 Descr ...
- POJ 3321 Apple Tree(树状数组)
Apple Tree Time Limit: 2000MS Memory Lim ...
- POJ 3321 Apple Tree dfs+二叉索引树
题目:http://poj.org/problem?id=3321 动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建 ...
随机推荐
- python pip 升级每个包
pip本身不自带升级所有包的功能, 但可以通过下面的脚本实现. import pip from subprocess import call for dist in pip.get_installed ...
- 利用PBFunc在Powerbuilder中解析Json对象
利用PBFunc工具在Powerbuilder解析json,只需要调用getattribute方法来获取 解析unicode格式的json: n_pbfunc_json lnv_json lnv_js ...
- SQL Server 分组 去除从复列
下面先来看看例子: table表 字段1 字段2 id name 1 a 2 b 3 c 4 c 5 ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q32-Q34)
Question 32You create a custom Web Part.You need to ensure that a custom property is visible in Edit ...
- linux网络流量实时监控工具之iptraf
这个工具还是很强大 linux网络流量实时监控工具之iptraf [我的Linux,让Linux更易用]IPTraf是一个网络监控工具,功能比nload更强大,可以监控所有的流量,IP流量,按协议分的 ...
- C语言指针的长度和类型
本文地址:http://www.cnblogs.com/archimedes/p/point-length-type.html,转载请注明源地址. 如果考虑应用程序的兼容性和可移植性,指针的长度就是一 ...
- iOS NSDate、NSCalendar、NSDateComponents
时间解析(NSDate.NSCalendar.NSDateComponents): 1.使用NSCalendar和NSDateComponents解析日期,直接获取到年月日时分秒.获取到年月日时分秒其 ...
- iOS开发笔记2:单例模式(singleton)
每一个app有且仅有一个UIApplication,类似UIApplication“ [UIApplication sharedApplication]”这种一个类有且仅有唯一实例的设计即单例模式. ...
- Mac OS X上安装 Ruby运行环境
环境 对于新入门的开发者,如何安装 Ruby和Ruby Gems 的运行环境可能会是个问题,本页主要介绍如何用一条靠谱的路子快速安装 Ruby 开发环境.此安装方法同样适用于产品环境! 系统需求 ...
- Java 网络编程----基本概念
网络现在是一个非常普遍的概念. 以下是维基百科上的解释: 网络一词有多种意义,可解作: 网络流也简称为网络(network).一般用于管道系统.交通系统.通讯系统建模. 有时特指计算机网络. 或特指其 ...