-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.py
42 lines (35 loc) · 1.05 KB
/
Solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Node:
def __init__(self):
self.tag = 0
self.tot = 0
self.left = None
self.right = None
def update(self, l, r, a, b):
if self.tag == 1:
return
mid = (a + b) >> 1
if l == a and r == b:
self.tag = 1
self.tot = b - a + 1
return
if not self.left:
self.left = Node()
if not self.right:
self.right = Node()
if mid >= l:
self.left.update(l, min(mid, r), a, mid)
if mid + 1 <= r:
self.right.update(max(mid + 1, l), r, mid + 1, b)
self.tag = 0
self.tot = self.left.tot + self.right.tot
class CountIntervals:
def __init__(self):
self.tree = Node()
def add(self, left: int, right: int) -> None:
self.tree.update(left, right, 0, 1000000010)
def count(self) -> int:
return self.tree.tot
# Your CountIntervals object will be instantiated and called as such:
# obj = CountIntervals()
# obj.add(left,right)
# param_2 = obj.count()