<p>We have an array of integers, <code>nums</code>, and an array of <code>requests</code> where <code>requests[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>. The <code>i<sup>th</sup></code> request asks for the sum of <code>nums[start<sub>i</sub>] + nums[start<sub>i</sub> + 1] + ... + nums[end<sub>i</sub> - 1] + nums[end<sub>i</sub>]</code>. Both <code>start<sub>i</sub></code> and <code>end<sub>i</sub></code> are <em>0-indexed</em>.</p>

<p>Return <em>the maximum total sum of all requests <strong>among all permutations</strong> of</em> <code>nums</code>.</p>

<p>Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], requests = [[1,3],[0,1]]
<strong>Output:</strong> 19
<strong>Explanation:</strong> One permutation of nums is [2,1,3,4,5] with the following result: 
requests[0] -&gt; nums[1] + nums[2] + nums[3] = 1 + 3 + 4 = 8
requests[1] -&gt; nums[0] + nums[1] = 2 + 1 = 3
Total sum: 8 + 3 = 11.
A permutation with a higher total sum is [3,5,4,2,1] with the following result:
requests[0] -&gt; nums[1] + nums[2] + nums[3] = 5 + 4 + 2 = 11
requests[1] -&gt; nums[0] + nums[1] = 3 + 5  = 8
Total sum: 11 + 8 = 19, which is the best that you can do.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4,5,6], requests = [[0,1]]
<strong>Output:</strong> 11
<strong>Explanation:</strong> A permutation with the max total sum is [6,5,4,3,2,1] with request sums [11].</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4,5,10], requests = [[0,2],[1,3],[1,1]]
<strong>Output:</strong> 47
<strong>Explanation:</strong> A permutation with the max total sum is [4,10,5,3,2,1] with request sums [19,18,10].</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == nums.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums[i]&nbsp;&lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= requests.length &lt;=&nbsp;10<sup>5</sup></code></li>
	<li><code>requests[i].length == 2</code></li>
	<li><code>0 &lt;= start<sub>i</sub>&nbsp;&lt;= end<sub>i</sub>&nbsp;&lt;&nbsp;n</code></li>
</ul>

void update(int ind ,int low,int high,int l,int r,vector<long long>& seg,vector<long long>& lazy){
    if(lazy[ind]!=0){
        seg[ind]+=(high-low+1)*lazy[ind];
        if(low!=high){
            lazy[2*ind+1]+=lazy[ind];
            lazy[2*ind+2]+=lazy[ind];
        }
        lazy[ind]=0;
    }
    if(low>=l&&high<=r){
        seg[ind]+=(high-low+1);
        if(low!=high){
            lazy[2*ind+1]++;
            lazy[2*ind+2]++;
        }
    }else if(low>r||high<l){
        return;
    }else{
        int mid=(low+high)/2;
        update(2*ind+1,low,mid,l,r,seg,lazy);
        update(2*ind+2,mid+1,high,l,r,seg,lazy);
        seg[ind]=seg[2*ind+1]+seg[2*ind+2];
    }
}

long long query(int ind,int low,int high,int l,int r,vector<long long>& seg,vector<long long>& lazy){
    if(lazy[ind]!=0){
        seg[ind]+=(high-low+1)*lazy[ind];
        if(low!=high){
            lazy[2*ind+1]+=lazy[ind];
            lazy[2*ind+2]+=lazy[ind];
        }
        lazy[ind]=0;
    }
    if(low>r||high<l){
        return 0;
    }
    if(low>=l&&high<=r){
        return seg[ind];
    }else{
        int mid=(low+high)/2;
        long long left=query(2*ind+1,low,mid,l,r,seg,lazy);
        long long right=query(2*ind+2,mid+1,high,l,r,seg,lazy);
        return left+right;
    }
}

class Solution {
public:
    int maxSumRangeQuery(vector<int>& nums, vector<vector<int>>& requests) {
        int n=nums.size();
        // vector<long long> seg(4*n,0);
        // vector<long long> lazy(4*n,0);
        // for(int i=0;i<requests.size();i++){
        //     update(0,0,n-1,requests[i][0],requests[i][1],seg,lazy);
        // }
        vector<long long> temp(n,0);
        for(int i=0;i<requests.size();i++){
            int l=requests[i][0];
            int r=requests[i][1];
            temp[l]++;
            if(r+1<n){
                temp[r+1]--;
            }
        }
        for(int i=1;i<n;i++){
            temp[i]+=temp[i-1];
        }
        sort(temp.begin(),temp.end(),greater<int>());
        sort(nums.begin(),nums.end(),greater<int>());
        long long ans=0;
        for(int i=0;i<n;i++){
            ans+=(temp[i]*nums[i])%1000000007;
            ans=ans%1000000007;
        }
        return ans;
    }
};
124945