class Solution:
    def splitPainting(self, segments: List[List[int]]) -> List[List[int]]:
        mix, res, last_i = DefaultDict(int), [], 0
        for start, end, color in segments:
            mix[start] += color
            mix[end] -= color
        for i in sorted(mix.keys()):
            if last_i in mix and mix[last_i]:
                res.append([last_i, i, mix[last_i]])
                mix[i] += mix[last_i]
            last_i = i
        return res
class Solution {
    //class for segments 
    class Seg{
        int val,end;
        int color;
        boolean isStart;
        public Seg(int val,int end,int color, boolean isStart){
            this.val = val;
            this.end = end;
            this.color = color;
            this.isStart = isStart; 
        }
        public String toString(){
            return "[" + val+" "+end+" "+color+" "+isStart+"]";
        }
    }
    public List<List<Long>> splitPainting(int[][] segments) {
        List<List<Long>> res = new ArrayList();
        
        List<Seg> list = new ArrayList();
        
        //making a list of segments
        for(int[] segment : segments){
            list.add(new Seg(segment[0],segment[1],segment[2],true));
            list.add(new Seg(segment[1],segment[1],segment[2],false));            
        }
        
        //Sorting the segments
        Collections.sort(list,(a,b)->{return a.val-b.val;});
        
        //System.out.println(list);
        
        //Iterating over list to combine the elements
        for(Seg curr: list){
            int len = res.size();
            if(curr.isStart){
                //if the segment is starting there could be three ways 
                if(res.size()>0 && res.get(len-1).get(0)==curr.val){
                    //if starting point of two things is same
                    List<Long> temp = res.get(len-1);
                    temp.set(1,Math.max(temp.get(1),curr.end));
                    temp.set(2,(long)(temp.get(2)+curr.color));
                }else if(res.size()>0 && res.get(len-1).get(1)>curr.val){
                    //if there is a start in between create a new segment of different color 
                    List<Long> prev = res.get(len-1);
                    prev.set(1,(long)curr.val);
                    List<Long> temp = new ArrayList();
                    temp.add((long)curr.val); temp.add((long)curr.end); temp.add((long)(prev.get(2)+curr.color));
                    res.add(temp);
                }else{
                    //Add a new value if nothing is present in result
                    List<Long> temp = new ArrayList();
                    temp.add((long)curr.val); temp.add((long)curr.end); temp.add((long)curr.color);
                    res.add(temp);
                }
            }else{
                if(res.size()>0 && res.get(len-1).get(0)==curr.val){
                    //if ending point of 2 segments is same
                    Long prevColor = res.get(len-1).get(2);
                    res.get(len-1).set(2,(long)(prevColor-curr.color));
                }
                else if(res.size()>0 && res.get(len-1).get(1)>curr.val){
                    //if there is a ending in between create a new segment of different color 
                    Long prevColor = res.get(len-1).get(2);
                    Long prevEnd = res.get(len-1).get(1);
                    res.get(len-1).set(1,(long)curr.val);
                    
                    List<Long> temp = new ArrayList();
                    temp.add((long)curr.val); temp.add((long)prevEnd); temp.add((long)(prevColor-curr.color));
                    res.add(temp);
                }
            }
            //System.out.println(res+" "+curr);
            
        }
        //System.out.println(res);
        return res; 
        
    }
}
Describe the Painting
There is a long and thin painting that can be represented by a number line. The painting was painted with multiple overlapping segments where each segment was painted with a unique color. You are given a 2D integer array segments, where segments[i] = [starti, endi, colori] represents the half-closed segment [starti, endi) with colori as the color.

The colors in the overlapping segments of the painting were mixed when it was painted. When two or more colors mix, they form a new color that can be represented as a set of mixed colors.


	For example, if colors 2, 4, and 6 are mixed, then the resulting mixed color is {2,4,6}.


For the sake of simplicity, you should only output the sum of the elements in the set rather than the full set.

You want to describe the painting with the minimum number of non-overlapping half-closed segments of these mixed colors. These segments can be represented by the 2D array painting where painting[j] = [leftj, rightj, mixj] describes a half-closed segment [leftj, rightj) with the mixed color sum of mixj.


	For example, the painting created with segments = [[1,4,5],[1,7,7]] can be described by painting = [[1,4,12],[4,7,7]] because:

	
		[1,4) is colored {5,7} (with a sum of 12) from both the first and second segments.
		[4,7) is colored {7} from only the second segment.
	
	


Return the 2D array painting describing the finished painting (excluding any parts that are not painted). You may return the segments in any order.

A half-closed segment [a, b) is the section of the number line between points a and b including point a and not including point b.

&nbsp;
Example 1:

Input: segments = [[1,4,5],[4,7,7],[1,7,9]]
Output: [[1,4,14],[4,7,16]]
Explanation: The painting can be described as follows:
- [1,4) is colored {5,9} (with a sum of 14) from the first and third segments.
- [4,7) is colored {7,9} (with a sum of 16) from the second and third segments.


Example 2:

Input: segments = [[1,7,9],[6,8,15],[8,10,7]]
Output: [[1,6,9],[6,7,24],[7,8,15],[8,10,7]]
Explanation: The painting can be described as follows:
- [1,6) is colored 9 from the first segment.
- [6,7) is colored {9,15} (with a sum of 24) from the first and second segments.
- [7,8) is colored 15 from the second segment.
- [8,10) is colored 7 from the third segment.


Example 3:

Input: segments = [[1,4,5],[1,4,7],[4,7,1],[4,7,11]]
Output: [[1,4,12],[4,7,12]]
Explanation: The painting can be described as follows:
- [1,4) is colored {5,7} (with a sum of 12) from the first and second segments.
- [4,7) is colored {1,11} (with a sum of 12) from the third and fourth segments.
Note that returning a single segment [1,7) is incorrect because the mixed color sets are different.


&nbsp;
Constraints:


	1 &lt;= segments.length &lt;= 2 * 104
	segments[i].length == 3
	1 &lt;= starti &lt; endi &lt;= 105
	1 &lt;= colori &lt;= 109
	Each colori is distinct.


/**
 * @param {number[][]} segments
 * @return {number[][]}
 */
var splitPainting = function(segments) {
    const arr = [];
    segments.forEach(([start, end, val])=>{
        arr.push([start, val]);
        arr.push([end, -val]);
    });
    arr.sort((i,j)=>i[0]-j[0]);

    const ans = [];
    let currVal = 0, prevTime;
    arr.forEach(([time, val])=>{
        if(prevTime !== undefined && currVal && prevTime !== time) ans.push([prevTime, time, currVal]);
        currVal += val;
        prevTime = time;
    })

    return ans;
};
typedef long long ll;

class Solution {
public:
    vector<vector<long long>> splitPainting(vector<vector<int>>& segments) {
        vector<vector<ll> > ans;
        map<int,ll> um;
        
        for(auto s : segments){
            um[s[0]] += s[2];
            um[s[1]] -= s[2];
        }
        bool flag = false;
        pair<int,ll> prev;
        ll curr = 0;
        
        for(auto x : um){
            if(flag == false){
                prev = x;
                curr += x.second;
                flag = true;
                continue;
            }
            
            vector<ll> v = {prev.first,x.first,curr};
            prev = x;
            if(curr)
                ans.push_back(v);
            curr += x.second;
            
        }
        return ans;
    }
};