In the last Div.4 round, Problem 1985F - Final Boss witnessed numerous hacks, particularly targeting binary search solutions due to unexpected overflow in extreme cases.
A common challenge is determining the appropriate high-value for binary search: choosing it too high (e.g., 1e18) risks overflow, while choosing it too low (e.g., 1e9) may lead to incorrect answers. Finding the perfect static high-value for all cases can be tricky.
Here are two common fixes:
- Use a Very High Value and Handle Overflow with
__int128(This approach can be complex and inefficient). - Select a Suitable "High-Value" through Trial and Error (Not an ideal solution).
A More Sustainable Solution
As we know, binary search operates on a monotonic sequence such as [0, 0, 0, ..., 0, 1, ..., 1, 1, 1, 1]. Our objective is to find the last 0 or the first 1 in this sequence. If we successfully set the lo to any 0 zero and hi to any 1, our binary search will work perfectly. Now instead of relying on a static high-value for all cases, we can dynamically determine the minimum high-value necessary. This method involves starting with a low value and increasing it logarithmically until we find a suitable high-value. This ensures that our binary search operates within a safe range. Here's how you can implement this:
long long lo = 0, hi = 1;
while(!check(hi)) hi *= 2; // Check returns either 0 or 1 based on hi.
At the end of the loop, hi will point to a potential minimum high-value. After that, you can write your regular code. You can check my solution to understand the technique better.
Note: I learn this technique from fahimcp495. Forgive me for any mistake.
I hope this technique will be helpful for those who don't know about this technique. Happy coding!








Or one can simply put a
breakstatement when the sum reaches beyond the desired answer (Specifically for yesterday's problem F)That's true, but this method is a general approach for most binary search problems. You don't need to consider anything else.
Yes that is a good thing it saved me from getting hacked and overflow.
Since you are talking about overflow issues, it is recommended to use
mid = lo + (hi-lo)/2, aslo + hican exceed theintorlong long intrange, buthi-lois guaranteed to stay in the range :)If you use C++20, use
std::midpoint(lo, hi)(cppreference).You can optimise it more:
A doubt- While dynamically increasing hi ,won't you have to keep a check if hi exceeds its statically assigned maximum value? (else there can be a case where ,there is no answer possible ,but since you dynamically keep increasing hi ,there can be a possible solution outside the range)
Well since it's a cp problem, you're usually guaranteed that an answer does exist within the problem constraints and doesn't exceed $$$2^{64}$$$.
Pardon my lack of understanding, but when you say overflow, are you referring to "mid" or something else? If it is mid, shouldn't writing mid = low + (high-low)/2 be enough?
Yes it'll, but the author's way is kinda safer and limits the range stopping unnecessary operations.
no the author is not referring to that, he is explaining how one can define a safe range to binary search on, so that later on when we binary search on answer the sum or whatever check or predicate function will not overflow.
crazy, so simple and useful
What if the values of
check(hi)were something like this : $$$0,0,0,\ldots,0,0,1$$$ where the only validhiwould be $$$2^{63}-1$$$?Then, following this general approach, the
hiwould be $$$2^0, 2^1, \ldots, 2^{61}, 2^{62}$$$. But now after $$$2^{62}$$$ it would overflow when you multiply it by 2?I think usually problem setters aren't psychics who try to annoy the participants, and it's CP so yes this won't happen, but if it did a single
ifwould suffice to check for not overflowor you can
use python