【POJ 3321】Apple Tree
有n个节点以1为根节点的树,给你树的边关系u-v,一开始每个节点都有一个苹果,接下来有两种操作,C x改变节点x的苹果状态,Q x查询x为根的树的所有苹果个数。
#include<cstdio>
#define N 100005
using namespace std;
int n,m,id,cnt;
int q[N],st[N],ed[N],head[N];
int rl[N<<],rr[N<<],sum[N<<];
struct edge{
int to,next;
}e[N<<];
void add(int u,int v){
e[++cnt]=(edge){v,head[u]};
head[u]=cnt;
}
void dfs(int x,int fa){
st[x]=++id;
for(int i=head[x];i;i=e[i].next)
if(e[i].to!=fa)
dfs(e[i].to,x);
ed[x]=id;
}
void build(int k,int l,int r){
rl[k]=l;rr[k]=r;
if(l==r){
sum[k]=;
return;
}
int mid=l+r>>;
build(k<<,l,mid);
build(k<<|,mid+,r);
sum[k]=sum[k<<]+sum[k<<|];
}
int query(int k,int a,int b){
int l=rl[k],r=rr[k];
if(a<=l&&r<=b) return sum[k];
if(b<l||a>r)return ;
return query(k<<,a,b)+query(k<<|,a,b);
}
void modify(int k,int x){
int l=rl[k],r=rr[k],mid=(l+r)>>;
if(l==r){
sum[k]^=;
return;
}
if(x<=mid)modify(k<<,x);
else modify(k<<|,x);
sum[k]=sum[k<<]+sum[k<<|];
}
int main(){
scanf("%d",&n);
for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(,);
scanf("%d",&m);
build(,,n);
for(int i=;i<=m;i++){
char op;
scanf(" %c",&op);
int x;
scanf("%d",&x);
if(op=='Q')
printf("%d\n",query(,st[x],ed[x]));
else
modify(,st[x]);
}
return ;
}
树状数组
#include<cstdio>
#define N 100005
using namespace std;
int n,m,id,cnt;
int q[N],st[N],ed[N],head[N];
int cal[N],a[N];
struct edge{
int to,next;
}e[N<<];
void add(int u,int v){
e[++cnt]=(edge){v,head[u]};
head[u]=cnt;
}
void dfs(int x,int fa){
st[x]=++id;
for(int i=head[x];i;i=e[i].next)
if(e[i].to!=fa)
dfs(e[i].to,x);
ed[x]=id;
}
int getsum(int x){
int s=;
for(;x;x-=x&(-x))s+=cal[x];
return s;
}
void update(int v,int x){
for(;x<=n;x+=x&(-x))cal[x]+=v;
}
int main(){
scanf("%d",&n);
for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
dfs(,);
scanf("%d",&m);
for(int i=;i<=n;i++)update(,i);
for(int i=;i<=m;i++){
char op;
scanf(" %c",&op);
int x;
scanf("%d",&x);
if(op=='C'){
update(a[x]?:-,st[x]);
a[x]^=;
}
else
printf("%d\n",getsum(ed[x])-getsum(st[x]-));
}
return ;
}
【POJ 3321】Apple Tree的更多相关文章
- 【POJ 2486】 Apple Tree(树型dp)
[POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8981 Acce ...
- 【POJ 2486】 Apple Tree (树形DP)
Apple Tree Description Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to a ...
- POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...
- 【树形dp】Apple Tree
[poj2486]Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10800 Accepted: 3 ...
- 【codeforces 348B】Apple Tree
[题目链接]:http://codeforces.com/problemset/problem/348/B [题意] 给你一棵树; 叶子节点有权值; 对于非叶子节点: 它的权值是以这个节点为根的子树上 ...
- POJ 3321:Apple Tree 树状数组
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22131 Accepted: 6715 Descr ...
- POJ 3321:Apple Tree(dfs序+树状数组)
题目大意:对树进行m次操作,有两类操作,一种是改变一个点的权值(将0变为1,1变为0),另一种为查询以x为根节点的子树点权值之和,开始时所有点权值为1. 分析: 对树进行dfs,将树变为序列,记录每个 ...
- 【POJ - 2385】Apple Catching(动态规划)
Apple Catching 直接翻译了 Descriptions 有两棵APP树,编号为1,2.每一秒,这两棵APP树中的其中一棵会掉一个APP.每一秒,你可以选择在当前APP树下接APP,或者迅速 ...
- 【POJ 1741】Tree
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11570 Accepted: 3626 Description ...
随机推荐
- offsetLeft和style.left的区别
offsetLeft 获取的是相对于父对象的左边距 left 获取或设置相对于 具有定位属性(position定义为relative)的父对象 的左边距 如果父div的position定义为relat ...
- tomcat 启动参数 Xms, Xmx, XX:MaxNewSize, XX:PermSize, -XX:MaxPermSize, Djava.awt.headless
在 tomcat/bin/catalina.sh 的 第一行#!/bin/sh 下添加 JAVA_OPTS="-server -Xms512m -Xmx1024m -XX:MaxNewSiz ...
- Codevs 1229 数字游戏
1229 数字游戏 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description Lele 最近上课的时候都很无聊,所以他发明了 ...
- 微软职位内部推荐-Service Engineer II for Azure Cloud Network
微软近期Open的职位: Are you interested in helping to drive the direction of a product that defines the clou ...
- [转]终于找到全annotation配置springMVC的方法了(事务不失效)
原文:http://icanfly.iteye.com/blog/778401 icanfly 写道 如果带上事务,那么用annotation方式的事务注解和bean配置,事务会失效,要将servic ...
- 由于 ASP.NET 进程标识对全局程序集缓存没有读权限,因此未能执行请求。错误: 0x80131902
由于 ASP.NET 进程标识对全局程序集缓存没有读权限,因此未能执行请求.错误: 0x80131902 分类: c#2013-06-17 10:22 89人阅读 评论(0) 收藏 举报 ASP.NE ...
- Integer.valueof(null)报错
原文 http://javacat360.iteye.com/blog/2024378 主题 Java 昨天,一同事问我一个问题,估计是他前段日子面试遇到的 问题很简单,String.valueof ...
- Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- codevs2010 求后序遍历
难度等级:白银 2010 求后序遍历 题目描述 Description 输入一棵二叉树的先序和中序遍历序列,输出其后序遍历序列. 输入描述 Input Description 共两行,第一行一个字符串 ...
- [MetaHook] SearchPattern function
By Nagi void *SearchPattern(void *pStartSearch, DWORD dwSearchLen, char *pPattern, DWORD dwPatternLe ...