HDU Aragorn's Story

题目链接

树抛入门裸题,这题是区间改动单点查询,于是套树状数组就OK了

代码:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std; const int N = 50005; inline int lowbit(int x) {return x&(-x);} int dep[N], fa[N], son[N], sz[N], top[N], id[N], idx;
vector<int> g[N]; int bit[N]; int n, m, p, val[N]; void dfs1(int u, int f, int d) {
dep[u] = d;
sz[u] = 1;
fa[u] = f;
son[u] = 0;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v == f) continue;
dfs1(v, u, d + 1);
sz[u] += sz[v];
if (sz[son[u]] < sz[v])
son[u] = v;
}
} void dfs2(int u, int tp) {
id[u] = ++idx;
top[u] = tp;
if (son[u]) dfs2(son[u], tp);
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
} void add(int x, int v) {
while (x < N) {
bit[x] += v;
x += lowbit(x);
}
} void add(int l, int r, int v) {
add(l, v);
add(r + 1, -v);
} void gao(int u, int v, int w) {
int tp1 = top[u], tp2 = top[v];
while (tp1 != tp2) {
if (dep[tp1] < dep[tp2]) {
swap(tp1, tp2);
swap(u, v);
}
add(id[tp1], id[u], w);
u = fa[tp1];
tp1 = top[u];
}
if (dep[u] > dep[v]) swap(u, v);
add(id[u], id[v], w);
} int query(int x) {
int ans = 0;
while (x) {
ans += bit[x];
x -= lowbit(x);
}
return ans;
} int main() {
while (~scanf("%d%d%d", &n, &m, &p)) {
idx = 0;
memset(bit, 0, sizeof(bit));
for (int i = 1; i <= n; i++) {
scanf("%d", &val[i]);
g[i].clear();
}
int u, v;
while (m--) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs1(1, -1, 1);
dfs2(1, 1);
for (int i = 1; i <= n; i++) add(id[i], id[i], val[i]);
char q[2];
int a, b, c;
while (p--) {
scanf("%s", q);
if (q[0] == 'I' || q[0] == 'D') {
scanf("%d%d%d", &a, &b, &c);
if (q[0] == 'D') c = -c;
gao(a, b, c);
} else {
scanf("%d", &a);
printf("%d\n", query(id[a]));
}
}
}
return 0;
}

HDU 3966 Aragorn&#39;s Story(树链剖分)的更多相关文章

  1. hdu 3966 Aragorn&#39;s Story(树链剖分+树状数组)

    pid=3966" target="_blank" style="">题目链接:hdu 3966 Aragorn's Story 题目大意:给定 ...

  2. HDU - 3966 Aragorn's Story(树链剖分入门+线段树)

    HDU - 3966 Aragorn's Story Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & ...

  3. hdu 3966 Aragorn's Story(树链剖分+树状数组/线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: 给出一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路 ...

  4. hdu 3966 Aragorn's Story(树链剖分+区间修改+单点查询)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:给一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路径上 ...

  5. HDU 3966 Aragorn's Story (树链剖分+树状数组)

    Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. HDU 3966 Aragorn's Story (简单树链剖分)

    题意:给一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路径上的所有点权值加上K D C1 C2 K:把C1与C2的路径上的所有点权值减去K Q C:查询节点编号为C ...

  7. HDU 3966 Aragorn's Story(树链剖分)题解

    题意:给一棵树,要求你对一个路径上的值进行加减,查询某个点的值 思路:重链剖分. 由于分了轻重儿子,我每次到重儿子的top只要O(1),经过的轻儿子最多logn条,那么我每次往上跳最多跳logn次. ...

  8. HDU 3966 & POJ 3237 & HYSBZ 2243 树链剖分

    树链剖分是一个很固定的套路 一般用来解决树上两点之间的路径更改与查询 思想是将一棵树分成不想交的几条链 并且由于dfs的顺序性 给每条链上的点或边标的号必定是连着的 那么每两个点之间的路径都可以拆成几 ...

  9. HDU 3966 Aragorn's Story 动态树 树链剖分

    Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  10. HDU 3966:Aragorn's Story(树链剖分)

    http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:有n个点n-1条边,每个点有一个权值,有两种操作:询问一个点上权值是多少和修改u到v这条链上的权值. ...

随机推荐

  1. 大数据计算:如何仅用1.5KB内存为十亿对象计数

    大数据计算:如何仅用1.5KB内存为十亿对象计数  Big Data Counting: How To Count A Billion Distinct Objects Using Only 1.5K ...

  2. cocos2d-x游戏开发系列教程-超级玛丽07-CMGameMap

    背景 在上一篇博客中,我们提到CMGameScene,但是CMGameScene只是个框架,实际担任游戏逻辑的是CMGameMap类,这个博文就来了解下CMGameMap 头文件 class CMGa ...

  3. OI不得不知的那些数学定理

    Binomial theorem One can define\[{r \choose k}=\frac{r\,(r-1) \cdots (r-k+1)}{k!} =\frac{(r)_k}{k!}\ ...

  4. notify()、notifyAll()和wait()

    看到一道面试题,写一个多线程程序,交替输出1.2.1.2…… 先写下程序: /** * Created by Andrew on 2015/10/28. */ public class OutputT ...

  5. STL vector使用方法介绍

    介绍 这篇文章的目的是为了介绍std::vector,怎样恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...

  6. 全局获取Context的技巧

    全局获取Context的技巧 在android开发中,非常多地方都要用到Context上下文这个类对象,比方:弹出 Toast 的时候须要.启动活动的时候须要.发送广播的时候须要.操作数据库的时候须要 ...

  7. 第一篇:NSTread线程的创建

    #import "ViewController.h" //导入头文件 #import <pthread.h> @interfaceViewController () @ ...

  8. OC-多线程GCD的使用细节

    1>GCD,全称Grand Central Dispatch,”牛逼的中枢调度器”,纯C语言,提供了非常多强大的函数2>GCD优势:(1)GCD是苹果公司为多核的并行运算提出的解决方案(2 ...

  9. ibatis3.0调用Oracle的存储过程

    直接上源码 一,oracle储存过程. create or replace procedure proc_get_th(i_hth in varchar2,o_ret out sys_refcurso ...

  10. 【 D3.js 入门系列 — 2 】 绑定数据和选择元素

    1. 如何绑定数据 D3 有一个很独特的功能:能将数据绑定到 DOM 上,也就是绑定到文档上.这么说可能不好理解,例如网页中有段落元素<p>,我们可以将整数 5 与 <p>绑定 ...