2021-07-31:给定数组father,大小为N,表示一共有N个节点,father[i] = j 表示点i的父亲是点j, father表示的树一定是一棵树而不是森林,给定数组values,大小为N
2021-07-31:给定数组father,大小为N,表示一共有N个节点,father[i] = j 表示点i的父亲是点j, father表示的树一定是一棵树而不是森林,给定数组values,大小为N,values[i]=v表示节点i的权值是v。实现如下4个方法,保证4个方法都很快!1)让某个子树所有节点值加上v,入参:int head, int v;2)查询某个子树所有节点值的累加和,入参:int head;3)在树上从a到b的整条链上所有加上v,入参:int a, int b, int v;4)查询在树上从a到b的整条链上所有节点值的累加和,入参:int a, int b。
福大大 答案2021-07-31:
时间紧。见代码。
代码用golang编写。代码如下:
package main
import (
"fmt"
"math"
)
func main() {
father := []int{1, 2, 3, 4, 5}
values := []int{6, 7, 8, 9, 10}
tc := NewTreeChain(father, values)
right := NewRight(father, values)
tc.addSubtree(11, 22)
right.addSubtree(11, 22)
tc.addSubtree(33, 44)
right.addSubtree(33, 44)
if tc.queryChain(33, 44) != right.queryChain(33, 44) {
fmt.Println("错误")
} else {
fmt.Println("正确")
}
}
type TreeChain struct {
// 时间戳 0 1 2 3 4
tim int
// 节点个数是n,节点编号是1~n
n int
// 谁是头
h int
// 朴素树结构
tree [][]int
// 权重数组 原始的0节点权重是6 -> val[1] = 6
val []int
// father数组一个平移,因为标号要+1
fa []int
// 深度数组!
dep []int
// son[i] = 0 i这个节点,没有儿子
// son[i] != 0 j i这个节点,重儿子是j
son []int
// siz[i] i这个节点为头的子树,有多少个节点
siz []int
// top[i] = j i这个节点,所在的重链,头是j
top []int
// dfn[i] = j i这个节点,在dfs序中是第j个
dfn []int
// 如果原来的节点a,权重是10
// 如果a节点在dfs序中是第5个节点, tnw[5] = 10
tnw []int
// 线段树,在tnw上,玩连续的区间查询或者更新
seg *SegmentTree
}
func NewTreeChain(father []int, values []int) *TreeChain {
ret := &TreeChain{}
// 原始的树 tree,弄好了,可以从i这个点,找到下级的直接孩子
// 上面的一大堆结构,准备好了空间,values -> val
// 找到头部点
ret.initTree(father, values)
// fa;
// dep;
// son;
// siz;
ret.dfs1(ret.h, 0)
// top;
// dfn;
// tnw;
ret.dfs2(ret.h, ret.h)
ret.seg = NewSegmentTree(ret.tnw)
ret.seg.build(1, ret.n, 1)
return ret
}
func (this *TreeChain) initTree(father []int, values []int) {
this.tim = 0
this.n = len(father) + 1
this.tree = make([][]int, this.n)
this.val = make([]int, this.n)
this.fa = make([]int, this.n)
this.dep = make([]int, this.n)
this.son = make([]int, this.n)
this.siz = make([]int, this.n)
this.top = make([]int, this.n)
this.dfn = make([]int, this.n)
this.tnw = make([]int, this.n)
this.n--
cnum := make([]int, this.n)
for i := 0; i < this.n; i++ {
this.val[i+1] = values[i]
}
for i := 0; i < this.n; i++ {
if father[i] == i {
this.h = i + 1
} else {
cnum[father[i]]++
}
}
this.tree[0] = make([]int, 0)
for i := 0; i < this.n; i++ {
this.tree[i+1] = make([]int, cnum[i])
}
for i := 0; i < this.n; i++ {
if i+1 != this.h {
cnum[father[i]]--
this.tree[father[i]+1][cnum[father[i]]] = i + 1
}
}
}
// u 当前节点
// f u的父节点
func (this *TreeChain) dfs1(u int, f int) {
this.fa[u] = f
this.dep[u] = this.dep[f] + 1
this.siz[u] = 1
maxSize := -1
for _, v := range this.tree[u] { // 遍历u节点,所有的直接孩子
this.dfs1(v, u)
this.siz[u] += this.siz[v]
if this.siz[v] > maxSize {
maxSize = this.siz[v]
this.son[u] = v
}
}
}
// u当前节点
// t是u所在重链的头部
func (this *TreeChain) dfs2(u int, t int) {
this.tim++
this.dfn[u] = this.tim
this.top[u] = t
this.tnw[this.tim] = this.val[u]
if this.son[u] != 0 { // 如果u有儿子 siz[u] > 1
this.dfs2(this.son[u], t)
for _, v := range this.tree[u] {
if v != this.son[u] {
this.dfs2(v, v)
}
}
}
}
// head为头的子树上,所有节点值+value
// 因为节点经过平移,所以head(原始节点) -> head(平移节点)
func (this *TreeChain) addSubtree(head int, value int) {
// 原始点编号 -> 平移编号
head++
// 平移编号 -> dfs编号 dfn[head]
this.seg.add(this.dfn[head], this.dfn[head]+this.siz[head]-1, value, 1, this.n, 1)
}
func (this *TreeChain) querySubtree(head int) int {
head++
return this.seg.query(this.dfn[head], this.dfn[head]+this.siz[head]-1, 1, this.n, 1)
}
func (this *TreeChain) addChain(a int, b int, v int) {
a++
b++
for this.top[a] != this.top[b] {
if this.dep[this.top[a]] > this.dep[this.top[b]] {
this.seg.add(this.dfn[this.top[a]], this.dfn[a], v, 1, this.n, 1)
a = this.fa[this.top[a]]
} else {
this.seg.add(this.dfn[this.top[b]], this.dfn[b], v, 1, this.n, 1)
b = this.fa[this.top[b]]
}
}
if this.dep[a] > this.dep[b] {
this.seg.add(this.dfn[b], this.dfn[a], v, 1, this.n, 1)
} else {
this.seg.add(this.dfn[a], this.dfn[b], v, 1, this.n, 1)
}
}
func (this *TreeChain) queryChain(a int, b int) int {
a++
b++
ans := 0
for this.top[a] != this.top[b] {
if this.dep[this.top[a]] > this.dep[this.top[b]] {
ans += this.seg.query(this.dfn[this.top[a]], this.dfn[a], 1, this.n, 1)
a = this.fa[this.top[a]]
} else {
ans += this.seg.query(this.dfn[this.top[b]], this.dfn[b], 1, this.n, 1)
b = this.fa[this.top[b]]
}
}
if this.dep[a] > this.dep[b] {
ans += this.seg.query(this.dfn[b], this.dfn[a], 1, this.n, 1)
} else {
ans += this.seg.query(this.dfn[a], this.dfn[b], 1, this.n, 1)
}
return ans
}
type SegmentTree struct {
MAXN int
arr []int
sum []int
lazy []int
}
func NewSegmentTree(origin []int) *SegmentTree {
ret := &SegmentTree{}
ret.MAXN = len(origin)
ret.arr = origin
ret.sum = make([]int, ret.MAXN<<2)
ret.lazy = make([]int, ret.MAXN<<2)
return ret
}
func (this *SegmentTree) pushUp(rt int) {
this.sum[rt] = this.sum[rt<<1] + this.sum[rt<<1|1]
}
func (this *SegmentTree) pushDown(rt int, ln int, rn int) {
if this.lazy[rt] != 0 {
this.lazy[rt<<1] += this.lazy[rt]
this.sum[rt<<1] += this.lazy[rt] * ln
this.lazy[rt<<1|1] += this.lazy[rt]
this.sum[rt<<1|1] += this.lazy[rt] * rn
this.lazy[rt] = 0
}
}
func (this *SegmentTree) build(l int, r int, rt int) {
if l == r {
this.sum[rt] = this.arr[l]
return
}
mid := (l + r) >> 1
this.build(l, mid, rt<<1)
this.build(mid+1, r, rt<<1|1)
this.pushUp(rt)
}
func (this *SegmentTree) add(L int, R int, C int, l int, r int, rt int) {
if L <= l && r <= R {
this.sum[rt] += C * (r - l + 1)
this.lazy[rt] += C
return
}
mid := (l + r) >> 1
this.pushDown(rt, mid-l+1, r-mid)
if L <= mid {
this.add(L, R, C, l, mid, rt<<1)
}
if R > mid {
this.add(L, R, C, mid+1, r, rt<<1|1)
}
this.pushUp(rt)
}
func (this *SegmentTree) query(L int, R int, l int, r int, rt int) int {
if L <= l && r <= R {
return this.sum[rt]
}
mid := (l + r) >> 1
this.pushDown(rt, mid-l+1, r-mid)
ans := 0
if L <= mid {
ans += this.query(L, R, l, mid, rt<<1)
}
if R > mid {
ans += this.query(L, R, mid+1, r, rt<<1|1)
}
return ans
}
// 为了测试,这个结构是暴力但正确的方法
type Right struct {
n int
tree [][]int
fa []int
val []int
path map[int]int
}
func NewRight(father []int, value []int) *Right {
ret := &Right{}
ret.n = len(father)
ret.tree = make([][]int, ret.n)
ret.fa = make([]int, ret.n)
ret.val = make([]int, ret.n)
for i := 0; i < ret.n; i++ {
ret.fa[i] = father[i]
ret.val[i] = value[i]
}
help := make([]int, ret.n)
h := 0
for i := 0; i < ret.n; i++ {
if father[i] == i {
h = i
} else {
help[father[i]]++
}
}
for i := 0; i < ret.n; i++ {
ret.tree[i] = make([]int, help[i])
}
for i := 0; i < ret.n; i++ {
if i != h {
help[father[i]]--
ret.tree[father[i]][help[father[i]]] = i
}
}
ret.path = make(map[int]int)
return ret
}
func (this *Right) addSubtree(head int, value int) {
this.val[head] += value
for _, next := range this.tree[head] {
this.addSubtree(next, value)
}
}
func (this *Right) querySubtree(head int) int {
ans := this.val[head]
for _, next := range this.tree[head] {
ans += this.querySubtree(next)
}
return ans
}
func (this *Right) addChain(a int, b int, v int) {
this.path = make(map[int]int)
this.path[a] = math.MinInt64
for a != this.fa[a] {
this.path[this.fa[a]] = a
a = this.fa[a]
}
_, ok := this.path[b]
for !ok {
this.val[b] += v
b = this.fa[b]
}
this.val[b] += v
for this.path[b] != math.MaxInt64 {
b = this.path[b]
this.val[b] += v
}
}
func (this *Right) queryChain(a int, b int) int {
this.path = make(map[int]int)
this.path[a] = math.MinInt64
for a != this.fa[a] {
this.path[this.fa[a]] = a
a = this.fa[a]
}
ans := 0
_, ok := this.path[b]
for !ok {
ans += this.val[b]
b = this.fa[b]
}
ans += this.val[b]
for this.path[b] != math.MinInt64 {
b = this.path[b]
ans += this.val[b]
}
return ans
}
2021-07-31:给定数组father,大小为N,表示一共有N个节点,father[i] = j 表示点i的父亲是点j, father表示的树一定是一棵树而不是森林,给定数组values,大小为N的更多相关文章
- [LeetCode]Subtree of Another Tree判断一棵树是不是另一棵树的子树
将树序列化为字符串,空节点用符号表示,这样可以唯一的表示一棵树. 用list记录所有子树的序列化,和目标树比较. List<String> list = new ArrayList< ...
- PHP 一个树为另一棵树的子结构 [TO BE CONTINUED]
输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) <?php class TreeNode { private $val; private $left; ...
- 判断一棵树是否为二叉搜索树(二叉排序树) python
输入一棵树,判断这棵树是否为二叉搜索树.首先要知道什么是排序二叉树,二叉排序树是这样定义的,二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: (1)若左子树不空,则左子树上所有结点的值均小于它的 ...
- SDUT 2129 树结构练习——判断给定森林中有多少棵树
树结构练习——判断给定森林中有多少棵树 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 众 ...
- A Simple Problem with Integers(100棵树状数组)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
- Codeforces - 828E DNA Evolution —— 很多棵树状数组
题目链接:http://codeforces.com/contest/828/problem/E E. DNA Evolution time limit per test 2 seconds memo ...
- hdu6035 Colorful Tree 树形dp 给定一棵树,每个节点有一个颜色值。定义每条路径的值为经过的节点的不同颜色数。求所有路径的值和。
/** 题目:hdu6035 Colorful Tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题意:给定一棵树,每个节点有一个颜色值.定 ...
- Trees in a Wood. UVA 10214 欧拉函数或者容斥定理 给定a,b求 |x|<=a, |y|<=b这个范围内的所有整点不包括原点都种一棵树。求出你站在原点向四周看到的树的数量/总的树的数量的值。
/** 题目:Trees in a Wood. UVA 10214 链接:https://vjudge.net/problem/UVA-10214 题意:给定a,b求 |x|<=a, |y|&l ...
- Codeforces Round #590 (Div. 3)【D题:26棵树状数组维护字符出现次数】
A题 题意:给你 n 个数 , 你需要改变这些数使得这 n 个数的值相等 , 并且要求改变后所有数的和需大于等于原来的所有数字的和 , 然后输出满足题意且改变后最小的数值. AC代码: #includ ...
- Codeforces Round #590 (Div. 3)【D题:维护26棵树状数组【好题】】
A题 题意:给你 n 个数 , 你需要改变这些数使得这 n 个数的值相等 , 并且要求改变后所有数的和需大于等于原来的所有数字的和 , 然后输出满足题意且改变后最小的数值. AC代码: #includ ...
随机推荐
- VulnHub靶场练习之 grotesque:2
步骤1:信息收集 拿到靶机首先看一下开放的端口 可以看到开放了很多端口 比如22 ssh.80 http等等 那么就先访问一下80看一下 页面没有什么有用的东西 查看一下网页源代码 也没有找到什么有价 ...
- LaTeX in 24 Hours - 1. Introduction
文章目录 1.1 What is LaTeX? 1.2 Why LaTeX Over Other Word Processors 1.3 How to Prepare a LaTeX Input Fi ...
- Promise静态方法实现(all race finally resolve reject)
示例 // Promise.resolve() Promise.resolve(1).then((data) => { console.log(data) // 1 }) // Promise. ...
- 关于windows cmd 控制台输出中文
由于中文在window 输出总是优乱码可能性 ,先建cmd.reg 负责下面内容 ,双击运行即可. Windows Registry Editor Version 5.00 [HKEY_CURR ...
- java数据类型转换有哪几种?看这篇就够了!
前言 在上一篇文章中,壹哥给大家讲解了Java中的数据类型,从此大家就知道了基本类型和引用类型,尤其是8种基本类型的使用和各自特点.但实际上数据类型的使用还有很多更深入的内容,比如java数据类型直接 ...
- 万字血书Vue—Vuex
Vuex概述 组件之间共享数据的方式(小范围) 全局事件总线 Vuex是什么? 专门在Vue中实现集中式状态(数据)管理的一个Vue插件,可以方便的实现组件之间的数据共享. 使用Vuex统一管理状态的 ...
- 使用dataX收获的教训
首先是安装dataX,安装非常简单,只需下载对应的压缩包即可. 下载地址:https://github.com/alibaba/DataX 首先我出现的第一个错误:命令提示符界面出现出现乱码. 解决方 ...
- 当后端人员未提供接口,前端人员该怎么测试 --mock
1.回顾 2.线上的mock http://rap2.taobao.org/ https://www.easy-mock.com/ 3.线上接口文档 Swagger https://swagger.i ...
- 基于Label studio实现UIE信息抽取智能标注方案,提升标注效率!
基于Label studio实现UIE信息抽取智能标注方案,提升标注效率! 项目链接见文末 人工标注的缺点主要有以下几点: 产能低:人工标注需要大量的人力物力投入,且标注速度慢,产能低,无法满足大规模 ...
- 基于SqlSugar的开发框架循序渐进介绍(25)-- 基于SignalR实现多端的消息通讯
基于ASP.NET Core SignalR 可以实现客户端和服务器之间进行即时通信.本篇随笔介绍一些SignalR的基础知识,以及结合对SqlSugar的开发框架的支持,实现SignalR的多端处理 ...