题意:

给出一棵树,每个顶点上有一个权值。

操作:选择一条路径,并将路径上所有的点的权值同时加或减某个数。

查询:某个点的当前权值

分析:

树链剖分完毕后,就是简单的线段树区间更新。

提交的时候注意要要加一句扩栈的代码,用C++提交。

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; void read(int& x) {
x = ;
char c = ' ';
while(c < '' || c > '') c = getchar();
while('' <= c && c <= '') {
x = x * + c - '';
c = getchar();
}
} const int maxn = + ;
const int maxnode = maxn * ; struct Edge
{
int v, nxt;
Edge() {}
Edge(int v, int nxt) :v(v), nxt(nxt) {}
}; int n, m, q;
int a[maxn]; int ecnt, head[maxn];
Edge edges[maxn * ]; void AddEdge(int u, int v) {
edges[++ecnt] = Edge(v, head[u]);
head[u] = ecnt;
edges[++ecnt] = Edge(u, head[v]);
head[v] = ecnt;
} int dep[maxn], fa[maxn], son[maxn], sz[maxn]; int tot, id[maxn], pos[maxn], top[maxn]; void dfs(int u) {
sz[u] = ; son[u] = -;
for(int i = head[u]; i; i = edges[i].nxt) {
int v = edges[i].v;
if(v == fa[u]) continue;
fa[v] = u;
dep[v] = dep[u] + ;
dfs(v);
sz[u] += sz[v];
if(son[u] == - || sz[v] > sz[son[u]]) son[u] = v;
}
} void dfs2(int u, int tp) {
top[u] = tp;
id[u] = ++tot;
pos[tot] = u;
if(son[u] == -) return;
if(son[u]) dfs2(son[u], tp);
for(int i = head[u]; i; i = edges[i].nxt) {
int v = edges[i].v;
if(v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
} int sumv[maxnode], addv[maxnode];
char cmd[]; void pushup(int o) { sumv[o] = sumv[o<<] + sumv[o<<|]; } void build(int o, int L, int R) {
if(L == R) { sumv[o] = a[pos[L]]; return; }
int M = (L + R) / ;
build(o<<, L, M);
build(o<<|, M+, R);
pushup(o);
} void pushdown(int o, int L, int R) {
if(addv[o]) {
addv[o<<] += addv[o];
addv[o<<|] += addv[o];
int M = (L + R) / ;
sumv[o<<] += (M - L + ) * addv[o];
sumv[o<<|] += (R - M) * addv[o];
addv[o] = ;
}
} void update(int o, int L, int R, int qL, int qR, int v) {
if(qL <= L && R <= qR) {
sumv[o] += (R - L + ) * v;
addv[o] += v;
return;
}
pushdown(o, L, R);
int M = (L + R) / ;
if(qL <= M) update(o<<, L, M, qL, qR, v);
if(qR > M) update(o<<|, M+, R, qL, qR, v);
pushup(o);
} void UPDATE(int u, int v, int val) {
int t1 = top[u], t2 = top[v];
while(t1 != t2) {
if(dep[t1] < dep[t2]) { swap(u, v); swap(t1, t2); }
update(, , n, id[t1], id[u], val);
u = fa[t1]; t1 = top[u];
}
if(dep[u] > dep[v]) swap(u, v);
update(, , n, id[u], id[v], val);
} int query(int o, int L, int R, int p) {
if(L == R) return sumv[o];
pushdown(o, L, R);
int M = (L + R) / ;
if(p <= M) return query(o<<, L, M, p);
else return query(o<<|, M+, R, p);
} int main()
{
while(scanf("%d%d%d", &n, &m, &q) == ) {
for(int i = ; i <= n; i++) read(a[i]); memset(head, , sizeof(head));
ecnt = ;
for(int i = ; i < n; i++) {
int u, v; read(u); read(v);
AddEdge(u, v);
} dfs();
tot = ;
dfs2(, ); memset(addv, , sizeof(addv));
build(, , n); while(q--) {
scanf("%s", cmd);
int u, v, d;
read(u);
if(cmd[] == 'Q') {
printf("%d\n", query(, , n, id[u]));
} else {
read(v); read(d);
if(cmd[] == 'D') d = -d;
UPDATE(u, v, d);
}
}
} return ;
}

代码君

HDU 3966 RE 树链剖分 线段树 Aragorn's Story的更多相关文章

  1. HDU 2460 Network(双连通+树链剖分+线段树)

    HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...

  2. Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组

    Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...

  3. 【BZOJ-2325】道馆之战 树链剖分 + 线段树

    2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1153  Solved: 421[Submit][Statu ...

  4. 【BZOJ2243】[SDOI2011]染色 树链剖分+线段树

    [BZOJ2243][SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的 ...

  5. BZOJ2243 (树链剖分+线段树)

    Problem 染色(BZOJ2243) 题目大意 给定一颗树,每个节点上有一种颜色. 要求支持两种操作: 操作1:将a->b上所有点染成一种颜色. 操作2:询问a->b上的颜色段数量. ...

  6. POJ3237 (树链剖分+线段树)

    Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...

  7. bzoj4034 (树链剖分+线段树)

    Problem T2 (bzoj4034 HAOI2015) 题目大意 给定一颗树,1为根节点,要求支持三种操作. 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子 ...

  8. HDU4897 (树链剖分+线段树)

    Problem Little Devil I (HDU4897) 题目大意 给定一棵树,每条边的颜色为黑或白,起始时均为白. 支持3种操作: 操作1:将a->b的路径中的所有边的颜色翻转. 操作 ...

  9. Aizu 2450 Do use segment tree 树链剖分+线段树

    Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...

  10. 【POJ3237】Tree(树链剖分+线段树)

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

随机推荐

  1. Java常用函数式接口--Consumer接口andThen()方法使用案例(二)

    Java常用函数式接口--Consumer接口使用案例

  2. 装饰者模式及php实现

    装饰模式(Decorator Pattern) : 动态地给一个对象增加一些额外的职责(Responsibility),就增加对象功能来说,装饰模式比生成子类实现更为灵活.其别名也可以称为包装器(Wr ...

  3. IOS使用固定定位遇到的问题

    近日需要实现移动端页面额外功能按钮,即点击加号弹出点赞与留言功能,通常这个按钮都会固定于页面的右下角,首先就想到使用固定定位来实现. 但是在测试时我们发现,在IOS中,当系统键盘弹出时,fixed会失 ...

  4. 从零开始的Lua宅[1]:编译Lua解释器

    Lua是一门神奇的脚本语言,游戏宅必备,懒人必备.Lua差多不是学起来用起来最简单的语言了,以至于简单到自身就是文档,自身就是配置文件.但是Lua的运行效率却是众多脚本中非常高的,据说仅次于V8爹下的 ...

  5. Python+Selenium与Chrome如何进行完美结合

    zhuan:http://blog.51cto.com/starpoint/2102975?cid=704621 使用WebDriver在Chrome浏览器上进行测试时,需要从http://chrom ...

  6. SqlServer 填充因子的说明

    CREATE NONCLUSTERED INDEX IX_d_name ON department(d_name) with fillfactor=30 使用 fill factor 选项可以指定 M ...

  7. SQL Server AlwaysON从入门到进阶(6)——分析和部署AlwaysOn Availability Group

    前言:   本节是整个系列的重点文章,到现在,读者应该已经对整个高可用架构有一定的了解,知道独立的SQL Server实例和基于群集的SQL Server FCI的区别.上一节已经介绍了如何安装SQL ...

  8. jsp之获传统方式取后台数据

    1.建立模型对象: package com.java.model; public class Student { private String name; private int age; publi ...

  9. HDU 5489 Removed Interval (LIS,变形)

    题意: 给出一个n个元素的序列,要求从中删除任一段长度为L的连续子序列,问删除后的LIS是多少?(n<=10w, L<=n ,元素可能为负) 思路: 如果会O(nlogn)求普通LIS的算 ...

  10. 51nod 1489 蜥蜴和地下室

    题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 哈利喜欢玩角色扮演的电脑游戏<蜥蜴和地下室>.此时,他正在扮演一个魔术 ...