lintcode Interval Sum

来源:互联网 发布:db2 执行sql 编辑:程序博客网 时间:2024/05/17 02:10
/** * Definition of Interval: * classs Interval { *     int start, end; *     Interval(int start, int end) { *         this->start = start; *         this->end = end; *     } */class IntervalTreeNode {  public:      int start, end;      long long  val;      IntervalTreeNode *left, *right;      IntervalTreeNode(int start, int end) {          this->start = start, this->end = end;          this->left = this->right = NULL;      }  };  IntervalTreeNode * subBuild(int start, int end,vector<int>&A) {        // write your code here        IntervalTreeNode *root=new IntervalTreeNode(start,end);        if(start==end){            root->val=A[start];            return root;        }        int mid=(start+end)/2;        root->left= subBuild(start,mid,A);        root->right= subBuild(mid+1,end,A);        root->val=root->left->val+root->right->val;        return root;    }    IntervalTreeNode * build(vector<int>&A,int start, int end) {        // write your code here        if(start>end)return NULL;        return subBuild(start,end,A);    }      class Solution { public:    long long subQuery(IntervalTreeNode*node,int start,int end){         if(!node||node->start>end||node->end<start){             return 0;         }         if(start<=node->start&&end>=node->end){             return node->val;         }         else return subQuery(node->left,start,end)+                subQuery(node->right,start,end);     }    /**     *@param A, queries: Given an integer array and an query list     *@return: The result list     */    vector<long long> intervalSum(vector<int> &A, vector<Interval> &queries) {        // write your code here        vector<long long>res;        int size=A.size();        if(size==0)return res;        IntervalTreeNode*root= build(A,0,size-1);        int qsize=queries.size();        for(int i=0;i<qsize;i++){            Interval &inter=queries[i];            res.push_back(subQuery(root,inter.start,inter.end));        }        return res;            }};
<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;">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 <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6000003814697px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">[start, end]</code>. For each query, calculate the sum number between index start and end in the given array, return the result list.</p><div class="line line-dashed" style="box-sizing: border-box; height: 2px; margin: 10px 0px; font-size: 0px; overflow: hidden; border-width: 1px 0px 0px; border-style: dashed; border-top-color: rgb(234, 237, 239); color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"></div><span class="text-sm" style="box-sizing: border-box; color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif;">Have you met this question in a real interview?</span><span style="color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"> </span><div class="btn-group" data-toggle="buttons" style="box-sizing: border-box; position: relative; display: inline-block; vertical-align: middle; color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"><div class="btn btn-white btn-xs" id="yes" style="box-sizing: border-box; display: inline-block; padding: 1px 5px; margin-bottom: 0px; font-size: 12px; line-height: 1.5; text-align: center; white-space: nowrap; vertical-align: middle; cursor: pointer; -webkit-user-select: none; border: 1px solid rgba(150, 160, 180, 0.298039); border-radius: 3px; box-shadow: rgba(0, 0, 0, 0.0470588) 0px -1px 1px inset; position: relative; float: left; margin-left: 0px; background-image: none; background-clip: padding-box;">Yes</div></div><span style="color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"></span><span style="color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"></span><span style="color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"></span><div class="m-t-lg m-b-lg" style="box-sizing: border-box; margin-top: 20px; margin-bottom: 20px; color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"><span style="box-sizing: border-box;">Example</span><div class="m-t-sm" style="box-sizing: border-box; margin-top: 10px;"><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">For array <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6000003814697px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">[1,2,7,8,5]</code>, and queries <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6000003814697px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">[(0,4),(1,2),(2,4)]</code>, return <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6000003814697px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">[23,9,20]</code></p></div></div><div class="m-t-lg m-b-lg" style="box-sizing: border-box; margin-top: 20px; margin-bottom: 20px; color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"><span style="box-sizing: border-box;">Note</span><div class="m-t-sm" style="box-sizing: border-box; margin-top: 10px;"><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">We suggest you finish problem <a target=_blank href="http://www.lintcode.com/problem/segment-tree-build/" title="Segment Tree Build" style="box-sizing: border-box; color: rgb(87, 87, 87); text-decoration: none; background: transparent;">Segment Tree Build</a>, <a target=_blank href="http://www.lintcode.com/problem/segment-tree-query/" title="Segment Tree Query" style="box-sizing: border-box; color: rgb(87, 87, 87); text-decoration: none; background: transparent;">Segment Tree Query</a> and <a target=_blank href="http://lintcode.com/en/problem/segment-tree-modify/" title="Segment Tree Modify" style="box-sizing: border-box; color: rgb(87, 87, 87); text-decoration: none; background: transparent;">Segment Tree Modify</a> first.</p></div></div><div class="m-t-lg m-b-lg" style="box-sizing: border-box; margin-top: 20px; margin-bottom: 20px; color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"><span style="box-sizing: border-box;">Challenge</span><div class="m-t-sm" style="box-sizing: border-box; margin-top: 10px;"><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">O(logN) time for each query</p></div></div>


0 0
原创粉丝点击