File size: 5,010 Bytes
14c9c2b | 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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | #include <bits/stdc++.h>
using namespace std;
using int64 = long long;
static const int64 QUERY_LIMIT = 50000;
int n;
long long k;
// Caching for binary search path (and generally safe to use everywhere)
vector<long long> cacheVal;
vector<unsigned char> cacheSeen;
inline size_t idx(int x, int y) {
return (size_t)x * (n + 1) + (size_t)y;
}
long long ask(int x, int y) {
cout << "QUERY " << x << " " << y << endl;
cout.flush();
long long v;
if (!(cin >> v)) {
// In case of I/O failure, exit immediately.
exit(0);
}
// Cache the value
if ((int)cacheVal.size() >= (n + 1) * (n + 1)) {
size_t id = idx(x, y);
cacheVal[id] = v;
cacheSeen[id] = 1;
}
return v;
}
long long getCached(int x, int y) {
size_t id = idx(x, y);
if (!cacheSeen[id]) {
long long v = ask(x, y);
cacheVal[id] = v;
cacheSeen[id] = 1;
return v;
}
return cacheVal[id];
}
void done(long long ans) {
cout << "DONE " << ans << endl;
cout.flush();
}
long long kthByMerge() {
struct Node {
long long val;
int i, j;
};
struct Cmp {
bool operator()(const Node& a, const Node& b) const {
if (a.val != b.val) return a.val > b.val;
if (a.i != b.i) return a.i > b.i;
return a.j > b.j;
}
};
priority_queue<Node, vector<Node>, Cmp> pq;
vector<long long> firstVal(n + 2, 0);
vector<unsigned char> firstKnown(n + 2, 0);
int nextRow = 1;
auto maybePushRows = [&]() {
if (pq.empty() && nextRow <= n) {
if (!firstKnown[nextRow]) {
firstVal[nextRow] = ask(nextRow, 1);
firstKnown[nextRow] = 1;
}
pq.push({firstVal[nextRow], nextRow, 1});
nextRow++;
}
while (nextRow <= n) {
if (!firstKnown[nextRow]) {
firstVal[nextRow] = ask(nextRow, 1);
firstKnown[nextRow] = 1;
}
if (pq.empty()) {
pq.push({firstVal[nextRow], nextRow, 1});
nextRow++;
} else {
long long topv = pq.top().val;
if (firstVal[nextRow] <= topv) {
pq.push({firstVal[nextRow], nextRow, 1});
nextRow++;
} else break;
}
}
};
long long popped = 0;
long long ans = 0;
maybePushRows();
while (true) {
if (pq.empty()) {
// If heap is empty, push next row (if any)
if (nextRow <= n) {
if (!firstKnown[nextRow]) {
firstVal[nextRow] = ask(nextRow, 1);
firstKnown[nextRow] = 1;
}
pq.push({firstVal[nextRow], nextRow, 1});
nextRow++;
} else {
// No more elements; shouldn't happen if k <= n*n
break;
}
}
Node t = pq.top(); pq.pop();
popped++;
if (popped == k) {
ans = t.val;
break;
}
if (t.j + 1 <= n) {
long long nv = ask(t.i, t.j + 1);
pq.push({nv, t.i, t.j + 1});
}
// After pushing next, maybe include more rows whose first elements are now <= new top
maybePushRows();
}
return ans;
}
long long countLE(long long X) {
long long cnt = 0;
int i = n;
int j = 1;
while (i >= 1 && j <= n) {
long long v = getCached(i, j);
if (v <= X) {
cnt += i;
j++;
} else {
i--;
}
}
return cnt;
}
long long kthByBinary() {
// Query min and max
long long low = ask(1, 1);
long long high = ask(n, n);
// Binary search for smallest value with count >= k
while (low < high) {
long long mid = low + ((high - low) >> 1);
long long c = countLE(mid);
if (c >= k) high = mid;
else low = mid + 1;
}
return low;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
if (!(cin >> n >> k)) {
return 0;
}
// Initialize cache for potential binary search
cacheVal.assign((size_t)(n + 1) * (n + 1), 0);
cacheSeen.assign((size_t)(n + 1) * (n + 1), 0);
// Strategy selection
const long long maxQueries = QUERY_LIMIT;
// Prefer row-wise merge if feasible
if (k + n <= maxQueries) {
long long ans = kthByMerge();
done(ans);
} else {
// Try binary search if feasible given value range <= 1e18 (60 iterations)
// 2n per count + 2 initial endpoints
if (2LL * n * 60 + 2 <= maxQueries) {
long long ans = kthByBinary();
done(ans);
} else {
// Fallback: attempt binary search with caching anyway (may exceed limit on worst cases)
long long ans = kthByBinary();
done(ans);
}
}
return 0;
} |