题目

树剖裸题,这个题更可以深刻的理解树剖中把树上的节点转换为区间的思想。

要注意在区间上连续的节点,一定是在一棵子树中。

#include <bits/stdc++.h>
#define int long long
#define ls left, mid, root << 1
#define rs mid + 1, right, root << 1 | 1
#define N 600100
using namespace std;
int n, m, rot, mod, tot, cnt;
int data[N], id[N], dep[N], size[N], lin[N], ans[N * 8], lazy[N * 8], dp[N], fa[N], top[N], son[N];
struct edg {
int to, nex;
} e[N];
inline void add(int f, int t)
{
e[++cnt].to = t;
e[cnt].nex = lin[f];
lin[f] = cnt;
}
inline void pushup(int root)
{
ans[root] = (ans[root << 1] + ans[root << 1 | 1]);
}
inline void pushdown(int root, int left, int right)
{
int mid = (left + right) >> 1;
if (lazy[root])
{
ans[root << 1] += (mid - left + 1) * lazy[root];
ans[root << 1];
ans[root << 1 | 1] += (right - mid) * lazy[root];
ans[root << 1 | 1];
lazy[root << 1] += lazy[root];
lazy[root << 1 | 1] += lazy[root];
lazy[root] = 0;
}
}
void build(int left, int right, int root)
{
if (left == right)
{
ans[root] = dp[left], ans[root];
return;
}
int mid = (left + right) >> 1;
build(ls), build(rs);
pushup(root);
}
inline void update(int left, int right, int root, int add, int ql, int qr)
{
if (left >= ql && right <= qr)
{
ans[root] += (right - left + 1) * add;
lazy[root] += add;
ans[root];
return;
}
int mid = (left + right) >> 1;
pushdown(root, left, right);//线段树的pushdown操作是为了弥补之前没向下传递标记的坑。
if (ql <= mid)
update(ls, add, ql, qr);
if (qr > mid)
update(rs, add, ql, qr);
pushup(root);
}
inline int query(int left, int right, int root, int ql, int qr)
{
int res = 0;
if (left >= ql && right <= qr)
return ans[root];
int mid = (left + right) >> 1;
pushdown(root, left, right);
if (ql <= mid)
res = ( res + query(ls, ql, qr) );
if (qr > mid)
res = res + query(rs, ql, qr) ;
return res;
}
void dfs1(int now, int f, int de)
{
fa[now] = f, dep[now] = de, size[now] = 1;
int maxsize = -1;
for (int i = lin[now]; i; i = e[i].nex)
{
if (e[i].to == f) continue;
dfs1(e[i].to, now, de + 1);
size[now] += size[e[i].to];
if (size[e[i].to] > maxsize)
{
maxsize = size[e[i].to];
son[now] = e[i].to;
}
}
}
void ulca(int x, int y, int z)
{
while (top[x] != top[y])
{
if (dep[top[x]] < dep[top[y]])
swap(x, y);
update(1, n, 1, z, id[top[x]], id[x]);
x = fa[top[x]];
}
if (dep[x] > dep[y])
swap(x, y);
update(1, n, 1, z, id[x], id[y]);
}
int qlca(int x, int y)
{
int res = 0;
while (top[x] != top[y])
{
if (dep[top[x]] < dep[top[y]])
swap(x, y);
res = ( res + query(1, n, 1, id[top[x]], id[x]) );
x = fa[top[x]];
}
if (dep[x] > dep[y])
swap(x, y);
res = ( res + query(1, n, 1, id[x], id[y]) );
return res;
}
void upd(int x, int y)
{
update(1, n, 1, y, id[x], id[x] + size[x] - 1);
}
int que(int x)
{
return query(1, n, 1, id[x], id[x] + size[x] - 1);
}
void dfs2(int now, int t)
{
top[now] = t;
dp[++tot] = data[now];
id[now] = tot;
if (!son[now])
return;
dfs2(son[now], t);
for (int i = lin[now]; i; i = e[i].nex)
{
int to = e[i].to;
if (to != fa[now] && to != son[now])
dfs2(to, to);
}
}
signed main()
{
scanf("%lld%lld", &n, &m);
rot = 1;
for (int i = 1; i <= n; i++)
scanf("%lld", &data[i]);
for (int i = 1; i < n; i++)
{
int a, b;
scanf("%lld%lld", &a, &b);
add(a, b);
add(b, a);
}
dfs1(rot, 0, 1);
dfs2(rot, rot);
build(1, n, 1);
for (int i = 1; i <= m; i++)
{
int flag;
scanf("%lld", &flag);
if (flag == 1)
{
int x, a;
scanf("%lld%lld", &x, &a);
update(1, n, 1, a, id[x], id[x]);
}
if (flag == 2)
{
int x, a;
scanf("%lld%lld", &x, &a);
update(1, n, 1, a, id[x], id[x] + size[x] - 1);
}
if (flag == 3)
{
int a;
scanf("%lld", &a);
printf("%lld\n", qlca(1, a));//不能写成id[1],id[a]因为id子树之间是连续的,所以a到1之间并不是连续边号。
}
}
return 0;
}
/*
5 5 2 24
7 3 7 8 0
1 2
1 5
3 1
4 1
3 4 2
3 2 2
4 5
1 5 1 3
2 1 3
*/

洛谷P3178[HAOI]2015 树上操作的更多相关文章

  1. cogs 1963. [HAOI 2015] 树上操作 树链剖分+线段树

    1963. [HAOI 2015] 树上操作 ★★★☆   输入文件:haoi2015_t2.in   输出文件:haoi2015_t2.out   简单对比时间限制:1 s   内存限制:256 M ...

  2. [bzoj 4034][HAOI 2015]树上操作

    Description 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中 ...

  3. 洛谷 P4665 [BalticOI 2015]Network

    洛谷 P4665 [BalticOI 2015]Network 你有一棵 $ n $ 个节点的树,你可以在树上加一些边,使这棵树变成一张无重边.自环的图,且删掉任意一条边它仍然联通.求最少要加多少条边 ...

  4. 洛谷P3178 [HAOI2015]树上操作(dfs序+线段树)

    P3178 [HAOI2015]树上操作 题目链接:https://www.luogu.org/problemnew/show/P3178 题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边 ...

  5. 洛谷 P3178 BZOJ 4034 [HAOI2015]树上操作

    题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种:操作 1 :把某个节点 x 的点权增加 a .操作 2 :把某个节点 x 为根的子树中所有点的点权都增加 ...

  6. 洛谷P1273 有线电视网 (树上分组背包)

    洛谷P1273 有线电视网 题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端,其他中转站为该树的内部节 ...

  7. 洛谷P3177||bzoj4033 [HAOI2015]树上染色

    洛谷P3177 bzoj4033 根本不会做... 上网查了题解,发现只要在状态定义的时候就考虑每一条边全局的贡献就好了? 考虑边的贡献和修改状态定义我都想到了,然而并不能想到要结合起来 ans[i] ...

  8. bzoj2333[SCOI2011]棘手的操作 洛谷P3273 [SCOI2011]棘手的操作

    2333? 先记一下吧,这题现在全部都是照着题解做的,因为怎么改都改不出来,只好对着题解改,以后还要再做过 以后再也不用指针了!太恶心了!空指针可不止直接特判那么简单啊,竟然还要因为空指针写奇怪的分类 ...

  9. 洛谷P3178 [HAOI2015]树上操作 题解 树链剖分+线段树

    题目链接:https://www.luogu.org/problem/P3178 这道题目是一道树链剖分的模板题. 但是在解决这道问题的同事刷新了我的两个认识: 第一个认识是:树链剖分不光可以处理链, ...

随机推荐

  1. go 缓冲IO

    package main import ( "bufio" "fmt" "os" "strings" ) func ma ...

  2. webpack+vue搭建vue项目

    阅读地址: https://www.jianshu.com/p/23beadfa4aa5 源码地址:https://github.com/Ezoio/IMI-SOURCE-CODE

  3. 单例模式的双重锁为什么要加volatile(转)

    单例模式如下: 需要volatile关键字的原因是,在并发情况下,如果没有volatile关键字,在第5行会出现问题. instance = new TestInstance();可以分解为3行伪代码 ...

  4. element-ui 穿梭框使用axios数据查询

    //class="input"样式自写,用来覆盖穿梭框自带的搜索,它自带的搜索框不能搜索外部数据,只能查询在穿梭框内的数据 <div style="text-ali ...

  5. IDEA中看Flink 1.9源码时报Sources not found for: org.apache.flink:flink-shaded-hadoop-2:2.4.1-7.0

    1.场景 在阅读Flink 1.9源码时,个别类如YarnClientImpl.java只能查看.class文件,想查看对应的.java source文件,点击Download source时,报So ...

  6. (Linux基础学习)第五章:Linux中的screen应用

    第1节:安装screen1.加载系统镜像文件,因为screen的安装包在系统镜像文件中图001 2.列出系统上所有的磁盘[root@centos6 ~]# lsblk图002 3.安装screen应用 ...

  7. IDG资本

    https://baike.baidu.com/item/IDG/10412 美国国际数据集团(International Data Group) 是全世界最大的信息技术出版.研究.发展与风险投资公司 ...

  8. 使用wc -l 来统计代码行数

    Linux使用wc来统计行数真的好用 如在当前路径下统计一共多少行代码 $ find ./ -name *.c |xargs wc -l #包括了空行 ##-l :lines 如果不包括空行 ¥fin ...

  9. 使用Numpy的矩阵来实现神经网络

    要是书都讲得这么细致, AI也不会那么难学啦. import numpy as np # sigmoid作为隐藏层的激活函数 def sigmoid(x): return 1 / (1 + np.ex ...

  10. Vuex基础 -01 -实现简易计数器 -支持 加数/ 减数/ 奇数再加/ 异步加法(setTimeout 1000ms) -单组件演示语法

    Vuex 的结构图 工程组织 Vuex的核心管理程序 store.js /* vuex的核心管理程序 */ import Vue from 'vue' import Vuex from 'vuex' ...