You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0700-0799/0715.Range Module/README_EN.md
+158-2
Original file line number
Diff line number
Diff line change
@@ -46,14 +46,29 @@ rangeModule.queryRange(16, 17); // return True, (The number 16 in [16, 17) is st
46
46
47
47
## Solutions
48
48
49
-
Segment Tree.
49
+
**Solution 1: Segment Tree**
50
+
51
+
According to the problem description, we need to maintain a set of intervals, supporting operations of interval addition, deletion, and query. For the addition and deletion of intervals, we can use a segment tree to maintain the set of intervals.
52
+
53
+
The segment tree divides the entire interval into multiple non-continuous sub-intervals, the number of which does not exceed $\log(width)$. To update the value of an element, only $\log(width)$ intervals need to be updated, and these intervals are all included in a large interval containing the element. When modifying the interval, we need to use **lazy propagation** to ensure efficiency.
54
+
55
+
- Each node of the segment tree represents an interval;
56
+
- The segment tree has a unique root node, representing the entire statistical range, such as $[1,N]$;
57
+
- Each leaf node of the segment tree represents an elementary interval of length $1$, $[x,x]$;
58
+
- For each internal node $[l,r]$, its left child is $[l,mid]$, and the right child is $[mid+1,r]$, where $mid=⌊(l+r)/2⌋$ (rounded down).
59
+
60
+
Due to the large data range of the problem, we can implement it with a dynamically allocated segment tree. A dynamically allocated segment tree means that we only allocate nodes when needed, instead of allocating all nodes at the beginning. This can save space, but it requires **lazy propagation** to maintain interval modification.
61
+
62
+
In terms of time complexity, the time complexity of each operation is $O(\log n)$. The space complexity is $O(m \times \log n)$. Here, $m$ is the number of operations, and $n$ is the data range.
0 commit comments