You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a triple booking.

A triple booking happens when three events have some non-empty intersection (i.e., some moment is common to all the three events.).

The event can be represented as a pair of integers start and end that represents a booking on the half-open interval [start, end), the range of real numbers x such that start &lt;= x &lt; end.

Implement the MyCalendarTwo class:


	MyCalendarTwo() Initializes the calendar object.
	boolean book(int start, int end) Returns true if the event can be added to the calendar successfully without causing a triple booking. Otherwise, return false and do not add the event to the calendar.


&nbsp;
Example 1:

Input
["MyCalendarTwo", "book", "book", "book", "book", "book", "book"]
[[], [10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55]]
Output
[null, true, true, true, false, true, true]

Explanation
MyCalendarTwo myCalendarTwo = new MyCalendarTwo();
myCalendarTwo.book(10, 20); // return True, The event can be booked. 
myCalendarTwo.book(50, 60); // return True, The event can be booked. 
myCalendarTwo.book(10, 40); // return True, The event can be double booked. 
myCalendarTwo.book(5, 15);  // return False, The event cannot be booked, because it would result in a triple booking.
myCalendarTwo.book(5, 10); // return True, The event can be booked, as it does not use time 10 which is already double booked.
myCalendarTwo.book(25, 55); // return True, The event can be booked, as the time in [25, 40) will be double booked with the third event, the time [40, 50) will be single booked, and the time [50, 55) will be double booked with the second event.


&nbsp;
Constraints:


	0 &lt;= start &lt; end &lt;= 109
	At most 1000 calls will be made to book.


class Node:
    def __init__(self, start, end, val):
        self.start = start
        self.end = end
        self.val = val
        self.left = None
        self.right = None
    
    def insertable(self, node):
        if node.end <= node.start:
            return True
        if node.end <= self.start:
            if not self.left:
                return True
            else:
                return self.left.insertable(node)
        elif node.start >= self.end:
            if not self.right:
                return True
            else:
                return self.right.insertable(node)
        else:
            if self.val == 1:
                leftS = min(self.start, node.start)
                leftE = max(self.start, node.start)
                rightS = min(self.end, node.end)
                rightE = max(self.end, node.end)
                if not self.left and not self.right:
                    return True
                elif not self.left:
                    return self.right.insertable(Node(rightS, rightE, 1))
                elif not self.right:
                    return self.left.insertable(Node(leftS, leftE, 1))
                else:
                    resL = self.left.insertable(Node(leftS, leftE, 1))
                    resR = self.right.insertable(Node(rightS, rightE, 1))
                    if resL and resR:
                        return True
                    return False
            else:
                return False

    def insert(self, node):
        if node.end <= node.start:
            return
        
        if node.end <= self.start:
            if not self.left:
                self.left = node
            else:
                self.left.insert(node)
        elif node.start >= self.end:
            if not self.right:
                self.right = node
            else:
                self.right.insert(node)
        else:
            leftS = min(self.start, node.start)
            leftE = max(self.start, node.start)
            rightS = min(self.end, node.end)
            rightE = max(self.end, node.end)
            self.val += 1
            self.start, self.end = leftE, rightS  
            if not self.left and not self.right:  
                self.left = Node(leftS, leftE, 1) if leftS < leftE else None
                self.right = Node(rightS, rightE, 1) if rightS < rightE else None
            elif not self.left:
                self.left = Node(leftS, leftE, 1) if leftS < leftE else None
                self.right.insert(Node(rightS, rightE, 1))
            elif not self.right:
                self.right = Node(rightS, rightE, 1) if rightS < rightE else None
                self.left.insert(Node(leftS, leftE, 1))
            else:
                self.left.insert(Node(leftS, leftE, 1))
                self.right.insert(Node(rightS, rightE, 1))
        return


class MyCalendarTwo:

    def __init__(self):
        self.root = None

    def book(self, start: int, end: int) -> bool:
        if not self.root:
            self.root = Node(start, end, 1)
            return True
        else:
            newNode = Node(start, end, 1)
            if self.root.insertable(newNode):
                self.root.insert(newNode)
                return True
            return False
class MyCalendarTwo {
    private List<int[]> bookings;
    private List<int[]> doubleBookings;
    
    public MyCalendarTwo() {
        bookings = new ArrayList<>();
        doubleBookings = new ArrayList<>();
    }
    
 
    public boolean book(int start, int end) {
        for(int[] b : doubleBookings)
        {
		//condition to check for the overlaping
            if(start < b[1] && end > b[0])
                return false;
        }
        
        for(int[] b : bookings)
        {
            if(start < b[1] && end > b[0]) {
                doubleBookings.add(new int[] {Math.max(start, b[0]), Math.min(end, b[1])});
            }
        }
        bookings.add(new int[]{start, end});
        return true;
    }
}
class MyCalendarTwo {
public:
    map<int,int>mpp;
    MyCalendarTwo() {

    }

    bool book(int start, int end) {
        mpp[start]++;
        mpp[end]--;
        int sum=0;
        for(auto it:mpp){
            sum+=it.second;
            if(sum>2){
                mpp[start]--;
                mpp[end]++;
                return false;
            }
        }
        return true;
    }
};
var MyCalendarTwo = function() {
  this.calendar = [];
  this.overlaps = [];
};

/** 
 * @param {number} start 
 * @param {number} end
 * @return {boolean}
 */
MyCalendarTwo.prototype.book = function(start, end) {
  for (let date of this.overlaps) {
    if (start < date[1] && end > date[0]) return false;
  }

  for (let date of this.calendar) {
    if (start < date[1] && end > date[0]) {
      this.overlaps.push([Math.max(date[0], start), Math.min(date[1], end)]);
    }
  }
  this.calendar.push([start, end]); 
  return true;
};
My Calendar II