用Go撸了个简单线段树练练手
package main
import "fmt"
//线段树节点
type Node struct {
L int
R int
Left *Node
Right *Node
Cover int
}
//建树
func (this *Node) build(l, r int) *Node {
this.L = l
this.R = r
this.Cover = 0
mid := (this.L + this.R) >> 1
if (l + 1) < r {
if this.L != mid {
NewLeftNode := Node{}
this.Left = NewLeftNode.build(this.L, mid)
}
if this.R != mid {
NewRightNode := Node{}
this.Right = NewRightNode.build(mid, this.R)
}
return this
} else {
return this
}
}
//插入
func (this *Node) Insert(l, r int) {
mid := (this.L + this.R) >> 1
if l == this.L && r == this.R {
//若插入的线段正好被包含,结束递归
this.Cover = 1
} else if r <= mid {
if this.Left != nil {
this.Left.Insert(l, r)
}
} else if l >= mid {
if this.Right != nil {
this.Right.Insert(l, r)
}
} else {
if this.Left !&