enumclassType{kEntering,kLeaving};structEvent{intday;intindex;Typetype;Event(intday,intindex,Typetype):day(day),index(index),type(type){}};classSolution{public:vector<int>amountPainted(vector<vector<int>>&paint){constintn=paint.size();constintminDay=(*ranges::min_element(paint,ranges::less{},[](constvector<int>&p){returnp[0];}))[0];constintmaxDay=(*ranges::max_element(paint,ranges::less{},[](constvector<int>&p){returnp[1];}))[1];vector<int>ans(n);vector<Event>events;// Stores the indices of paints that are available now.set<int>runningIndices;for(inti=0;i<n;++i){constintstart=paint[i][0];constintend=paint[i][1];events.emplace_back(start,i,Type::kEntering);// 1 := enteringevents.emplace_back(end,i,Type::kLeaving);// -1 := leaving}ranges::sort(events,[](constauto&a,constauto&b){returna.day<b.day;});inti=0;// events' indexfor(intday=minDay;day<maxDay;++day){while(i<events.size()&&events[i].day==day){if(events[i].type==Type::kEntering)runningIndices.insert(events[i].index);elserunningIndices.erase(events[i].index);++i;}if(!runningIndices.empty())++ans[*runningIndices.begin()];}returnans;}};
enumType{ENTERING,LEAVING}classEvent{publicintday;publicintindex;publicTypetype;publicEvent(intday,intindex,Typetype){this.day=day;this.index=index;this.type=type;}}classSolution{publicint[]amountPainted(int[][]paint){finalintn=paint.length;finalintminDay=Arrays.stream(paint).mapToInt(x->x[0]).min().getAsInt();finalintmaxDay=Arrays.stream(paint).mapToInt(x->x[1]).max().getAsInt();int[]ans=newint[n];// Stores the indices of paints that are available now.TreeSet<Integer>runningIndices=newTreeSet<>();List<Event>events=newArrayList<>();for(inti=0;i<n;++i){finalintstart=paint[i][0];finalintend=paint[i][1];events.add(newEvent(start,i,Type.ENTERING));// 1 := enteringevents.add(newEvent(end,i,Type.LEAVING));// -1 := leaving}Collections.sort(events,(a,b)->Integer.compare(a.day,b.day));inti=0;// events' indexfor(intday=minDay;day<maxDay;++day){while(i<events.size()&&events.get(i).day==day){if(events.get(i).type==Type.ENTERING)runningIndices.add(events.get(i).index);elserunningIndices.remove(events.get(i).index);++i;}if(!runningIndices.isEmpty())++ans[runningIndices.first()];}returnans;}}
fromsortedcontainersimportSortedListclassSolution:defamountPainted(self,paint:list[list[int]])->list[int]:minDay=min(sfors,einpaint)maxDay=max(efors,einpaint)ans=[0]*len(paint)# Stores the indices of paints that are available now.runningIndices=SortedList()events=[]# (day, index, type)fori,(start,end)inenumerate(paint):events.append((start,i,1))# 1 := enteringevents.append((end,i,-1))# -1 := leavingevents.sort()i=0# events' indexfordayinrange(minDay,maxDay):whilei<len(events)andevents[i][0]==day:day,index,type=events[i]iftype==1:runningIndices.add(index)else:runningIndices.remove(index)i+=1ifrunningIndices:ans[runningIndices[0]]+=1returnans