• 307. Range Sum Query


    /*
     * 307. Range Sum Query - Mutable
     * 2016-7-3 by Mingyang
     * 丧心病狂的Segment Tree,无语了
     */
    class NumArray {
        class SegmentTreeNode {
            int start, end;
            SegmentTreeNode left, right;
            int sum;
    
            public SegmentTreeNode(int start, int end) {
                this.start = start;
                this.end = end;
                this.left = null;
                this.right = null;
                this.sum = 0;
            }
        }
          
        SegmentTreeNode root = null;
       
        public NumArray(int[] nums) {
            root = buildTree(nums, 0, nums.length-1);
        }
    
        private SegmentTreeNode buildTree(int[] nums, int start, int end) {
            if (start > end) {
                return null;
            } else {
                SegmentTreeNode ret = new SegmentTreeNode(start, end);
                if (start == end) {
                    ret.sum = nums[start];
                } else {
                    int mid = start  + (end - start) / 2;             
                    ret.left = buildTree(nums, start, mid);
                    ret.right = buildTree(nums, mid + 1, end);
                    ret.sum = ret.left.sum + ret.right.sum;
                }         
                return ret;
            }
        }
       
        void update(int i, int val) {
            update(root, i, val);
        }
       
        void update(SegmentTreeNode root, int pos, int val) {
            if (root.start == root.end) {
               root.sum = val;
            } else {
                int mid = root.start + (root.end - root.start) / 2;
                if (pos <= mid) {
                     update(root.left, pos, val);
                } else {
                     update(root.right, pos, val);
                }
                root.sum = root.left.sum + root.right.sum;
            }
        }
    
        public int sumRange(int i, int j) {
            return sumRange(root, i, j);
        }
        
        public int sumRange(SegmentTreeNode root, int start, int end) {
            if (root.end == end && root.start == start) {
                return root.sum;
            } else {
                int mid = root.start + (root.end - root.start) / 2;
                if (end <= mid) {
                    return sumRange(root.left, start, end);
                } else if (start >= mid+1) {
                    return sumRange(root.right, start, end);
                }  else {    
                    return sumRange(root.right, mid+1, end) + sumRange(root.left, start, mid);
                }
            }
        }
    }
  • 相关阅读:
    Android ViewPager使用详解
    Android开发 侧边滑动菜单栏SlidingMenu结合Fragment
    ArrayList
    Gstreamer中加入�x265编解码器
    python fabric实现远程操作和部署
    java.text.NumberFormat使用方法
    Delphi 2007体验!
    Codeforces 360C Levko and Strings dp
    FindWindow使用方法
    C++ Primer 学习笔记_87_用于大型程序的工具 --异常处理
  • 原文地址:https://www.cnblogs.com/zmyvszk/p/5640105.html
Copyright © 2020-2023  润新知