(简单) POJ 3321 Apple Tree,树链剖分+树状数组。
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 to N 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?
题目就是给一棵树,然后动态维护节点子树的值的和。
本来很简单的一个题,只要dfs记录进入时间和出的时间就好,结果我傻逼的用树链剖分来做得,不过也简单,算是个裸题。
不过忘了初始化树状数组WA了,%>_<%。。。
代码如下:
// ━━━━━━神兽出没━━━━━━
// ┏┓ ┏┓
// ┏┛┻━━━━━━━┛┻┓
// ┃ ┃
// ┃ ━ ┃
// ████━████ ┃
// ┃ ┃
// ┃ ┻ ┃
// ┃ ┃
// ┗━┓ ┏━┛
// ┃ ┃
// ┃ ┃
// ┃ ┗━━━┓
// ┃ ┣┓
// ┃ ┏┛
// ┗┓┓┏━━━━━┳┓┏┛
// ┃┫┫ ┃┫┫
// ┗┻┛ ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━ // Author : WhyWhy
// Created Time : 2015年07月17日 星期五 15时35分58秒
// File Name : 3321.cpp #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=; struct Edge
{
int to,next;
}; Edge E[MaxN<<];
int head[MaxN],Ecou; int fa[MaxN],son[MaxN],siz[MaxN],dep[MaxN],top[MaxN],w[MaxN];
int Tcou; int C[MaxN]; void init()
{
Tcou=;
Ecou=;
w[]=;
top[]=; memset(head,-,sizeof(head));
memset(C,,sizeof(C));
} void addEdge(int u,int v)
{
E[Ecou].to=v;
E[Ecou].next=head[u];
head[u]=Ecou++;
} void dfs1(int u,int pre,int d)
{
int v; dep[u]=d;
fa[u]=pre;
siz[u]=;
son[u]=-; for(int i=head[u];i!=-;i=E[i].next)
if(E[i].to!=pre)
{
v=E[i].to;
dfs1(v,u,d+);
siz[u]+=siz[v]; if(son[u]==- || siz[son[u]]<siz[v])
son[u]=v;
}
} void dfs2(int u)
{
if(son[u]==-)
return; top[son[u]]=top[u];
w[son[u]]=++Tcou; dfs2(son[u]); int v; for(int i=head[u];i!=-;i=E[i].next)
if(E[i].to!=son[u] && E[i].to!=fa[u])
{
v=E[i].to;
top[v]=v;
w[v]=++Tcou;
dfs2(v);
}
} int N; inline int lowbit(int x)
{
return x&(-x);
} void add(int x,int d)
{
while(x<=N)
{
C[x]+=d;
x+=lowbit(x);
}
} int sum(int x)
{
int ret=; while(x>)
{
ret+=C[x];
x-=lowbit(x);
} return ret;
} void update(int u,int v,int d)
{
int f1=top[u],f2=top[v]; while(f1!=f2)
{
if(dep[f1]<dep[f2])
{
swap(f1,f2);
swap(u,v);
} add(w[f1],d);
add(w[u]+,-d);
u=fa[f1];
f1=top[u];
} if(dep[u]>dep[v])
swap(u,v); add(w[u],d);
add(w[v]+,-d);
} bool rem[MaxN]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int M;
int a,b;
char s[]; while(~scanf("%d",&N))
{
init();
memset(rem,,sizeof(rem)); for(int i=;i<N;++i)
{
scanf("%d %d",&a,&b);
addEdge(a,b);
addEdge(b,a);
} dfs1(,-,);
dfs2(); scanf("%d",&M); for(int i=;i<=N;++i)
update(,i,); while(M--)
{
scanf("%s %d",s,&a); if(s[]=='Q')
printf("%d\n",sum(w[a]));
else
{
update(,a,rem[a]?:-);
rem[a]=!rem[a];
}
}
} return ;
}
(简单) POJ 3321 Apple Tree,树链剖分+树状数组。的更多相关文章
- 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(后根遍历将树转化成序列,用树状数组维护)
题意:一棵树,有很多分叉,每个分叉上最多有1个苹果. 给出n,接下来n-1行,每行u,v,表示分叉u,v之间有树枝相连.这里数据中u相当于树中的父节点,v相当于子节点. 给出两个操作: 1.C x ...
- POJ 3321 Apple Tree(dfs序树状数组)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=10486 题意:一颗有n个分支的苹果树,根为1,每个分支只有一个苹果,给出n- ...
- POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)
id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...
- hdu 3966 Aragorn's Story(树链剖分+树状数组/线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: 给出一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路 ...
- Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组
Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...
- 洛谷 P3384 【模板】树链剖分-树链剖分(点权)(路径节点更新、路径求和、子树节点更新、子树求和)模板-备注结合一下以前写的题目,懒得写很详细的注释
P3384 [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节 ...
- POJ 3321 Apple Tree 【树状数组+建树】
题目链接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...
随机推荐
- 查找List中的最大最小值
以下实例演示了如何使用 Collections 类的 max() 和 min() 方法来获取List中最大最小值: import java.util.*; public class Main { pu ...
- C++调用C#之C# COM控件
C#做界面真的是比C++方便多了,所以尝试了一下,使用C++做核心功能(例如绘图),然后用C#来做节目(例如对话框),考虑到以后可能不能使用.net,使用DLL做一个隔离层,隔离C++和C#,方便以后 ...
- java 反射的实例
JAVA反射机制是在运行状态中,对于任意一个类,都能够得到这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制 ...
- AutoTile 自动拼接(二) 学习与实践
开始代码前,我们要做点准备工作. 下面 跟着我做. 首先我 扣了一个 图. 这个是 做 水的资源,所以是动态的,我把其余两张也扣了出来. 看起来一样,不是,这样看肯定 看不出所以然,你们先放到u3d中 ...
- Hadoop学习笔记—5.自定义类型处理手机上网日志
转载自http://www.cnblogs.com/edisonchou/p/4288737.html Hadoop学习笔记—5.自定义类型处理手机上网日志 一.测试数据:手机上网日志 1.1 关于这 ...
- World Finals 2003 UVA - 1025 A Spy in the Metro(动态规划)
分析:时间是一个天然的序,这个题目中应该决策的只有时间和车站,使用dp[i][j]表示到达i时间,j车站在地上已经等待的最小时间,决策方式有三种,第一种:等待一秒钟转移到dp[i+1][j]的状态,代 ...
- cocos2d-x 那些常犯的错误
Label::_label; if(_label==NULL){ //初始化_label的代码 } //指针默认值不等于NULL,需要赋初始值Label::_label=NULL; string st ...
- “inno setup打包,win7下安装没有桌面快捷方式,xp下安装正常”
修改桌面的快捷键为选中就行了:Flags: checkablealone;在[Tasks]下面修改代码如下:Name: "desktopicon"; Description: &q ...
- JavaScript基础教程
功能介绍: (1)数据验证 弹出提示的对话框,产生判断的效果. (2)气泡提示窗口 右下角等等一类的窗口 (3)产生新窗口 (4)无边框窗口背景变灰,也是可以节约系统资源的
- PHP使用Redis
首先确保安装了Redis扩展 详细的使用方式 请浏览扩展文档 https://github.com/phpredis/phpredis#connection $obj = new Redis(); $ ...