#include <bits/stdc++.h>
using namespace std;
#include "grader.h"
pair<int, int> Rescue2(int Rl, int Rr, int Cl, int Cr, int MR, int MC, int X){
int c = 0;
map<int, pair<int, int>> m;
for(int i = Rl; i <= Rr; i++){
for(int j = Cl; j <= Cr; j++){
m[abs(i-MR)+abs(j-MC)] = {i, j};
c = max(c, abs(i-MR)+abs(j-MC));
}
}
int l = 0, r = c;
while(r-l > 1){
int mid = (l+r)/2;
int x = m[mid].first, y = m[mid].second;
int h = Measure(x, y);
if(h == X){
return {x, y};
}
if(h < X) r = mid;
else l = mid;
}
for(int i = Rl; i <= Rr; i++){
for(int j = Cl; j <= Cr; j++){
int k = abs(i-MR)+abs(j-MC);
if(k == l || k == r){
int h = Measure(i, j);
if(h == X) return {i, j};
}
}
}
return {-1, -1};
}
void Rescue(int R, int C, int MR, int MC, int X){
pair<int, int> p;
p = Rescure(1, MR, 1, MC, MR, MC, X);
if(p != make_pair(-1, -1)) Pinpoint(p.first, p.second);
p = Rescure(1, MR, MC, C, MR, MC, X);
if(p != make_pair(-1, -1)) Pinpoint(p.first, p.second);
p = Rescure(MR, R, 1, MC, MR, MC, X);
if(p != make_pair(-1, -1)) Pinpoint(p.first, p.second);
p = Rescure(MR, R, MC, C, MR, MC, X);
if(p != make_pair(-1, -1)) Pinpoint(p.first, p.second);
}