dengxinyi 6 年之前
父節點
當前提交
371e5f96f7
共有 2 個文件被更改,包括 136 次插入0 次删除
  1. 40 0
      oj/algorithms/quicksort/partition.go
  2. 96 0
      oj/algorithms/segmenttree/segmenttree.go

+ 40 - 0
oj/algorithms/quicksort/partition.go

@@ -0,0 +1,40 @@
+package main
+
+import (
+	"fmt"
+	"math/rand"
+	"time"
+)
+
+func threeWayParttion(nums []int, target int) {
+	nextLess, nextScan, nextLarger := 0, 0, len(nums)-1
+	for nextScan <= nextLarger {
+		if nums[nextScan] < target {
+			swap(nums, nextScan, nextLess)
+			nextScan++
+			nextLess++
+		} else if target < nums[nextScan] {
+			swap(nums, nextScan, nextLarger)
+			nextLarger--
+		} else {
+			nextScan++
+		}
+	}
+}
+
+func swap(nums []int, i, j int) {
+	nums[i], nums[j] = nums[j], nums[i]
+}
+
+func main() {
+	var n int
+	fmt.Scan(&n)
+	nums := make([]int, n)
+	rand.Seed(time.Now().Unix())
+	for i := range nums {
+		nums[i] = rand.Intn(3) + 1
+	}
+	fmt.Println(nums)
+	threeWayParttion(nums, 2)
+	fmt.Println(nums)
+}

+ 96 - 0
oj/algorithms/segmenttree/segmenttree.go

@@ -0,0 +1,96 @@
+package main
+
+import (
+	"fmt"
+)
+
+type node struct {
+	l     int
+	r     int
+	left  *node
+	right *node
+	val   int
+	inc   int // Lazy
+}
+
+func (n node) mid() int {
+	return n.l + (n.r-n.l)/2
+}
+
+var tree []node
+var tid int
+
+func newTree(n int) *node {
+	tree = make([]node, 2*n)
+	tid = 0
+	root := &tree[0]
+	root.buildTree(1, n)
+	return root
+}
+
+func (root *node) buildTree(l, r int) {
+	root.l = l
+	root.r = r
+	if l == r {
+		return
+	}
+	tid++
+	root.left = &tree[tid]
+	tid++
+	root.right = &tree[tid]
+	m := l + (r-l)/2
+	root.left.buildTree(l, m)
+	root.right.buildTree(m+1, r)
+}
+
+func (root *node) update(l, r, val int) {
+	if root.l == l && root.r == r {
+		root.inc += val
+		return
+	}
+	root.val += (r - l + 1) * val
+	if r <= root.mid() {
+		root.left.update(l, r, val)
+	} else if root.mid()+1 <= l {
+		root.right.update(l, r, val)
+	} else {
+		root.left.update(l, root.mid(), val)
+		root.right.update(root.mid()+1, r, val)
+	}
+}
+
+func (root *node) query(l, r int) int {
+	if root.l == l && root.r == r {
+		return root.val + (r-l+1)*root.inc
+	}
+	if root.inc != 0 {
+		root.val += (root.r - root.l + 1) * root.inc   // PushDown
+		root.left.update(root.l, root.mid(), root.inc) // !!the RANGE is important
+		root.right.update(root.mid()+1, root.r, root.inc)
+	}
+	root.inc = 0 // Finished update
+	if r <= root.mid() {
+		return root.left.query(l, r)
+	} else if root.mid()+1 <= l {
+		return root.right.query(l, r)
+	} else {
+		return root.left.query(l, root.mid()) + root.right.query(root.mid()+1, r)
+	}
+}
+
+func main() {
+	var n int
+	fmt.Scan(&n)
+	root := newTree(n)
+	for i := 1; i <= n; i++ {
+		root.update(i, i, i)
+	}
+	fmt.Println("Sum of 1 ~", n, "is", root.query(1, n))
+	var l, r, val int
+	fmt.Print("Update: ")
+	fmt.Scan(&l, &r, &val)
+	root.update(l, r, val)
+	fmt.Print("Query: ")
+	fmt.Scan(&l, &r)
+	fmt.Println(root.query(l, r))
+}