有n个节点以1为根节点的树,给你树的边关系u-v,一开始每个节点都有一个苹果,接下来有两种操作,C x改变节点x的苹果状态,Q x查询x为根的树的所有苹果个数。

 
求出树的dfs序,st[i]保存i的进入时间戳,ed[i]保存i的退出时间戳,则st[i]到ed[i]就是子树节点的对应时间戳。
每个节点打了两次时间戳,其中ed[i]会等于最后访问的一个子节点的st[i]。
于是用线段树/树状数组的单点修改和区间求和就可以解决。

#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的更多相关文章

  1. 【POJ 2486】 Apple Tree(树型dp)

    [POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8981   Acce ...

  2. 【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 ...

  3. 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 ...

  4. 【树形dp】Apple Tree

    [poj2486]Apple Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10800   Accepted: 3 ...

  5. 【codeforces 348B】Apple Tree

    [题目链接]:http://codeforces.com/problemset/problem/348/B [题意] 给你一棵树; 叶子节点有权值; 对于非叶子节点: 它的权值是以这个节点为根的子树上 ...

  6. POJ 3321:Apple Tree 树状数组

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22131   Accepted: 6715 Descr ...

  7. POJ 3321:Apple Tree(dfs序+树状数组)

    题目大意:对树进行m次操作,有两类操作,一种是改变一个点的权值(将0变为1,1变为0),另一种为查询以x为根节点的子树点权值之和,开始时所有点权值为1. 分析: 对树进行dfs,将树变为序列,记录每个 ...

  8. 【POJ - 2385】Apple Catching(动态规划)

    Apple Catching 直接翻译了 Descriptions 有两棵APP树,编号为1,2.每一秒,这两棵APP树中的其中一棵会掉一个APP.每一秒,你可以选择在当前APP树下接APP,或者迅速 ...

  9. 【POJ 1741】Tree

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11570   Accepted: 3626 Description ...

随机推荐

  1. Android中关于Volley的使用(五)从RequestQueue开始来深入认识Volley

    在前面的几篇文章中,我们学习了如何用Volley去网络加载JSON数据,如何利用ImageRequest和NetworkImageView去网络加载数据,而关于Volley的使用,我们都是从下面一行代 ...

  2. 配置Supervisor开机启动

    配置Supervisor开机启动: 新建一个"supervisord.service"文件 # dservice for systemd (CentOS 7.0+) # by ET ...

  3. iOS:CYLTabBarController【低耦合集成TabBarController】

    导航 与其他自定义TabBarController的区别 集成后的效果 项目结构 使用CYLTabBarController 第一步:使用CocoaPods导入CYLTabBarController ...

  4. php正则表达式治疗结巴

    用正则表达式去解决结巴这个问题可以通过下面进行解决: 解决思路是: 先找到重复的不部分 用str_replace($source,$replace,$str);来进行代理 下面分两种情况,最后将这两种 ...

  5. 未能正确加载包“Microsoft.Data.Entity.Design.Package.MicrosoftDataEntityDesignPackage

    本文出处:http://blog.sina.com.cn/s/blog_6fe3efa301016i64.html vs 2005 ,vs 2008, vs 2010,安装后有时出现这个错误(我的机器 ...

  6. myeclipse 8.5离线安装python插件

    看到网上很多教程扯了一大堆,好麻烦的样子. 1)下载pyhon开发插件,PyDev 2.2.0.zip 我在百度137708820网盘里已分享,链接http://pan.baidu.com/s/1mg ...

  7. pip安装包报错:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat

    pip安装包报错:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat Windows7下pip安装包报错:Microso ...

  8. 前端见微知著工具篇:Grunt实现自动化

    转载说明 本篇文章为转载文章,来源为[前端福利]用grunt搭建自动化的web前端开发环境-完整教程,之所以转载,是因为本文写的太详细了,我很想自己来写,但是发现跳不出这篇文章的圈子,因为写的详尽,所 ...

  9. WebHeaderCollection 类

    https://msdn.microsoft.com/zh-cn/library/system.net.webheadercollection(v=VS.95).aspx /// <summar ...

  10. Xen虚拟化基本原理详解

    标签:虚拟化 xen 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://wangzan18.blog.51cto.com/80210 ...