Maintain the array explicitly and process the queries in order.
For a type 1 query, copy , sort the copy, and remove duplicates. The lower_bound index of each original value in this sorted list is its compressed coordinate. Do not change elements outside the selected range.
For a type 2 query, scan the range to obtain its minimum and maximum . Instead of using floating-point arithmetic, compare for every candidate value . Choose the smallest value among those minimizing this expression. Store array values and intermediate calculations in .
For a type 3 query, add directly to every element in the selected range.
Correctness: The position in the sorted distinct-value list equals the number of distinct smaller values, so type 1 is correct. Type 2 considers every value occurring in the range, compares precisely twice its distance to the required midpoint, and applies the tie rule. Type 3 changes exactly the required elements. Therefore all queries are processed correctly.
Each type 1 query takes time, while each other query takes time. There are at most type 1 queries, giving total time and auxiliary space .
long long