【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 ...
随机推荐
- [No00003C]操作系统Operating Systems进程同步与信号量Processes Synchronization and Semaphore
操作系统Operating Systems进程同步与信号量Processes Synchronization and Semaphore 进程合作:多进程共同完成一个任务 从纸上到实际:生产者− − ...
- excel技巧
1. 使文字自动适配方格大小. 自动调整行高(但是合并后的方格不行)
- poj1273 Drainage Ditches
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 68414 Accepted: 2648 ...
- poj 2528
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 56958 Accepted: 16464 ...
- Android http超时选项的测试
Android通过HttpConnectionParams类为http参数设置提供了两个超时的设置选项,分别是setSoTimeout和setConnectionTimeout.初看一眼Android ...
- Hash链表
<?php /* +------------------------------------------------------------------------------ | dateti ...
- cocoapod
更新代码: sudo gem install -n /usr/local/bin cocoapods --pre ex: The dependency `` is not used in any co ...
- GNU Trove trove4j
GNU Trove (http://trove4j.sourceforge.net/) 是一个Java 集合类库.在某些场景下,Trove集合类库提供了更好的性能,而且内存使用更少.以下是Trove中 ...
- C#输出log信息
在写程序的过程中,有时候我们需要添加一些log信息,这个时候,可以采用下面的方法来实现. public static void WriteLog(string ExtraMsg, Exception ...
- 安装Docker Toolbox后出现的问题
Installing Docker Toolbox on Windows with Hyper-V Installed Installing Docker on Windows is a fairly ...