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这条链上的权值. ...
随机推荐
- android电池(四):电池 电量计(MAX17040)驱动分析篇【转】
本文转载自:http://blog.csdn.net/xubin341719/article/details/8969369 电池电量计,库仑计,用max17040这颗电量IC去计量电池电量,这种方法 ...
- POJ 2230 Watchcow 【欧拉路】
Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6336 Accepted: 2743 Specia ...
- Java -- 容器使用 Set, List, Map, Queue, Collections
1. ArrayList ArrayList<String> c = new ArrayList<String>(); c.add("hello"); c. ...
- Jquery实现动态导航栏和轮播导航栏
动态导航栏和轮播导航栏的实现思想: 利用jquery技术的append()方法和bind()方法实现li标签的添加和点击事件绑定,在利用$getJSON(url,data,function)请求方法实 ...
- Oracle视图的使用
--视图的语法 create [ or replace ] [NO Force | Force] View schema.view_name--视图名称 [(alias,...)inline_cons ...
- jquery带下拉菜单和焦点图
jQuery,下拉菜单,二级菜单,索引按钮,焦点图代码,jquery带下拉菜单和焦点图是一款顶部通栏带二级下拉菜单和banner导航菜单代码. JQuery特效代码来源:http://www.huiy ...
- 简单使用c3p0连接池
首先,c3p0是一个连接池插件 需要jar包: 使用手动配置: /** * 手动配置使用c3p0 * @throws PropertyVetoException * @throws SQLExcept ...
- C++中的前置和后置++
在C++中进行操作符重载的时候,前置++返回的是一个引用. 这就设计到了对于基本变量进行前置操作时候的理解了. 例如,对于int类型变量,在进行前置++的时候,是会将i进行加1,然后返回i的引用.而i ...
- Java 集合常用特点
1:集合:1) Collection(单列集合) --------------------------------------------------------------------- ...
- VC用MCI播放mp3等音乐文件
VC播放mp3等音乐文件,可以使用MCI.MCI ( Media Control Interface ) ,即媒体控制接口,向基于Windows操作系统的应用程序提供了高层次的控制媒体设备接口的能力. ...