Range Min/Max Operations & Range Historical Extremum
This article explains the problem of handling historical interval maximum/minimum values using segment trees, as mentioned by Ji (Jiyuan) in the 2016 National Training Team paper.
Interval Maximum/Minimum Operations
Broadly speaking, interval maximum/minimum operations refer to taking the \(\max\) or \(\min\) of all numbers in the interval \([l,r]\) with respect to \(x\), i.e., \(a_i=\max(a_i,x)\) or \(a_i=\min(a_i,x)\).
HDU5306 Gorgeous Sequence
Maintain a sequence \(a\) and perform the following operations:
0 l r t\(\forall l\le i\le r,~ a_i=\min(a_i,t)\).1 l rOutput \(\max\limits_{i=l}^r a_i\).2 l rOutput \(\sum\limits_{i=l}^r a_i\).
Multiple test cases, guaranteed \(T\le 100,~\sum n,\sum m\le 10^6\).
Taking the \(\min\) over an interval means only numbers greater than \(t\) are changed. Therefore, the target of this operation is no longer the entire interval, but "the numbers in this interval that are greater than \(t\)". Thus, we can adopt the following approach: each node maintains the maximum value \(Max\) of the interval, the second maximum value \(Se\), the interval sum \(Sum\), and the count \(Cnt\) of maximum values. Next, we consider the operation of taking the \(\min\) with \(t\) over the interval.
- If \(Max\le t\), clearly \(t\) is meaningless, return directly.
- If \(Se<t < Max\), then this \(t\) can update the current interval's maximum. So we add \(Cnt(t-Max)\) to the interval sum, update \(Max\) to \(t\), and set a lazy tag.
- If \(t\le Se\), then we don't know how many numbers are involved in the update. Our strategy is to recursively operate downward. Then push up the information.
What is the complexity of this algorithm? Using potential analysis, the complexity is \(O(m\log n)\). For detailed analysis, refer to the paper.
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
BZOJ4695 Most Fake Female Contestant
Maintain a sequence \(a\) and perform the following operations:
1 l r x\(\forall l\le i\le r,~ a_i=a_i+x\).2 l r x\(\forall l\le i\le r,~ a_i=\max(a_i,x)\).3 l r x\(\forall l\le i\le r,~ a_i=\min(a_i,x)\).4 l rOutput \(\sum\limits_{i=l}^r a_i\).5 l rOutput \(\max\limits_{i=l}^r a_i\).6 l rOutput \(\min\limits_{i=l}^r a_i\).
\(n,m\le 5\times 10^5,~|a_i|\le 10^8\). All type 1 operations have \(|x|\le 10^3\), other operations satisfy \(|x|\le10^8\).
Using the same method, we maintain the maximum, second maximum, count of maximums, minimum, second minimum, count of minimums, and interval sum. Besides this information, we also need to maintain lazy tags for interval \(\max\), interval \(\min\), and interval addition. Compared to the previous problem, this involves the order of tag propagation. We adopt the following strategy:
- We consider the interval addition tag as the highest priority, and the other two tags as equal.
- When adding a \(v\) tag to a node, besides updating the satellite information and the current node's interval addition tag with \(v\), we also use \(v\) to update the interval \(\max\) and interval \(\min\) tags.
- When taking the \(\min\) with \(v\) for a node (ignoring the brute-force search process here, assuming the tag meets the addition condition), besides updating satellite information, we compare it with the interval \(\max\) tag. If \(v\) is less than the interval \(\max\) tag, then all numbers will eventually become \(v\), so we change the interval \(\max\) tag to \(v\). Otherwise, do nothing.
- Taking the \(\max\) with \(v\) over an interval is similar.
When maintaining information, number set overlap may occur when there is only one or two numbers, e.g., a number being both the maximum and the second minimum, which requires special handling.
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | |
Summary
In this chapter, we presented four example problems, explaining the maintenance of basic interval maximum/minimum operations, priority handling of multiple tags, the idea of number set classification, and maintenance of multiple categories. Essentially, the basic idea for handling interval maximum/minimum operations is the classification and efficient merging of number set information. In the next chapter, we will explore problems related to historical interval maximum/minimum values.
Historical Maximum/Minimum Problems
Historical Maximum/Minimum is Not Persistence
Note that the historical maximum/minimum problems discussed in this chapter are different from so-called persistent data structures. We refer to these special problems as historical maximum/minimum problems. Historical maximum/minimum problems can be divided into three categories.
Historical Maximum
Simply put, the historical maximum of a position is the maximum value that has ever appeared at that position. Formally, we define an auxiliary array \(B\), initially identical to \(A\). After each operation on \(A\), we take the \(\max\) over the entire array:
Then, we call \(B_i\) the historical maximum of this position.
Historical Minimum
The definition is similar to historical maximum. After each operation on \(A\), we take the \(\min\) over the entire array. Then, we call \(B_i\) the historical minimum of this position.
Historical Version Sum
The auxiliary array \(B\) is initially all \(0\). After each operation, we accumulate the entire \(A\) array onto the \(B\) array:
We call \(B_i\) the historical version sum at position \(i\).
Next, we discuss historical maximum/minimum problems divided into four categories.
Problems Solvable with Tags
CPU Monitoring
Sequences \(A,B\) are initially the same:
- Interval assignment \(x\) on \(A\).
- Interval addition \(x\) on \(A\).
- Query the interval \(\max\) of \(A\).
- Query the interval \(\max\) of \(B\).
After each operation, we perform an update: \(\forall i\in [1,n],\ B_i=\max(B_i,A_i)\). \(n,m\le 10^5\).
First, ignore operation 1. Then we only have interval addition operations. We maintain a tag \(Add\) representing the value added to the current interval; this tag can solve the interval \(\max\) problem. Next, consider the historical interval \(\max\). We define a tag \(Pre\), whose meaning is: during the lifetime of this tag, the historical maximum of the \(Add\) tag.
This definition might be vague. Therefore, we first explain the lifetime of a tag. A tag goes through the following process:
- Created at node \(u\).
- While receiving several new tags at node \(u\), merged with the new tags (for tags of the same type).
- The tag at node \(u\) is propagated to \(u\)'s children, and \(u\)'s tag is cleared.
We consider that during this process, from step 1 to before step 3, is the lifetime of the tag at node \(u\). After two tags merge, they become the same tag, and their lifetimes also merge (i.e., take the earlier creation time as the start of the lifetime). An equivalent statement is: the time period from the last moment this node's tag was propagated to the current moment.
Why define lifetime? Using this concept, we can prove: during the lifetime of a node's tag, its child nodes do not undergo any changes and retain the state before this lifetime. The reason is simple: because during this period, you did not propagate tags.
Thus, you can ensure that the historical maximum \(Add\) during the current tag's lifetime can be updated to the child nodes' tags and information. Because the child nodes' tags and information have not changed during this time period. So we propagate \(u\)'s tag to its child \(s\), and it's not hard to see:
Similarly, information updates are done using the corresponding tags.
Next, consider operation 1.
The interval assignment operation turns all numbers into one number. After that, whether it's interval addition/subtraction or assignment, all numbers in the interval remain the same (unless you end the current tag's lifetime and propagate tags). Therefore, we can treat all tags after the first interval assignment as interval assignment tags. That is, a tag's lifetime is roughly divided into two stages:
- Merging of several addition/subtraction operation tags, without receiving any assignment tag.
- Assignment operation tags, without any addition/subtraction tags (addition/subtraction tags are converted to assignment tags).
So we split the node's Pre tag into \((P_1,P_2)\). \(P_1\) represents the maximum addition/subtraction tag in the first stage; \(P_2\) represents the maximum assignment tag in the second stage. Using a similar method, we can perform tag propagation and information updates. The time complexity is \(O(m\log n)\) (note: this problem does not have interval \(\max\)/\(\min\) operations with \(x\)).
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用