CF BUDDY
← Problems·

1520F2 · Guess the K-th Zero (Hard version)

2200 · binary search, constructive algorithms, data structures

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 1tmin(n,104)1 \le t \le \min(n, 10^4) and the total number of queries is limited to 61046 \cdot 10^4.

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 kk-th zero from the left tt times.

Polycarp can make no more than 61046 \cdot 10^4 requests totally of the following type:

  • ? ll rr — find out the sum of all elements in positions from ll to rr (1lrn1 \le l \le r \le n) 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 kk-th zero was xx, then after Polycarp guesses this position, the xx-th element of the array will be replaced from 00 to 11.

Help Polycarp win the game.

Note: In the first test, the array [1,0,1,1,0,1][1, 0, 1, 1, 0, 1] is hidden. After answering the query k=2k=2, the array changed to [1,0,1,1,1,1][1, 0, 1, 1, 1, 1].

Sample Cases

Case 1

Input

6 2

2

2

1

1

0

1

0

Output

? 4 6

? 1 1

? 1 2

? 5 5

! 5

? 2 2

! 2

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback