#206 Interval Sum

本文介绍了一种使用段树解决区间求和问题的方法。通过构建段树并进行修改以匹配输入数组,然后针对每个查询计算指定区间的元素总和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述:

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.

 Notice

We suggest you finish problem Segment Tree BuildSegment Tree Query and Segment Tree Modify first.

Example

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

Challenge 

O(logN) time for each query

题目思路:

这题用segment tree来做就是一个套路(虽然代码长了点,但是还是很好懂的):

1. 建一个segment tree

2. 把数组A的信息写入tree中

3. 一个一个query

Mycode(AC = 393ms):

/**
 * Definition of Interval:
 * classs Interval {
 *     int start, end;
 *     Interval(int start, int end) {
 *         this->start = start;
 *         this->end = end;
 *     }
 */
class SegmentTree {
public:
    SegmentTree *left, *right;
    int start, end;
    long long sum;
    SegmentTree(int start, int end, long long sum) {
        this->start = start;
        this->end = end;
        this->sum = sum;
        this->left = NULL;
        this->right = NULL;
    }
};

class Solution { 
public:
    /**
     *@param A, queries: Given an integer array and an query list
     *@return: The result list
     */
    SegmentTree *buildSegmentTree(int start, int end) {
        if (start > end) {
            return NULL;
        }
        else if (start == end) {
            return new SegmentTree(start, end, 0);
        }
        else {
            SegmentTree *root = new SegmentTree(start, end, 0);
            root->left = buildSegmentTree(start, (start + end) / 2);
            root->right = buildSegmentTree((start + end) / 2 + 1, end);
            
            return root;
        }
    }
    
    void modifySegmentTree(SegmentTree* root, vector<int> &A) {
        if (root == NULL) {
            return;
        }
        else if (root->start == root->end) {
            root->sum = (long long)A[root->start];
        }
        else {
            modifySegmentTree(root->left, A);
            modifySegmentTree(root->right, A);
            
            root->sum = root->left->sum + root->right->sum;
        }
    }
    
    long long querySegmentTree(SegmentTree* root, int start, int end) {
        if (root == NULL || start > end) {
            return 0;
        }
        else if (root->start == start && root->end == end) {
            return root->sum;
        }
        else {
            long long left = querySegmentTree(root->left,
                                        max(start, root->left->start),
                                        min(end, root->left->end));
            long long right = querySegmentTree(root->right,
                                        max(start, root->right->start),
                                        min(end, root->right->end));
            return left + right;
        }
    }
     
    vector<long long> intervalSum(vector<int> &A, vector<Interval> &queries) {
        // write your code here
        vector<long long> ans;
        if (queries.size() == 0) return ans;
        
        ans.resize(queries.size());
        SegmentTree *root = buildSegmentTree(0, A.size() - 1);
        modifySegmentTree(root, A);
        
        for (int i = 0; i < queries.size(); i++) {
            ans[i] = querySegmentTree(root, queries[i].start, queries[i].end);
        }
        
        return ans;
    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值