HDU 3966 Aragorn's Story (简单树链剖分)
题意:给一棵树,并给定各个点权的值,然后有3种操作:
I C1 C2 K: 把C1与C2的路径上的所有点权值加上K
D C1 C2 K:把C1与C2的路径上的所有点权值减去K
Q C:查询节点编号为C的权值
析:就是简单的树链剖分,可以用树状数组来维护,然后就OK了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 50000 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} struct Edge{
int to, next;
};
Edge edges[maxn<<1];
int p[maxn], fp[maxn], son[maxn], top[maxn];
int dp[maxn], fa[maxn], head[maxn], num[maxn];
int pos, cnt;
int sum[maxn]; void init(){
pos = 1; cnt = 0;
memset(sum, 0, sizeof sum);
memset(son, -1, sizeof son);
memset(head, -1, sizeof head);
}
int a[maxn]; void add_edge(int u, int v){
edges[cnt].to = v;
edges[cnt].next = head[u];
head[u] = cnt++;
} void dfs1(int u, int f, int d){
dp[u] = d; fa[u] = f;
num[u] = 1;
for(int i = head[u]; ~i; i = edges[i].next){
int v = edges[i].to;
if(v == f) continue;
dfs1(v, u, d+1);
num[u] += num[v];
if(son[u] == -1 || num[v] > num[son[u]])
son[u] = v;
}
} void dfs2(int u, int sp){
top[u] = sp; p[u] = pos++;
fp[p[u]] = u;
if(son[u] == -1) return ;
dfs2(son[u], sp);
for(int i = head[u]; ~i; i = edges[i].next){
int v = edges[i].to;
if(v != fa[u] && v != son[u]) dfs2(v, v);
}
} int lowbit(int x){ return -x&x; }
void add(int x, int val){
while(x <= n){
sum[x] += val;
x += lowbit(x);
}
} int getSum(int x){
int ans = 0;
while(x){
ans += sum[x];
x -= lowbit(x);
}
return ans;
} void update(int u, int v, int val){
int f1 = top[u], f2 = top[v];
while(f1 != f2){
if(dp[f1] < dp[f2]){
swap(f1, f2);
swap(u, v);
}
add(p[f1], val);
add(p[u]+1, -val);
u = fa[f1];
f1 = top[u];
}
if(dp[u] > dp[v]) swap(u, v);
add(p[u], val);
add(p[v]+1, -val);
} int main(){
int q;
while(scanf("%d %d %d", &n, &m, &q) == 3){
for(int i = 1; i <= n; ++i) scanf("%d", a+i);
init();
for(int i = 0; i < m; ++i){
int u, v;
scanf("%d %d", &u, &v);
add_edge(u, v);
add_edge(v, u);
}
dfs1(1, 0, 0);
dfs2(1, 1);
for(int i = 1; i <= n; ++i){
add(p[i], a[i]);
add(p[i]+1, -a[i]);
}
int u, v, x;
while(q--){
char s[5];
scanf("%s", s);
if(s[0] == 'Q'){
scanf("%d", &x);
printf("%d\n", getSum(p[x]));
continue;
}
scanf("%d %d %d", &u, &v, &x);
if(s[0] == 'D') x = -x;
update(u, v, x);
}
}
return 0;
}
HDU 3966 Aragorn's Story (简单树链剖分)的更多相关文章
- hdu 3966 Aragorn's Story(树链剖分+树状数组)
pid=3966" target="_blank" style="">题目链接:hdu 3966 Aragorn's Story 题目大意:给定 ...
- HDU - 3966 Aragorn's Story(树链剖分入门+线段树)
HDU - 3966 Aragorn's Story Time Limit: 3000MS Memory Limit: 32768KB 64bit IO Format: %I64d & ...
- hdu 3966 Aragorn's Story(树链剖分+树状数组/线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: 给出一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路 ...
- hdu 3966 Aragorn's Story(树链剖分+区间修改+单点查询)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:给一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路径上 ...
- 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(树链剖分)题解
题意:给一棵树,要求你对一个路径上的值进行加减,查询某个点的值 思路:重链剖分. 由于分了轻重儿子,我每次到重儿子的top只要O(1),经过的轻儿子最多logn条,那么我每次往上跳最多跳logn次. ...
- HDU 3966 & POJ 3237 & HYSBZ 2243 树链剖分
树链剖分是一个很固定的套路 一般用来解决树上两点之间的路径更改与查询 思想是将一棵树分成不想交的几条链 并且由于dfs的顺序性 给每条链上的点或边标的号必定是连着的 那么每两个点之间的路径都可以拆成几 ...
- 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(树链剖分)
http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:有n个点n-1条边,每个点有一个权值,有两种操作:询问一个点上权值是多少和修改u到v这条链上的权值. ...
随机推荐
- Render树、RenderObject与RenderLayer
Chapter: 呈现树的构建 1. 呈现树与CSS盒子模型千丝万缕的关系 2. 呈现树与DOM树的关系 3. 浏览器构建呈现树的流程 4. Firefox的规则树和样式上下文树 5. 规则树是如何解 ...
- sqoop job 增量导入
使用sqoop job做增量导入 在执行导入模式为 incremental 的sqoop job 时,sqoop会获取上次导入操作的 –check-column的value值,也就是说使用sqoop ...
- JS获取内联样式
JS获取内联样式 //获取内联样式 function getCss(obj,attr){//obj:对象,name:style属性 if(obj.currentStyle) { return obj. ...
- wiredtiger存储引擎介绍——本质就是LSM,当然里面也可以包含btree和列存储
见:http://www.slideshare.net/profyclub_ru/4-understanding-and-tuning-wired-tiger-the-new-high-perform ...
- Django 文件下载功能
def file_download(request): con= MySQLdb.connect(host='192.168.xxx.xxx',user='root',passwd='xxxx',db ...
- 【遍历二叉树】12往二叉树中添加层次链表的信息【Populating Next Right Pointers in Each Node II】
本质上是二叉树的层次遍历,遍历层次的过程当中把next指针加上去. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...
- [原]NYOJ-子串和44
大学生程序代写 /*子串和 时间限制:5000 ms | 内存限制:65535 KB 难度:3 描述 给定一整型数列{a1,a2...,an},找出连续非空子串{ax,ax+1,...,ay},使 ...
- ffmpeg 翻译文档
ffmpeg 翻译文档 (参考源文件ffmpeg-all 包含重要组件) 目录: 1 命令语法 2 描概览 3 详细说明 4 流的选择(指定) 5 选项 技提示(原版已废弃) 6 例子 7 语法 8 ...
- Agc017_E Jigsaw
传送门 题目大意 有$n$块拼图,每一块都由左中右三个部分组成,每块拼图中间部分是高为$H$的长方形,对于第$i$块品推左侧是高为$A_i$距离底部为$C_i$的长方体,右侧是高位$B_i$距底部为$ ...
- Unity中的ShaderToys——将大神们写的shader搬到unity中来吧
http://lib.csdn.net/article/unity3d/38699 这篇文章翻译自国外的一篇文章(这里是原文链接),正在使用unity的你是否在shader toy上发现很多牛逼哄哄的 ...