File size: 2,476 Bytes
1fd0050
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <bits/stdc++.h>
using namespace std;

static const int QUERY_LIMIT = 50000;

long long used_queries = 0;

long long do_query(int x, int y) {
    cout << "QUERY " << x << " " << y << endl;
    cout.flush();
    long long v;
    if (!(cin >> v)) {
        exit(0);
    }
    used_queries++;
    return v;
}

[[noreturn]] void done(long long ans) {
    cout << "DONE " << ans << endl;
    cout.flush();
    exit(0);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    long long k;
    if (!(cin >> n >> k)) {
        return 0;
    }

    long long total = 1LL * n * n;
    long long k_from_end = total - k + 1;

    struct Node {
        long long v;
        int i, j;
        bool operator<(const Node& other) const {
            return v > other.v; // min-heap
        }
    };
    struct NodeMax {
        long long v;
        int i, j;
        bool operator<(const NodeMax& other) const {
            return v < other.v; // max-heap
        }
    };

    if (k <= k_from_end) {
        priority_queue<Node> pq;
        vector<int> ptr(n + 1, 1);
        // Initialize heads of all rows (column 1)
        for (int i = 1; i <= n; ++i) {
            long long v = do_query(i, 1);
            pq.push({v, i, 1});
        }
        long long cnt = 0;
        long long ans = 0;
        while (!pq.empty()) {
            Node cur = pq.top(); pq.pop();
            cnt++;
            if (cnt == k) {
                ans = cur.v;
                done(ans);
            }
            int i = cur.i;
            int j = cur.j;
            if (j + 1 <= n) {
                long long v = do_query(i, j + 1);
                pq.push({v, i, j + 1});
            }
        }
    } else {
        priority_queue<NodeMax> pq;
        vector<int> ptr(n + 1, n);
        // Initialize tails of all rows (column n)
        for (int i = 1; i <= n; ++i) {
            long long v = do_query(i, n);
            pq.push({v, i, n});
        }
        long long target = k_from_end;
        long long cnt = 0;
        long long ans = 0;
        while (!pq.empty()) {
            NodeMax cur = pq.top(); pq.pop();
            cnt++;
            if (cnt == target) {
                ans = cur.v;
                done(ans);
            }
            int i = cur.i;
            int j = cur.j;
            if (j - 1 >= 1) {
                long long v = do_query(i, j - 1);
                pq.push({v, i, j - 1});
            }
        }
    }

    return 0;
}