传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1036

保存模版。

执行qmax与qsum操作,往上爬的时候最开始的代码出了点小问题,往上爬的点应该是dep[top[u]]更深的点,而不是dep[u]更深的点。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> const int maxn = 30005; int n, q, t1, t2, root;
int head[maxn], to[maxn << 1], next[maxn << 1], lb, cnt;
int siz[maxn], dep[maxn], fa[maxn], id[maxn], heavy[maxn], v[maxn], top[maxn], a[maxn];
char opr[10];
struct Node {
int ql, qr, mx, sm;
} tree[maxn << 2]; inline void ist(int aa, int ss) {
to[lb] = ss;
next[lb] = head[aa];
head[aa] = lb;
++lb;
}
void dfs1(int r) {
siz[r] = 1;
dep[r] = dep[fa[r]] + 1;
int mx = 0, id = 0;
for (int j = head[r]; j != -1; j = next[j]) {
if (to[j] != fa[r]) {
fa[to[j]] = r;
dfs1(to[j]);
siz[r] += siz[to[j]];
if (siz[to[j]] > mx) {
mx = siz[id = to[j]];
}
}
}
heavy[r] = id;
}
void dfs2(int r, int tp) {
if (!r) {
return;
}
top[r] = tp;
id[r] = ++cnt;
a[cnt] = v[r];
dfs2(heavy[r], tp);
for (int j = head[r]; j != -1; j = next[j]) {
if (to[j] != fa[r] && to[j] != heavy[r]) {
dfs2(to[j], to[j]);
}
}
}
void make_tree(int p, int left, int right) {
tree[p].ql = left;
tree[p].qr = right;
if (left == right) {
tree[p].mx = tree[p].sm = a[left];
return;
}
int mid = (left + right) >> 1;
make_tree(p << 1, left, mid);
make_tree(p << 1 | 1, mid + 1, right);
tree[p].mx = std::max(tree[p << 1].mx, tree[p << 1 | 1].mx);
tree[p].sm = tree[p << 1].sm + tree[p << 1 | 1].sm;
}
void upd(int p, int r, int value) {
if (tree[p].ql == tree[p].qr) {
tree[p].mx = tree[p].sm = value;
return;
}
int mid = (tree[p].ql + tree[p].qr) >> 1;
if (r <= mid) {
upd(p << 1, r, value);
}
else {
upd(p << 1 | 1, r, value);
}
tree[p].mx = std::max(tree[p << 1].mx, tree[p << 1 | 1].mx);
tree[p].sm = tree[p << 1].sm + tree[p << 1 | 1].sm;
}
int getmax(int p, int left, int right) {
if (tree[p].ql == left && tree[p].qr == right) {
return tree[p].mx;
}
int mid = (tree[p].ql + tree[p].qr) >> 1;
if (right <= mid) {
return getmax(p << 1, left, right);
}
if (left > mid) {
return getmax(p << 1 | 1, left, right);
}
return std::max(getmax(p << 1, left, mid), getmax(p << 1 | 1, mid + 1, right));
}
int getsum(int p, int left, int right) {
if (tree[p].ql == left && tree[p].qr == right) {
return tree[p].sm;
}
int mid = (tree[p].ql + tree[p].qr) >> 1;
if (right <= mid) {
return getsum(p << 1, left, right);
}
if (left > mid) {
return getsum(p << 1 | 1, left, right);
}
return getsum(p << 1, left, mid) + getsum(p << 1 | 1, mid + 1, right);
}
int qmax(int u, int v) {
int rt = -2147483646;
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) {
std::swap(u, v);
}
rt = std::max(rt, getmax(1, id[top[u]], id[u]));
u = fa[top[u]];
}
if (dep[u] < dep[v]) {
std::swap(u, v);
}
rt = std::max(rt, getmax(1, id[v], id[u]));
return rt;
}
int qsum(int u, int v) {
int rt = 0;
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) {
std::swap(u, v);
}
rt += getsum(1, id[top[u]], id[u]);
u = fa[top[u]];
}
if (dep[u] < dep[v]) {
std::swap(u, v);
}
rt += getsum(1, id[v], id[u]);
return rt;
} int main(void) {
//freopen("in.txt", "r", stdin);
unsigned seed;
memset(head, -1, sizeof head);
memset(next, -1, sizeof next);
scanf("%d", &n);
seed += n;
for (int i = 1; i < n; ++i) {
scanf("%d%d", &t1, &t2);
ist(t1, t2);
ist(t2, t1);
seed += t1;
}
for (int i = 1; i <= n; ++i) {
scanf("%d", v + i);
} srand(seed);
root = rand() % n + 1;
dfs1(root);
dfs2(root, root);
make_tree(1, 1, n); scanf("%d", &q);
while (q--) {
scanf("%s%d%d", opr, &t1, &t2);
if (opr[1] == 'M') {
printf("%d\n", qmax(t1, t2));
}
else if (opr[1] == 'S') {
printf("%d\n", qsum(t1, t2));
}
else {
upd(1, id[t1], t2);
}
}
return 0;
}

  

_bzoj1036 [ZJOI2008]树的统计Count【树链剖分】的更多相关文章

  1. BZOJ 1036: [ZJOI2008]树的统计Count [树链剖分]【学习笔记】

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 14302  Solved: 5779[Submit ...

  2. 【BZOJ1036】[ZJOI2008]树的统计Count 树链剖分

    [BZOJ1036][ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. ...

  3. Bzoj 1036: [ZJOI2008]树的统计Count 树链剖分,LCT

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 11102  Solved: 4490[Submit ...

  4. BZOJ 1036: [ZJOI2008]树的统计Count( 树链剖分 )

    树链剖分... 不知道为什么跑这么慢 = = 调了一节课啊跪.. ------------------------------------------------------------------- ...

  5. bzoj1036 [ZJOI2008]树的统计Count 树链剖分模板题

    [ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成 一些操作: I. CHANGE u ...

  6. bzoj 1036: [ZJOI2008]树的统计Count 树链剖分+线段树

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 16294  Solved: 6645[Submit ...

  7. BZOJ 1036: [ZJOI2008]树的统计Count (树链剖分模板题)

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 14982  Solved: 6081[Submit ...

  8. BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分)(线段树单点修改)

    [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 14968  Solved: 6079[Submit][Stat ...

  9. Cogs 1688. [ZJOI2008]树的统计Count(树链剖分+线段树||LCT)

    [ZJOI2008]树的统计Count ★★★ 输入文件:bzoj_1036.in 输出文件:bzoj_1036.out 简单对比 时间限制:5 s 内存限制:162 MB [题目描述] 一棵树上有n ...

  10. BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分 - 点权剖分 - 单点权修改)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1036 树链剖分模版题,打的时候注意点就行.做这题的时候,真的傻了,单词拼错检查了一个多小时 ...

随机推荐

  1. 使用Python将数据插入数据库(一)

    假如我现在有一个Excel数据表格,需要将其数据插入MySQL数据库中.数据如下: 对应的Python实现代码如下: #导入需要使用到的数据模块 import pandas as pd import ...

  2. JAVA数组去除重复数据

    一.用List集合实现   , , , , , , ,}; List<Integer> list = new ArrayList<Integer>(); ; i<str. ...

  3. lightoj 1138 - Trailing Zeroes (III)【二分】

    题目链接:http://lightoj.com/volume_showproblem.php? problem=1138 题意:问 N. 末尾 0 的个数为 Q 个的数是什么? 解法:二分枚举N,由于 ...

  4. C#结构类型图

    C#结构类型图     分类: C#

  5. EA生成实体类代码

    引言 在做机房个人版重构的时候,就听说了EA是一个强大的软件.仅仅只是知道的时候,已经画完了图,没有怎么用EA其它的功能,所以一直没有见识过罢了.如今到了机房合作了,想到EA一定要好好用,这样能省不少 ...

  6. Spring中注解

    @Autowired :spring注解 @Resource :J2EE注解 @Transactional(rollbackFor=Exception.class):指定回滚 @RequestMapp ...

  7. 3 Angular 2 快速上手启动项目Demo

    Angular2.x与Angular1.x 的区别类似 Java 和 JavaScript 或者说是雷锋与雷峰塔的区别,想要运行Angular2需要安装一些第三方依赖,不会像Angular1.x那样, ...

  8. 关于集成支付宝SDK的开发

    下载 首先,你要想找到这个SDK,都得费点功夫.如今的SDK改名叫移动支付集成开发包了,下载页面在 这里 的 "请点此下载集成开发包" Baidu和Googlep排在前面的支付宝开 ...

  9. import-module in $profile

    $PROFILE C:\Users\clu\Documents\PowerShell\Microsoft.PowerShell_profile.ps1 Import-Module 'C:\Users\ ...

  10. django 初试

    /************************************************************************************** * django 初试 ...