線段樹

來源:互聯網
上載者:User

標籤:des   blog   os   ar   for   div   sp   log   on   

 

object IntervalTree1 extends App{    val score = Array(1, 2, 3, 4, 5)    val commands = Array(        "Q 1 5", "U 3 6", "Q 3 4",        "Q 4 5", "U 2 9", "Q 1 5"    )    val (n, m) = (5, 6)    val iTree = new IntervalTree    var root: Tree = _    root = iTree.build(1, n)    //iTree.printTree(root)    commands.foreach( cmd => {        val tokens = cmd.split(" ")        val (c, a, b) = (tokens(0), tokens(1).toInt, tokens(2).toInt)        c match {            case "Q" => println(iTree.query(root, a, b))            case "U" => {score(a) = b; iTree.update(root, a, b)}        }    })    class Tree    {        var l: Tree = null        var r: Tree = null        var lv = 0        var rv = 0        var maxscore = 0    }    class IntervalTree    {        var pos = 0        var nodes = Array.fill(400010)(new Tree())        def build(left: Int, right: Int): Tree =        {            val node = nodes(pos); pos += 1            node.lv = left; node.rv = right;            if (left == right) {                node.maxscore = Math.max(score(left - 1), score(right - 1))                node            }            else {                val mid = (left + right) >> 1                node.l = build(left, mid)                node.r = build(mid + 1, right)                node.maxscore = Math.max(node.l.maxscore, node.r.maxscore)            }            node        }        def query(root: Tree, left: Int, right: Int): Int =        {            if (left == root.lv && right == root.rv) return root.maxscore            val mid = (root.lv + root.rv) >> 1            mid match {                case m if (right <= m) => query(root.l, left, right)                case m if (left > m) => query(root.r, left, right)                case _ => Math.max(query(root.l, left, mid), query(root.r, mid + 1, right))            }        }        def update(root: Tree, value: Int, newValue: Int): Int =        {            val mid = (root.lv + root.rv) >> 1            if (value == root.lv &&  value == root.rv) {                root.maxscore = newValue                return root.maxscore            }            val ret = mid match {                case m if value <= m => update(root.l, value,  newValue)                case m if value > m => update(root.r, value, newValue)            }            root.maxscore = Math.max(root.maxscore, ret)            root.maxscore        }        def printTree(node: Tree)        {            if (node != null) {                println(s"[${node.lv},${node.rv}]")                printTree(node.l)                printTree(node.r)            }        }    }}

  

線段樹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.