General templates for sliding on non-overlapping intervals
Use the following functions for sliding window problems having non-overlapping intervals regardless of whether each interval has a cost(weight) or not.CodeCompl
Use the following functions for sliding window problems having non-overlapping intervals regardless of whether each interval has a cost(weight) or not.

# Code
```cpp []
#define ll long long
#define cost(x) ((vec[x].size()>2)?vec[x][2]:1)
class Solution {
public:
    ll slide(vector<vector<int>>& vec, int k){ // non-overlapping intervals
        sort(vec.begin(),vec.end());
        ll curr=0,ans=0,n=vec.size(),partial;
        for(int i=0,j=0;i<n;++i){
            int l=vec[i][0],r=vec[i][1],windowLen=l+k-1;partial=0;
            while(j<n&&vec[j][1]<=windowLen)curr+=1LL*cost(j)*(vec[j][1]-vec[j++][0]+1);
            if(j<n)partial=1LL*max(0,windowLen-vec[j][0]+1)*cost(j);
            ans=max(ans,curr+partial);
            // cout<<i<<" "<<j<<" "<<curr<<" "<<partial<<" "<<ans<<endl;
            curr-=1LL*(r-l+1)*cost(i);
        }
        return ans;
    }

    ll reverseSlide(vector<vector<int>>& vec, int k){ // non-overlapping intervals
        sort(vec.begin(),vec.end());
        ll curr=0,ans=0,n=vec.size(),partial;
        for(int i=n-1,j=n-1;i>=0;--i){
            int l=vec[i][0],r=vec[i][1],windowLen=r-k+1;partial=0;
            while(j>=0&&windowLen<=vec[j][0])curr+=1LL*cost(j)*(vec[j][1]-vec[j--][0]+1);
            if(j>=0)partial=1LL*max(0,vec[j][1]-windowLen+1)*cost(j);
            ans=max(ans,curr+partial);
            // // cout<<i<<" "<<j<<" "<<curr<<" "<<partial<<" "<<ans<<endl;
            curr-=1LL*(r-l+1)*cost(i);
        }
        return ans;
    }

    ll maximumCoins(vector<vector<int>>& coins, int k) {
        return max(slide(coins,k),reverseSlide(coins,k));
    }
};
```
```python3 []
class Solution:
    def slide(self,lst,k):
        lst.sort()
        n=len(lst)
        curr=ans=j=0
        def cost(x):return lst[x][2] if len(lst[x])>2 else 1
        for i in range(n):
            l,r=lst[i][0],lst[i][1]
            windowLen=l+k-1
            while j<n and lst[j][1]<=windowLen:
                curr+=cost(j)*(lst[j][1]-lst[j][0]+1)
                j+=1
            partial=max(0,windowLen-lst[j][0]+1)*cost(j) if j<n else 0
            ans=max(ans,curr+partial)
            curr-=(r-l+1)*cost(i)
        return ans
            
    def reverseSlide(self,lst,k):
        lst.sort()
        n=len(lst)
        j=n-1
        curr=ans=0
        def cost(x):return lst[x][2] if len(lst[x])>2 else 1
        for i in reversed(range(n)):
            l,r=lst[i][0],lst[i][1]
            windowLen=r-k+1
            while j>=0 and windowLen<=lst[j][0]:
                curr+=cost(j)*(lst[j][1]-lst[j][0]+1)
                j-=1
            partial=max(0,lst[j][1]-windowLen+1)*cost(j) if j>=0 else 0
            ans=max(ans,curr+partial)
            curr-=(r-l+1)*cost(i)
        return ans
            
    def maximumCoins(self, coins: List[List[int]], k: int) -> int:
        return max(self.slide(coins,k),self.reverseSlide(coins,k))
```


# Complexity
- Time complexity: $$O(nlogn + n)$$ ~ $$O(nlogn)$$
    - slide - $$O(nlogn+2*n)$$
    - reverseSlide - $$O(nlogn+2*n)$$

- Space complexity: $$O(1)$$

## Bonus point: 
**just use the `slide` function** to solve this problem -  [2271. Maximum White Tiles Covered by a Carpet](https://leetcode.com/problems/maximum-white-tiles-covered-by-a-carpet/description/)
['Sliding Window', 'Sorting', 'C++', 'Python3']