POJ 题目3321 Apple Tree(线段树)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 21566 | Accepted: 6548 |
Description
There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree.
The tree has N forks which are connected by branches. Kaka numbers the forks by 1 toN and the root is always numbered by 1. Apples will grow on the forks and two apple won't grow on the same fork. kaka wants to know how many apples are
there in a sub-tree, for his study of the produce ability of the apple tree.
The trouble is that a new apple may grow on an empty fork some time and kaka may pick an apple from the tree for his dessert. Can you help kaka?
Input
The first line contains an integer N (N ≤ 100,000) , which is the number of the forks in the tree.
The following N - 1 lines each contain two integers u and v, which means forku and fork
v are connected by a branch.
The next line contains an integer M (M ≤ 100,000).
The following M lines each contain a message which is either
"C x" which means the existence of the apple on fork
x has been changed. i.e. if there is an apple on the fork, then Kaka pick it; otherwise a new apple has grown on the empty fork.
or
"Q x" which means an inquiry for the number of apples in the sub-tree above the forkx, including the apple (if exists) on the fork x
Note the tree is full of apples at the beginning
Output
Sample Input
3
1 2
1 3
3
Q 1
C 2
Q 1
Sample Output
3
2
Source
POJ Monthly--2007.08.05, Huang, Jinsong
题目大意给你一棵树,每一个节点開始的时候有一个苹果,下边m个操作,Q a,查询以a和a的子树的总共的苹果数,c b。改动操作,改变b节点,,開始在有变没有,没有变成有
ac代码
#include<stdio.h>
#include<string.h>
struct node
{
int u,v,next;
}edge[100010<<1];
int head[100010],cnt,num[100010],son[100010],cc,vis[100100];
void add(int u,int v)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void dfs(int u)
{
num[u]=++cc;
//son[u]=1;
vis[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!vis[v])
dfs(v);
// son[u]+=son[v];
}
son[u]=cc;
}
struct s
{
int sum,cover;
}node[100010<<2];
void pushdown(int tr,int m)
{
if(node[tr].cover)
{
node[tr<<1].cover=node[tr<<1|1].cover=1;
node[tr<<1].sum=m-(m>>1);
node[tr<<1|1].sum=(m>>1);
node[tr].cover=0;
}
}
void build(int l,int r,int tr)
{
node[tr].cover=1;
node[tr].sum=r-l+1;
if(l==r)
{
return;
}
int mid=(l+r)>>1;
build(l,mid,tr<<1);
build(mid+1,r,tr<<1|1);
}
void update(int pos,int l,int r,int tr)
{
if(l==pos&&r==pos)
{
if(node[tr].cover)
{
node[tr].cover=0;
node[tr].sum=0;
}
else
{
node[tr].cover=1;
node[tr].sum=1;
}
return;
}
pushdown(tr,r-l+1);
int mid=(l+r)>>1;
if(pos<=mid)
update(pos,l,mid,tr<<1);
else
update(pos,mid+1,r,tr<<1|1);
node[tr].sum=node[tr<<1].sum+node[tr<<1|1].sum;
if(node[tr<<1].cover&&node[tr<<1|1].cover)
{
node[tr].cover=1;
}
}
int query(int L,int R,int l,int r,int tr)
{
if(L<=l&&R>=r)
{
return node[tr].sum;
}
int mid=(l+r)>>1;
pushdown(tr,r-l+1);
int ans=0;
if(L<=mid)
ans+=query(L,R,l,mid,tr<<1);
if(R>mid)
ans+=query(L,R,mid+1,r,tr<<1|1);
return ans;
/*if(R<=mid)
return query(L,R,l,mid,tr<<1);
else
if(L>mid)
return query(L,R,mid+1,r,tr<<1|1);
else
return query(L,mid,l,mid,tr<<1)+query(mid+1,R,mid+1,r,tr<<1|1);*/
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
cnt=0;
cc=0;
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
int i;
for(i=0;i<n-1;i++)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
}
dfs(1);
int m;
scanf("%d",&m);
build(1,n,1);
while(m--)
{
char s[2];
scanf("%s",s);
if(s[0]=='Q')
{
int a;
scanf("%d",&a);
int ans=query(num[a],son[a],1,n,1);
printf("%d\n",ans);
}
else
{
int a;
scanf("%d",&a);
update(num[a],1,n,1);
}
}
}
}
POJ 题目3321 Apple Tree(线段树)的更多相关文章
- POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)
id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...
- 【POJ 2486】 Apple Tree(树型dp)
[POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8981 Acce ...
- POJ 3321 Apple Tree 【树状数组+建树】
题目链接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...
- (简单) POJ 3321 Apple Tree,树链剖分+树状数组。
Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow ...
- poj 3321:Apple Tree(树状数组,提高题)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18623 Accepted: 5629 Descr ...
- poj 3321 Apple Tree(一维树状数组)
题目:http://poj.org/problem?id=3321 题意: 苹果树上n个分叉,Q是询问,C是改变状态.... 开始的处理比较难,参考了一下大神的思路,构图成邻接表 并 用DFS编号 白 ...
- POJ 3321 Apple Tree(树状数组)
点我看题目 题意 : 大概是说一颗树有n个分岔,然后给你n-1对关系,标明分岔u和分岔v是有边连着的,然后给你两个指令,让你在Q出现的时候按照要求输出. 思路 :典型的树状数组.但是因为没有弄好数组 ...
- POJ 3321 Apple Tree (DFS + 树状数组)
题意: 一棵苹果树有N个分叉,编号1---N(根的编号为1),每个分叉只能有一颗苹果或者没有苹果. 现在有两种操作: 1.某个分叉上的苹果从有变无或者从无边有. 2.需要统计以某个分叉为根节点时,它的 ...
- POJ 题目3667 Hotel(线段树,区间更新查询,求连续区间)
Hotel Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 13805 Accepted: 5996 Descriptio ...
随机推荐
- angular-resource
上一篇中讲到使用$http同服务器进行通信,但是功能上比较简单,angularjs还提供了另外一个可选的服务$resource,使用它可以非常方便的同支持restful的服务单进行数据交互. 安装 n ...
- 今天修了一个bug,关于debug日志的问题
是别人的代码,很诡异. 就是开了debug日志,没问题. 关了debug日志,就出问题. 开始我以为是debug日志拖慢了速度,所以有一些竞态环境的影响. 后来发现是在debug日志里面有一些side ...
- HDU 2643
(第二类斯特林数*N的阶乘 )的和. #include <iostream> #include <cstdio> #include <algorithm> #def ...
- DB-MySQL:MySQL 语句性能优化
ylbtech-DB-MySQL:MySQL 语句性能优化 1.返回顶部 1. MySQL概述1.数据库设计 3范式2.数据库分表分库---会员系统() 水平分割(分页如何查询)MyChar .垂直3 ...
- 138.安全退出的异常,要用throw 尽量不用exit(0)
#include<iostream> #include<cstdlib> using namespace std; ////非安全退出,结束进程, //C++ 必须释放对象,最 ...
- _itoa进制转换
#define _CRT_SECURE_NO_WARNINGS #include <stdlib.h> #include <stdio.h> void main() { int ...
- ie浏览器下get方式获取数据无效问题
在ie浏览器用get方式获取数据时因为发送得到参数地址都是一样的,所以浏览器会优先从缓存获取数据,而不去服务器请求数据,post由于参数不同所以不会影响. 解决方法: 1. Internet选项-- ...
- vim编辑器常用语法
1)yy (功能描述:复制光标当前一行) y数字y (功能描述:复制一段(从第几行到第几行))2)p (功能描述:箭头移动到目的行粘贴)3)u (功能描述:撤销上一步)4)dd (功能描述:删除光标当 ...
- 关闭WPS屏保
WPS作为国产的Office,可谓是越来越流氓,无缘无故给你弄个屏保,不好好做Office就知道如何圈钱,或许这就是国内IT行业和国外的差距吧. 我用的WPS内部版本是:10.1.0.7311 如何查 ...
- Codeforces 845C. Two TVs 思路:简单贪心算法
题目: 题目原文链接:http://codeforces.com/contest/845/problem/C 题意:现在我们有一个电视清单,有两个电视,电视清单上有每一个节目的开始时间和结束时间. 电 ...