题目

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

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

#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. Java初学心得(二)

    数组概述 一,数组基本操作 ①一维数组的创建:数组元素类型[] 数组名字=new 数组类型[数组元素个数] 例:int []arr=new int[5];数组长度为5 ②初始化一维数组:第一种:int ...

  2. IdentityServer4之Jwt身份验证方案分析

    一,准备内容 在之前讲过的asp.net core 实现OAuth2.0四种模式系列中的IdentityApi客户端用到了以下配置代码 public void ConfigureServices(IS ...

  3. 1+X证书学习日志 —— css样式表

    ## 因为初级的内容较多,所以选了一些有用的 需要记忆的内容写下 方便日后回顾 CSS语法   选择符{属性:属性值;} ##             所有的css代码 都要放在css样式表里面    ...

  4. Fortify漏洞之Dynamic Code Evaluation: Code Injection(动态脚本注入)和 Password Management: Hardcoded Password(密码硬编码)

    继续对Fortify的漏洞进行总结,本篇主要针对  Dynamic Code Evaluation: Code Injection(动态脚本注入) 和 Password Management: Har ...

  5. Core Animation笔记(动画)

    一.隐式动画 layer默认开启隐式动画 禁用隐式动画 [CATransaction setDisableActions:true]; 设置隐士动画时间 //默认0.25s [CATransactio ...

  6. wireshark语法小结

    https://blog.csdn.net/cumirror/article/details/7054496 过滤重传包 https://blog.csdn.net/Joseph_ChiRunning ...

  7. 删除Ubuntu的UEFI启动项

    bcdedit 删除 千万不要手贱用diskpart之类的命令直接删除文件夹,大写的没,有,用! 感谢这个视频的up主,youtube看不到请翻墙.https://www.youtube.com/wa ...

  8. ConcurrentLinkedQueue (一)

    ConcurrentLinkedQueue 主要讲一下在JDK8中,ConcurrentLikedQueue是如何入队,出队的. 首先我们要明白,ConcurrentLikedQueue是一种安全的没 ...

  9. 学习python的日常7

    ---恢复内容开始--- 正则表达式: 在正则表达式中,用\d可以匹配一个数字,\w可以匹配一个字母或数字,'.'可以匹配任意字符,用*表示任意个字符,用+表示至少一个字符,用?表示0个货一个字符,用 ...

  10. mysql学习之基础篇08 UTF8编码

    这次我们来说一下在Mysql中的编码问题: 我们知道应用于计算机的最早的字符集是ASCII,它所组成的编码是ASCII编码:由于对于其他国家来说它所容纳的字符个数比较少,后来就出现了ANSI字符集,它 ...