hdu 3966 Aragorn's Story(树链剖分+树状数组)
pid=3966" target="_blank" style="">题目链接:hdu 3966 Aragorn's Story
题目大意:给定一个棵树,然后三种操作
- Q x:查询节点x的值
- I x y w:节点x到y这条路径上全部节点的值添加w
- D x y w:节点x到y这条路径上全部节点的值降低w
解题思路:树链剖分,用树状数组维护每一个节点的值。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
#define lowbit(x) ((x)&(-x))
const int maxn = 50000;
int N, M, Q, val[maxn+5], fenw[maxn+5];
vector<int> g[maxn+5];
inline void add (int x, int v) {
while (x <= N) {
fenw[x] += v;
x += lowbit(x);
}
}
inline void add (int l, int r, int v) {
add(l, v);
add(r+1, -v);
}
inline int query (int x) {
int ret = 0;
while (x) {
ret += fenw[x];
x -= lowbit(x);
}
return ret;
}
int id, far[maxn+5], son[maxn+5], dep[maxn+5], cnt[maxn+5], top[maxn+5], idx[maxn+5];
void dfs_fir(int u, int pre, int d) {
dep[u] = d;
cnt[u] = 1;
son[u] = 0;
far[u] = pre;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v == pre)
continue;
dfs_fir(v, u, d + 1);
cnt[u] += cnt[v];
if (cnt[son[u]] < cnt[v])
son[u] = v;
}
}
void dfs_sec(int u, int rot) {
idx[u] = id++;
top[u] = rot;
if (son[u])
dfs_sec(son[u], rot);
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v == son[u] || v == far[u])
continue;
dfs_sec(v, v);
}
}
void change (int u, int v, int c) {
int p = top[u], q = top[v];
while (p != q) {
if (dep[p] < dep[q]) {
swap(p, q);
swap(u, v);
}
add(idx[p], idx[u], c);
u = far[p];
p = top[u];
}
/*
if (dep[u] > dep[v]) swap(u, v);
add(idx[u], idx[v], c);
*/
if (dep[u] < dep[v]) swap(u, v);
add(idx[v], idx[u], c);
}
int main () {
while (scanf("%d%d%d", &N, &M, &Q) == 3) {
id = 1;
memset(fenw, 0, sizeof(fenw));
for (int i = 1; i <= N; i++) {
scanf("%d", &val[i]);
g[i].clear();
}
int u, v, c;
for (int i = 0; i < M; i++) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs_fir(1, -1, 0);
dfs_sec(1, 1);
for (int i = 1; i <= N; i++) add(idx[i], idx[i], val[i]);
char op[5];
while (Q--) {
scanf("%s%d", op, &u);
if (op[0] == 'Q')
printf("%d\n", query(idx[u]));
else {
scanf("%d%d", &v, &c);
if (op[0] == 'D') c = -c;
change(u, v, c);
}
}
}
return 0;
}
hdu 3966 Aragorn's Story(树链剖分+树状数组)的更多相关文章
- 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结点最短路径上所有节 ...
- HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树
HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...
- HDU 3966 Aragorn's Story (树链剖分+树状数组)
Aragorn's Story Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3966 /// 树链剖分+树状数组
题意: http://acm.hdu.edu.cn/showproblem.php?pid=3966 给一棵树,并给定各个点权的值,然后有3种操作: I x y z : 把x到y的路径上的所有点权值加 ...
- HDU 5044 (树链剖分+树状数组+点/边改查)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5044 题目大意:修改链上点,修改链上的边.查询所有点,查询所有边. 解题思路: 2014上海网赛的变 ...
- HDU 5293 Train chain Problem - 树链剖分(树状数组) + 线段树+ 树型dp
传送门 题目大意: 一颗n个点的树,给出m条链,第i条链的权值是\(w_i\),可以选择若干条不相交的链,求最大权值和. 题目分析: 树型dp: dp[u][0]表示不经过u节点,其子树的最优值,dp ...
- bzoj1146整体二分+树链剖分+树状数组
其实也没啥好说的 用树状数组可以O(logn)的查询 套一层整体二分就可以做到O(nlngn) 最后用树链剖分让序列上树 #include<cstdio> #include<cstr ...
随机推荐
- day6_python学习笔记_chapter8_条件,循环
1. if elif else 2. 条件表达式:三元操作符: smaller = x if x < y else y == if x < y : smaller =x else : ...
- ListView 条目加载上滑下滑首尾缩放动画实现
要实现这个效果,只需要再适配器getView之前,给每个条目的view设置相应的动画即可. 首先需要2个动画的xml文件. 在res下新建anim文件夹:(res/anim) 第一个动画xml文件: ...
- JavaScriptの例
Dateのオブジェクト: <html> <head> <title>Date Object Example</title> </head> ...
- ASPから広がり
ASP是动态服务器页面(Active Server Page)外语缩写.[1]是微软公司开发的代替CGI脚本程序的一种应用,它可以与数据库和其它程序进行交互,是一种简单.方便的编程工具.ASP的网页文 ...
- hdu 3529 Bomberman - Just Search! 重复覆盖
题目链接 依然是重复覆盖的模板.... #include<bits/stdc++.h> using namespace std; #define pb(x) push_back(x) #d ...
- JS表格排序
var employees = [] employees[0] = { name: "George", age: 32, retiredate: "March 12, 2 ...
- JAVA并发,CountDownLatch使用
该文章转自:http://www.itzhai.com/the-introduction-and-use-of-a-countdownlatch.html CountDownLatch 1.类介绍 一 ...
- node 上传文件 路径 重命名等问题
最近在学习node,想做一个简单的网站.首先想到的是上传文件的功能,查了下,发现有一个formidable模块,操作方便,便拿来尝试了一下,结果很纠结. 下载安装的就不用说了,用npm即可.说一下,自 ...
- Select XML Nodes by Name [C#]
原文 http://www.csharp-examples.net/xml-nodes-by-name/ To find nodes in an XML file you can use XPath ...
- tp中phpexcel导出实例
public function phpexcel(){ //测试$this->display("User:xx");//跨模块分配页面User模块xx.html // xx\ ...