将树通过树链剖分转化成线性序列,用线段树维护最值,和值即可。

  1 #include <bits/stdc++.h>
2 using namespace std;
3 const int N = 3e4 + 10;
4 int n, m, head[N], to[N << 1], nxt[N << 1], tot;
5 int total, fa[N], dep[N], size[N], son[N], top[N];
6 int w[N], id[N], rev[N];
7 int Max, Sum;
8 struct node{
9 int mx, sum;
10 }t[N << 2];
11 void add(int x, int y) {
12 nxt[++ tot] = head[x]; head[x] = tot; to[tot] = y;
13 }
14 void dfs1(int u, int f) { //求dep,fa,size,son
15 size[u] = 1;
16 for (int i = head[u]; i; i = nxt[i]) {
17 int v = to[i];
18 if (v == f) continue;
19 dep[v] = dep[u] + 1;
20 fa[v] = u;
21 dfs1(v, u);
22 size[u] += size[v];
23 if (size[v] > size[son[u]]) son[u] = v;
24 }
25 }
26 void dfs2(int u, int t) { //求top,id,rev
27 top[u] = t;
28 id[u] = ++ total; // u对应的dfs序下标
29 rev[total] = u; // dfs序下标对应的u
30 if (!son[u]) return ;
31 dfs2(son[u], t);//先沿重儿子dfs
32 for (int i = head[u]; i; i = nxt[i]) {
33 int v = to[i];
34 if (v != fa[u] && v != son[u]) dfs2(v, v);
35 }
36 }
37 namespace SegmentTree {
38 #define mid ((l + r) >> 1)
39 #define lson k << 1, l, mid
40 #define rson k << 1 | 1, mid + 1, r
41 void pushup(int k) {
42 t[k].mx = max(t[k << 1].mx, t[k << 1 | 1].mx);
43 t[k].sum = t[k << 1].sum + t[k << 1 | 1].sum;
44 }
45 void build(int k, int l, int r) {
46 if (l == r) {
47 t[k].mx = t[k].sum = w[rev[l]];
48 return ;
49 }
50 build(lson), build(rson);
51 pushup(k);
52 }
53 void update(int k, int l, int r, int pos, int v) {
54 if (l == r) {
55 t[k].mx = t[k].sum = v;
56 return ;
57 }
58 if (pos <= mid) update(lson, pos, v);
59 else update(rson, pos, v);
60 pushup(k);
61 }
62 void query(int k, int l, int r, int L, int R) {
63 if (L <= l && R >= r) {
64 Max = max(Max, t[k].mx);
65 Sum += t[k].sum;
66 return ;
67 }
68 if (L <= mid) query(lson, L, R);
69 if (R > mid) query(rson, L, R);
70 }
71 void ask(int u, int v) {
72 while (top[u] != top[v]) {
73 if(dep[top[u]] < dep[top[v]]) swap(u, v);
74 query(1, 1, total, id[top[u]], id[u]);
75 u = fa[top[u]];
76 }
77 if (dep[u] > dep[v]) swap(u, v);
78 query(1, 1, total, id[u], id[v]);
79 }
80 }
81 using namespace SegmentTree;
82 int main() {
83 int x, y; char str[10];
84 scanf("%d", &n);
85 for (int i = 1; i < n; i ++) {
86 scanf("%d %d", &x, &y);
87 add(x, y), add(y, x);
88 }
89 for (int i = 1; i <= n; i ++) scanf("%d", &w[i]);
90 dep[1] = 1;
91 dfs1(1, 0);
92 dfs2(1, 1);
93 build(1, 1, total);
94 scanf("%d", &m);
95 for (int i = 1; i <= m; i ++) {
96 scanf("%s", str);
97 scanf("%d %d", &x, &y);
98 if(str[0] == 'C') update(1, 1, total, id[x], y);
99 else {
100 Sum = 0;
101 Max = -0x3f3f3f3f;
102 ask(x, y);
103 if (str[1] == 'M') printf("%d\n", Max);
104 else printf("%d\n", Sum);
105 }
106 }
107 return 0;
108 }

HYSBZ1036 [ZJOI2008]树的统计(树链剖分)的更多相关文章

  1. BZOJ 1036: [ZJOI2008]树的统计Count-树链剖分(点权)(单点更新、路径节点最值、路径求和)模板,超级认真写了注释啊啊啊

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

  2. 树的统计Count---树链剖分

    NEFU专项训练十和十一——树链剖分 Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t ...

  3. BZOJ1036[ZJOI2008]树的统计——树链剖分+线段树

    题目描述 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v ...

  4. [ZJOI2008]树的统计——树链剖分

    本题是一个树链剖分裸题,由于比较菜,老是RE,后来发现是因为使用了全局变量. /************************************************************ ...

  5. [luogu P2590 ZJOI2008] 树的统计 (树链剖分)

    题目描述 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w. 我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u ...

  6. luoguP2590 [ZJOI2008]树的统计(树链剖分)

    luogu P2590 [ZJOI2008]树的统计 题目 #include<iostream> #include<cstdlib> #include<cstdio> ...

  7. 洛谷P2590 [ZJOI2008] 树的统计 [树链剖分]

    题目传送门 树的统计 题目描述 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w. 我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t ...

  8. BZOJ 1036 树的统计-树链剖分

    [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 12904 Solved: 5191[Submit][Status ...

  9. BZOJ-1036 树的统计Count 链剖线段树(模板)=(树链剖分+线段树)

    潇爷昨天刚刚讲完...感觉得还可以...对着模板打了个模板...还是不喜欢用指针.... 1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Lim ...

  10. bzoj1036 树的统计 树链剖分模板

    题意:给出树上任意两点,求路径上的值的和与最大值,带单点修改操作 树链剖分思路: 1.对树进行dfs求出点的深度和父亲节点,然后求出轻重儿子(重儿子就是点最多的那个子树,其余都是轻儿子),用一个son ...

随机推荐

  1. Javascript 函数声明、调用、闭包

    1 # Javascript 函数声明.调用.闭包 2 # 一.函数声明 3 # 1.直接声明.浏览器在执行前,会先将变量和函数声明进行提升. 4 fn(); 5 function fn () { 6 ...

  2. ApacheCon 2020 参会指南

    每年一度的 Apache 北美大会因为疫情的原因转到线上来举行了, 这次会议的主题是 ApacheCon@Home, 也就是说借助网络我们可以足不出户就可以参加 Apache 大会了.今年的会议为了针 ...

  3. day19--Java集合02

    Java集合02 6.ArrayList ArrayList的注意事项: Permits all element , including null ,ArrayList 可以加入null ,并且可以加 ...

  4. OpenStack-iaas之“先点”云平台安装

    1.认识OpenStack 1.云计算的起源 早在2006年3月,亚马逊公司首先提出弹性计算云服务.2006年8月9日,谷歌公司首席执行官埃里克·施密特(Eric Schmidt)在谷歌搜索引擎大会( ...

  5. 爬虫简介与excel表格操作

    爬虫简介与excel表格操作 re模块简介 1.在python中使用正则表达式的话那么re模块就是选择之一 import re # 导入re模块 2.在re模块中使用findall找到所有我们给他的值 ...

  6. SyncFusion安装和使用

    1.Visual Studio 继承 Visual Studio菜单栏 → 扩展 → 扩展管理 → 搜索框中搜索 "Syncfusion Windows",单击 "Win ...

  7. NOI P序列题 (二分)

    题面 题解 --WQS二分 想到这个这题就完了. 赛时没想到这个你就完了. 时间复杂度 O ( n log ⁡ a ) O(n\log a) O(nloga) 不难发现这题有凸性,可以WQS二分. 我 ...

  8. [NOI2021] 密码箱 (平衡树,连分数,Stern-Brocot 树,矩阵)

    题面 记忆犹新 题解 f f f 函数值给得非常明显,一看就给人一种熟悉感--这不是连分数吗? 众所周知,连分数有个递推公式,即 p i = a i p i − 1 + p i − 2 q i = a ...

  9. [CF1537E] Erase and Extend (字符串)

    题面 给一个长度为 n \tt n n 的字符串,你可以进行无限次以下两种操作之一: 删去末尾的字符(此时要保证删去后字符串非空). 把当前整个字符串复制一份,接到自己的后面. 输出最终通过操作能达到 ...

  10. 从0到1写一款自动为Markdown标题添加序号的Jetbrains插件

    1. markdown-index 最近做了一个Jetbrains的插件,叫markdown-index,它的作用是为Markdown文档的标题自动添加序号,效果如下: 目前已经可以在Jetbrain ...