HDU3966-Aragorn's Story(树链剖分)
第一道树链剖分。
早就想学。。一直懒。。
感觉还是比较简单的。
主要是要套其他数据结构,线段树大概还好,平衡树之类的肯定就跪了。
http://blog.csdn.net/acdreamers/article/details/10591443 这篇博客写的真的超级棒 简单明了
感觉树链剖分的难点就是更新的地方
看懂之后自己写一遍错误也都在那里>_<
树链剖分就是把数分成一个个链,然后首尾相连
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = ; //
struct Edge {
int to, next;
} edge[N*];
int head[N], cntE;
void addedge(int u, int v) {
edge[cntE].to = v; edge[cntE].next = head[u]; head[u] = cntE++;
edge[cntE].to = u; edge[cntE].next = head[v]; head[v] = cntE++;
}
//
int dep[N], sz[N], fa[N], son[N];
void dfs1(int u, int par, int d) {
dep[u] = d; sz[u] = ; fa[u] = par;
for (int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if (v != par) {
dfs1(v, u, d+);
sz[u] += sz[v];
if (son[u] == - || sz[v] > sz[son[u]]) son[u] = v;
}
}
}
int top[N], dfn[N], rk[N], idx;
void dfs2(int u, int rt) {
top[u] = rt; dfn[u] = ++idx; rk[idx] = u;
if (son[u] == -) return;
dfs2(son[u], rt);
for (int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if (v != fa[u] && v != son[u]) dfs2(v, v);
}
}
//
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
int sum[N<<], a[N];
void build(int o, int l, int r) {
if (l == r) { sum[o] = a[rk[l]]; return; }
int mid = (l+r) >> ;
build(lson); build(rson);
sum[o] = ;
} void pushdown(int o) {
if (sum[o]) {
sum[o<<] += sum[o];
sum[o<<|] += sum[o];
sum[o] = ;
}
} int query(int o, int l, int r, int v) {
if (l == r) return sum[o];
pushdown(o);
int mid = (l+r) >> ;
if (v <= mid) return query(lson, v);
return query(rson, v);
} void update(int o, int l, int r, int L, int R, int v) {
if (l >= L && r <= R) { sum[o] += v; return ; }
pushdown(o);
int mid = (l+r) >> ;
if (L <= mid) update(lson, L, R, v);
if (R > mid) update(rson, L, R, v);
} void change(int x, int y, int w, int n) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
update(, , n, dfn[top[x]], dfn[x], w);
x = fa[top[x]];
}
if (dep[x] > dep[y]) swap(x, y);
update(, , n, dfn[x], dfn[y], w);
} void init() {
idx = cntE = ;
memset(head, -, sizeof head);
memset(son, -, sizeof son);
} int main()
{
int n, m, p;
while (~scanf("%d%d%d", &n, &m, &p)) {
init();
int u, v, w;
char op[];
for (int i = ; i <= n; ++i) scanf("%d", &a[i]);
for (int i = ; i <= m; ++i) scanf("%d%d", &u, &v), addedge(u, v); dfs1(, , ); dfs2(, ); build(, , n); while (p--) {
scanf("%s", op);
if (*op == 'Q') {
scanf("%d", &u);
printf("%d\n", query(, , n, dfn[u]));
} else {
scanf("%d%d%d", &u, &v, &w);
if (*op == 'D') w = -w;
change(u, v, w, n);
}
}
}
return ;
}
HDU3966-Aragorn's Story(树链剖分)的更多相关文章
- hdu3966 Aragorn's Story 树链剖分
题目传送门 题目大意: 有n个兵营形成一棵树,给出q次操作,每一次操作可以使两个兵营之间的所有兵营的人数增加或者减少同一个数目,每次查询输出某一个兵营的人数. 思路: 树链剖分模板题,讲一下树链剖分过 ...
- HDU3669 Aragorn's Story 树链剖分 点权
HDU3669 Aragorn's Story 树链剖分 点权 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: n个点的,m条边,每个点都 ...
- HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树
HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...
- Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组
Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...
- HDU 3966 Aragorn's Story(树链剖分)(线段树区间修改)
Aragorn's Story Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Hdu 3966 Aragorn's Story (树链剖分 + 线段树区间更新)
题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2 ...
- HDU 3966 Aragorn's Story 树链剖分+BIT区间修改/单点询问
Aragorn's Story Description Our protagonist is the handsome human prince Aragorn comes from The Lord ...
- hdu 3966 Aragorn's Story 树链剖分 按点
Aragorn's Story Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3966 Aragorn's Story (树链剖分入门题)
树上路径区间更新,单点查询. 线段树和树状数组都可以用于本题的维护. 线段树: #include<cstdio> #include<iostream> #include< ...
- HDU 3966 Aragorn's Story 树链剖分
Link: http://acm.hdu.edu.cn/showproblem.php?pid=3966 这题注意要手动扩栈. 这题我交g++无限RE,即使手动扩栈了,但交C++就过了. #pragm ...
随机推荐
- install Nagios on Unbuntu Unix
Ubuntu Quickstart Up To: ContentsSee Also: Quickstart Installation Guides, Security Considerations I ...
- CodeForces 299A Ksusha and Array
http://codeforces.com/problemset/problem/299/A 题意 :输入n个数,要求找出一个数能让其他所有的数整除,如果没有的话输出-1.有多个的话输出其中一个. 思 ...
- 用java运行Hadoop程序报错:org.apache.hadoop.fs.LocalFileSystem cannot be cast to org.apache.
用java运行Hadoop例程报错:org.apache.hadoop.fs.LocalFileSystem cannot be cast to org.apache.所写代码如下: package ...
- ZooKeeper 安装部署及hello world
ZooKeeper 安装部署及hello world 先给一堆学习文档,方便以后查看官网文档地址大全: OverView(概述)http://zookeeper.apache.org/doc/r3. ...
- Qt xcode wrapper Qios OpenFly
https://github.com/richardmg/QtWrapper https://github.com/richardmg/qios https://github.com/richardm ...
- pku,杨建武:文本挖掘技术
http://webkdd.org/course/ http://www.icst.pku.edu.cn/lcwm/course/WebDataMining/ http://www.icst.pku. ...
- 什么是WebService
举个例子:现在有5个项目,项目彼此独立,甚至都不是同一类语言进行开发的.这5个项目是:百度知道,百度贴吧,百度新闻,百度视频,百度百科.突然有一天,老板说:把这几个系统揉称一个大项目,起名直接叫做百度 ...
- eclipse 点击 open Implementation就退出eclipse
输入法不对.. 切换到纯英文的输入法. 微软自带的那个.. 我电脑上也这样. 现在好了 (安装谷歌输入法貌似存在这个问题)
- unity3d游戏物体跟着鼠标方向移动
效果:当点击鼠标左键时,游戏对象会朝鼠标点击的方向移动,类似魔兽争霸一样. 思路:把鼠标的坐标转化成世界坐标(鼠标默认是屏幕坐标),然后当点击鼠标时,物体将朝着鼠标的世界坐标方向移动. 如果你看到这的 ...
- BZOJ_3172_[TJOI2013]_单词_(AC自动机)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=3172 \(n\)个单词组成一篇文章,求每个单词在文章中出现的次数. 分析 这道题很像BZOJ_ ...