lintcode-medium-Interval Sum

Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers [start, end]. For each query, calculate the sum number between index start and end in the given array, return the result list.

 

For array [1,2,7,8,5], and queries [(0,4),(1,2),(2,4)], return[23,9,20]

 

/**
 * Definition of Interval:
 * public classs Interval {
 *     int start, end;
 *     Interval(int start, int end) {
 *         this.start = start;
 *         this.end = end;
 *     }
 */
public class Solution {
    /**
     *@param A, queries: Given an integer array and an query list
     *@return: The result list
     */
    public ArrayList<Long> intervalSum(int[] A, ArrayList<Interval> queries) {
        // write your code here
        
        ArrayList<Long> result = new ArrayList<Long>();
        
        if(A == null || A.length == 0 || queries == null || queries.size() == 0)
            return result;
        
        SegmentTreeNode root = build(0, A.length - 1);
        for(int i = 0; i < A.length; i++)
            modify(root, i, (long) A[i]);
        
        for(Interval inter: queries){
            result.add(query(root, inter.start, inter.end));
        }
        
        return result;
    }
    
    class SegmentTreeNode{
        int start;
        int end;
        long sum;
        
        SegmentTreeNode left;
        SegmentTreeNode right;
        
        public SegmentTreeNode(int start, int end){
            this.start = start;
            this.end = end;
            this.sum = 0;
            
            this.left = null;
            this.right = null;
        }
    }
    
    public SegmentTreeNode build(int start, int end){
        if(start > end)
            return null;
        
        if(start == end)
            return new SegmentTreeNode(start, end);
        
        SegmentTreeNode root = new SegmentTreeNode(start, end);
        
        int mid = start + (end - start) / 2;
        SegmentTreeNode left = build(start, mid);
        SegmentTreeNode right = build(mid + 1, end);
        
        root.left = left;
        root.right = right;
        
        return root;
    }
    
    public void modify(SegmentTreeNode root, int index, long value){
        if(index > root.end || index < root.start)
            return;
        
        if(root.start == root.end && root.start == index){
            root.sum = value;
            return;
        }
        
        int mid = root.start + (root.end - root.start) / 2;
        
        if(index <= mid)
            modify(root.left, index, value);
        else
            modify(root.right, index, value);
        
        root.sum = root.left.sum + root.right.sum;
        return;
    }
    
    public long query(SegmentTreeNode root, int start, int end){
        if(start > root.end || end < root.start)
            return 0;
        
        if(root.start == start && root.end == end)
            return root.sum;
        
        long leftsum = 0;
        long rightsum = 0;
        
        int mid = root.start + (root.end - root.start) / 2;
        
        if(start <= mid){
            if(end <= mid){
                leftsum = query(root.left, start, end);
            }
            else{
                leftsum = query(root.left, start, mid);
            }
        }
        
        if(end > mid){
            if(start > mid){
                rightsum = query(root.right, start, end);
            }
            else{
                rightsum = query(root.right, mid + 1, end);
            }
        }
        
        return leftsum + rightsum;
    }
}

 

转载于:https://www.cnblogs.com/goblinengineer/p/5313947.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值