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/0600-0699/0699.Falling Squares/README_EN.md
+125-2Lines changed: 125 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -68,7 +68,25 @@ Note that square 2 only brushes the right side of square 1, which does not count
68
68
69
69
<!-- solution:start -->
70
70
71
-
### Solution 1
71
+
### Solution 1: Segment Tree
72
+
73
+
According to the problem description, we need to maintain a set of intervals that support modification and query operations. In this case, we can use a segment tree to solve the problem.
74
+
75
+
A segment tree divides the entire interval into multiple non-contiguous sub-intervals, with the number of sub-intervals not exceeding $\log(width)$, where $width$ is the length of the interval. To update the value of an element, we only need to update $\log(width)$ intervals, and these intervals are all contained within a larger interval that includes the element. When modifying intervals, we need to use **lazy propagation** to ensure efficiency.
76
+
77
+
- Each node of the segment tree represents an interval;
78
+
- The segment tree has a unique root node representing the entire statistical range, such as $[1, n]$;
79
+
- Each leaf node of the segment tree represents a primitive interval of length 1, $[x, x]$;
80
+
- For each internal node $[l, r]$, its left child is $[l, \textit{mid}]$, and its right child is $[\textit{mid} + 1, r]$, where $\textit{mid} = \frac{l + r}{2}$;
81
+
82
+
For this problem, the information maintained by the segment tree nodes includes:
83
+
84
+
1. The maximum height $v$ of the blocks in the interval
85
+
2. Lazy propagation marker $add$
86
+
87
+
Additionally, since the range of the number line is very large, up to $10^8$, we use dynamic node creation.
88
+
89
+
In terms of time complexity, each query and modification has a time complexity of $O(\log n)$, and the total time complexity is $O(n \log n)$. The space complexity is $O(n)$.
72
90
73
91
<!-- tabs:start -->
74
92
@@ -372,7 +390,7 @@ func newNode(l, r int) *node {
0 commit comments