#include<bits/stdc++.h> #define ll long long #define PII pair<int,int>
usingnamespace std;
int n; map<PII, int> vis; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; PII ans[200050];
// a: new Pair, b: old Pair structnode { PII a, b; node(PII _a, PII _b): a(_a), b(_b) {} };
intmain(){ cin >> n; for (int i = 1; i <= n; ++i) { int x, y; cin >> x >> y; vis.insert({{x, y}, i}); }
queue<node> q, qq;
for (auto& v: vis) { auto p = v.first; // current point auto idx = v.second; // index for (int i = 0; i < 4; ++i) { int nx = p.first + dx[i]; // around points int ny = p.second + dy[i]; PII np = {nx, ny}; if (vis.count(np) == 0) { // not in ans[idx] = np; qq.push(node(p, np)); // [in-vis points, out-vis points] break; } } }
q = qq; while (!qq.empty()) { auto pqq = qq.front(); qq.pop(); // erase old pairs vis.erase(pqq.a); }
while (!q.empty()) { auto pq = q.front(); q.pop(); auto in_p = pq.a; auto out_p = pq.b; for (int i = 0; i < 4; ++i) { int nx = in_p.first + dx[i]; int ny = in_p.second + dy[i]; PII cur_p = {nx, ny}; if (vis.count(cur_p)) { // find! ans[vis[cur_p]] = out_p; // current point's surroundings is vis.erase(cur_p); // remove visited points q.push(node(cur_p, out_p)); } } }
for (int i = 1; i <= n; ++i) { cout << ans[i].first << ' ' << ans[i].second << '\n'; }