POJ 3321 Apple Tree DFS序+fenwick
题目大意:有一颗长满苹果的苹果树,有两个操作。
1.询问以一个点为根的子树中有多少个苹果。
2.看看一个点有没有苹果,假设没有苹果。那么那里就立即长出一个苹果(= =!);否则就把那个苹果摘下来。
思路:进行一次深搜,将每一个节点最開始出现的时间和最后出现的时间记在一个数组里,那么这两点之间的点就是它以及它的子树的二倍,然后就用树状数组来维护区间和即可了。
CODE:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 200010
using namespace std; pair<int,int> pos[MAX]; int points,asks;
int head[MAX],total;
int next[MAX << 1],aim[MAX << 1];
int cnt;
bool src[MAX];
int fenwick[MAX]; char c[10]; inline void Add(int x,int y);
void DFS(int x,int last); inline void Fix(int x,int c);
inline int GetSum(int x); int main()
{
cin >> points;
for(int x,y,i = 1;i < points; ++i) {
scanf("%d%d",&x,&y);
Add(x,y),Add(y,x);
}
DFS(1,-1);
memset(src,true,sizeof(src));
for(int i = 1;i <= points; ++i)
Fix(pos[i].first,1),Fix(pos[i].second,1);
cin >> asks;
for(int x,i = 1;i <= asks; ++i) {
scanf("%s%d",c,&x);
if(c[0] == 'Q')
printf("%d\n",(GetSum(pos[x].second) - GetSum(pos[x].first - 1)) >> 1);
else {
if(src[x]) {
Fix(pos[x].first,-1);
Fix(pos[x].second,-1);
src[x] = false;
}
else {
Fix(pos[x].first,1);
Fix(pos[x].second,1);
src[x] = true;
}
}
}
return 0;
} inline void Add(int x,int y)
{
next[++total] = head[x];
aim[total] = y;
head[x] = total;
} void DFS(int x,int last)
{
pos[x].first = ++cnt;
for(int i = head[x];i;i = next[i]) {
if(aim[i] == last) continue;
DFS(aim[i],x);
}
pos[x].second = ++cnt;
} inline void Fix(int x,int c)
{
for(;x <= (points << 1);x += x&-x)
fenwick[x] += c;
} inline int GetSum(int x)
{
int re = 0;
for(;x;x -= x&-x)
re += fenwick[x];
return re;
}
POJ 3321 Apple Tree DFS序+fenwick的更多相关文章
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- poj 3321 Apple Tree dfs序+线段树
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Description There is an apple tree outsid ...
- POJ 3321 Apple Tree DFS序 + 树状数组
多次修改一棵树节点的值,或者询问当前这个节点的子树所有节点权值总和. 首先预处理出DFS序L[i]和R[i] 把问题转化为区间查询总和问题.单点修改,区间查询,树状数组即可. 注意修改的时候也要按照d ...
- POJ 3321 Apple Tree dfs+二叉索引树
题目:http://poj.org/problem?id=3321 动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建 ...
- POJ 3321 Apple Tree (DFS + 树状数组)
题意: 一棵苹果树有N个分叉,编号1---N(根的编号为1),每个分叉只能有一颗苹果或者没有苹果. 现在有两种操作: 1.某个分叉上的苹果从有变无或者从无边有. 2.需要统计以某个分叉为根节点时,它的 ...
- 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 (树状数组+dfs序)
题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...
随机推荐
- Week7(10月24日)
Part I:提问 =========================== 1.数据验证属性的练习. 按要求写出教室和课程的模型类. (1)教室类主键不自动增值,手工输入. (2)教室名字不超过10 ...
- Vedis - An Embeddable Datastore Engine
Vedis - An Embeddable Datastore Engine An Embeddable Datastore Engine Tweet Follo ...
- hdu2243之AC自动机+矩阵乘法
考研路茫茫——单词情结 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- c# 课堂总结7--函数
函数:数据类型-变量类型-运算符号表达式-语句(顺序,分支,循环)-数字 程序里的函数:能完成一个相对独立功能的代码块.数学里的函数:高度抽象.函数四要素:函数名,输入(参数),输出(返回值类型),加 ...
- Android:ViewPager04
目录结构层次: 注意点: 1.MyAdapter.java一定要 extends PagerAdapter 2.MyAdapter要覆写以下几个方法: a.public int getCount(){ ...
- [转]Centos 6.5 优化 一些基础优化和安全设置
关于CentOS服务器的优化下文作为参考. 本文 centos 6.5 优化 的项有18处: 1.centos6.5最小化安装后启动网卡2.ifconfig查询IP进行SSH链接3.更新系统源并且升级 ...
- 架构漫谈:UML中几种类间关系:继承、实现、依赖、关联、聚合、组合的联系与区别
这是一堂关于UML基础知识的补习课:现在我们做项目时间都太紧了,基本上都没有做过真正的class级别的详细设计,更别提使用UML来实现规范建模了:本篇主要就以前自己一直感觉很迷糊的几种class之间的 ...
- handler.postDelayed()和timerTask
public static void scrollToListviewTop(final XListView listView) { listView.smoothScrollToPosition(0 ...
- xmlns:android="http://schemas.android.com/apk/res/android的作用是
xmlns:android="http://schemas.android.com/apk/res/android的作用是 这个是xml的命名空间,有了他,你就可以alt+/作为提示,提示你 ...
- Outlook Express 收发邮件出现"0x800CCC0F"错误代码解决方法
非常多网友在使用Outlook Express收发邮件的过程中,有时能够发送邮件,但出现不能接收信件的问题.每次在收邮件都会在某个地方停止不动,然后报超时,出现错误代码0x800CCC19或0x800 ...