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这条链上的权值. ...
随机推荐
- if-else 与 switch-case语句
if语句就是起一个判断作用,在c语言中有三种表达形式:1.if(表达式)语句:pl. if(x==y)printf("%d",x); //* "== ...
- day4 内置函数 迭代器&生成器 yield总结 三元运算 闭包
内置函数: 内置函数 # abs()返回一个数字的绝对值.如果给出复数,返回值就是该复数的模. b = -100 print(b) print(abs(b)) # all() 所有为真才为真,只要有一 ...
- Nginx Rewrite语法详解
重写中用到的指令 if (条件) {} 设定条件,再进行重写 set #设置变量 return #返回状态码 return 403; break #跳出rewrite rewrite #重写 I ...
- 剑指offer之 数组中出现次数超过一半的数字
public class Solution { public int MoreThanHalfNum_Solution(int [] array) { if(array==null||array.le ...
- Symbol Table(符号表)
一.定义 符号表是一种存储键值对的数据结构并且支持两种操作:将新的键值对插入符号表中(insert):根据给定的键值查找对应的值(search). 二.API 1.无序符号表 几个设计决策: A.泛型 ...
- php数据结构课程---2、链表(php中 是如何实现单链表的(也就是php中如何实现对象引用的))
php数据结构课程---2.链表(php中 是如何实现单链表的(也就是php中如何实现对象引用的)) 一.总结 一句话总结: php是弱类型语言,变量即可表示数值,也可表示对象:链表节点的数据域的值就 ...
- 1.start
1. react-native init Helloworld // 创建 helloworld 工程 2. 进入 helloworld ->android, 运行 react-navite ...
- Struts2 - 表单的重复提交问题
用户重复提交表单在某些场合将会造成非常严重的后果.例如,在使用信用卡进行在线支付的时候,如果服务器的响应速度太慢,用户有可能会多次点击提交按钮,而这可能导致那张信用卡上的金额被消费了多次.因此,重复提 ...
- 关于对H264码流的PS的封装的相关代码实现
1.写在开始之前: 最近因为新工作要维护别人留下的GB模块代码,先熟悉了流程,然后也试着封装了下ps流,结果也能通过测试正常预览了,当然,其中开发读文档的头疼,预览花屏,卡帧的事情都有遇到,当时慢慢的 ...
- POJ3417Network
Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5311 Accepted: 1523 Descripti ...