| #include <iostream> |
| #include <vector> |
| #include <algorithm> |
| #include <map> |
| #include <random> |
|
|
| using namespace std; |
|
|
| |
| int N; |
| long long K; |
| int query_count = 0; |
| const int QUERY_LIMIT = 50000; |
|
|
| |
| |
| map<long long, long long> cache_map; |
|
|
| |
| |
| long long query(int r, int c) { |
| |
| if (r < 0 || r >= N || c < 0 || c >= N) return -2e18; |
| |
| long long key = (long long)r * N + c; |
| if (cache_map.count(key)) { |
| return cache_map[key]; |
| } |
| |
| |
| if (query_count >= QUERY_LIMIT) { |
| return -1; |
| } |
|
|
| cout << "QUERY " << r + 1 << " " << c + 1 << endl; |
| long long val; |
| cin >> val; |
| query_count++; |
| cache_map[key] = val; |
| return val; |
| } |
|
|
| void answer(long long ans) { |
| cout << "DONE " << ans << endl; |
| exit(0); |
| } |
|
|
| int main() { |
| |
| ios_base::sync_with_stdio(false); |
| cin.tie(NULL); |
|
|
| if (!(cin >> N >> K)) return 0; |
|
|
| |
| vector<int> L(N, 0); |
| vector<int> R(N, N - 1); |
| |
| |
| |
| long long cnt_less = 0; |
|
|
| |
| mt19937_64 rng(1337); |
|
|
| while (true) { |
| |
| long long total_active = 0; |
| vector<long long> prefix_active(N + 1, 0); |
| for (int i = 0; i < N; ++i) { |
| if (L[i] <= R[i]) { |
| total_active += (R[i] - L[i] + 1); |
| } |
| prefix_active[i+1] = total_active; |
| } |
|
|
| if (total_active == 0) { |
| break; |
| } |
|
|
| |
| long long rand_idx = rng() % total_active; |
| int pivot_r = -1, pivot_c = -1; |
| |
| |
| int row_idx = lower_bound(prefix_active.begin(), prefix_active.end(), rand_idx + 1) - prefix_active.begin() - 1; |
| pivot_r = row_idx; |
| long long offset = rand_idx - prefix_active[row_idx]; |
| pivot_c = L[pivot_r] + offset; |
|
|
| long long pivot_val = query(pivot_r, pivot_c); |
|
|
| |
| |
| |
| |
| |
| vector<int> S_le(N); |
| long long count_le = 0; |
| int curr_c = N - 1; |
| |
| |
| if (N > 0) curr_c = R[0]; |
| |
| for (int i = 0; i < N; ++i) { |
| if (curr_c > R[i]) curr_c = R[i]; |
| |
| |
| while (curr_c >= L[i]) { |
| long long val = query(i, curr_c); |
| if (val > pivot_val) { |
| curr_c--; |
| } else { |
| break; |
| } |
| } |
| |
| S_le[i] = curr_c; |
| if (curr_c >= L[i]) { |
| count_le += (curr_c - L[i] + 1); |
| } |
| } |
| |
| |
| long long total_le = cnt_less + count_le; |
|
|
| if (total_le < K) { |
| |
| |
| |
| cnt_less = total_le; |
| for (int i = 0; i < N; ++i) { |
| L[i] = max(L[i], S_le[i] + 1); |
| } |
| } else { |
| |
| |
| |
| |
| vector<int> S_lt(N); |
| long long count_lt = 0; |
| if (N > 0) curr_c = R[0]; |
| |
| for (int i = 0; i < N; ++i) { |
| if (curr_c > R[i]) curr_c = R[i]; |
| |
| while (curr_c >= L[i]) { |
| long long val = query(i, curr_c); |
| if (val >= pivot_val) { |
| curr_c--; |
| } else { |
| break; |
| } |
| } |
| S_lt[i] = curr_c; |
| if (curr_c >= L[i]) { |
| count_lt += (curr_c - L[i] + 1); |
| } |
| } |
| |
| long long total_lt = cnt_less + count_lt; |
| |
| if (K <= total_lt) { |
| |
| |
| |
| for (int i = 0; i < N; ++i) { |
| R[i] = min(R[i], S_lt[i]); |
| } |
| } else { |
| |
| |
| answer(pivot_val); |
| } |
| } |
| } |
|
|
| return 0; |
| } |