Problem: This is an interactive problem.
This is a hard version of the problem. The difference from the easy version is that in the hard version and the total number of queries is limited to .
Polycarp is playing a computer game. In this game, an array consisting of zeros and ones is hidden. Polycarp wins if he guesses the position of the -th zero from the left times.
Polycarp can make no more than requests totally of the following type:
- ? — find out the sum of all elements in positions from to () inclusive.
To make the game more interesting, each guessed zero turns into one and the game continues on the changed array. More formally, if the position of the -th zero was , then after Polycarp guesses this position, the -th element of the array will be replaced from to .
Help Polycarp win the game.
Note: In the first test, the array is hidden. After answering the query , the array changed to .