Language: C++

#include<bits/stdc++.h>

using namespace std;

struct Node {
	int pl,pr,vl,vr,type;
	vector<int> sons;
} a[404040];

struct Point {
	int x,y,tag;
}point[202020];

int ans[202020],dp[404040][2],n,tot,x[202020],y[202020],p[202020],rkx[202020],rky[202020];

inline int dist(int a, int b) {
	return abs(x[p[a]]-x[p[b]])+abs(y[p[a]]-y[p[b]]);
}

int l[MAXN], r[MAXN];

void work(int pos) {
	//cout << pos << endl;
	if (pos <= n) {
		ans[p[pos]] = dp[pos][0];
		return;
	}
	vector <int> &s = a[pos].sons;
	for (int i = 0, last = a[pos].pl; i < s.size(); i++) {
		l[i] = last; if (a[s[i]].type != 0) last = a[s[i]].pr + 1;
	}
	for (int i = s.size() - 1, last = a[pos].pr; i >= 0; i--) {
		r[i] = last; if (a[s[i]].type != 0) last = a[s[i]].pl - 1;
	}
	for (int i = 0; i < s.size(); i++) {
		int dest = s[i], pl = l[i], pr = r[i];
		
		if (pl == a[dest].pl && pr == a[dest].pr) 
			dp[dest][0] = dp[dest][1] = 0;
		
		else if (pl == a[dest].pl) {
			if (pl == a[pos].pl && pr == a[pos].pr) {
				dp[dest][0] = dist(a[dest].pl, pr) + dp[pos][1];
				dp[dest][1] = dist(a[dest].pr, pr) + dp[pos][1];
			}
			else {
				dp[dest][0] = dist(a[dest].pl, pr);
				dp[dest][1] = dist(a[dest].pr, pr);
			}
		} else if (pr == a[dest].pr) {
			if (pl == a[pos].pl && pr == a[pos].pr) {
				dp[dest][0] = dist(a[dest].pl, pl) + dp[pos][0];
				dp[dest][1] = dist(a[dest].pr, pl) + dp[pos][0];
			}
			else {
				dp[dest][0] = dist(a[dest].pl, pl);
				dp[dest][1] = dist(a[dest].pr, pl);
			}
		} else {
			if (pl == a[pos].pl && pr == a[pos].pr) {
				dp[dest][0] = min(dp[pos][0] + dist(a[dest].pl, pr) + dist(pl, pr), dp[pos][1] + dist(a[dest].pl, pl) + dist(pl, pr));
				dp[dest][1] = min(dp[pos][0] + dist(a[dest].pr, pr) + dist(pl, pr), dp[pos][1] + dist(a[dest].pr, pl) + dist(pl, pr));
			} else {
				dp[dest][0] = min(dist(a[dest].pl, pr) + dist(pl, pr), dist(a[dest].pl, pl) + dist(pl, pr));
				dp[dest][1] = min(dist(a[dest].pr, pr) + dist(pl, pr), dist(a[dest].pr, pl) + dist(pl, pr));
			}
		}
		dp[dest][0] -= a[dest].pl - pl + pr - a[dest].pr;
		dp[dest][1] -= a[dest].pl - pl + pr - a[dest].pr;
	}
	for (int i = 0; i < s.size(); i++)
		work(s[i]);
}
inline bool chk(Node &A,Node &B) {
	return max(A.vr,B.vr)-min(A.vl,B.vl) == max(A.pr,B.pr)-min(A.pl,B.pl);
}
inline int join(int x, int y) {
	if (a[x].vl < a[y].vl) {
		a[++tot].type = 1;
		a[tot].pl = a[x].pl;
		a[tot].vl = a[x].vl;
		a[tot].pr = a[y].pr;
		a[tot].vr = a[y].vr;
	} 
	else {		
		a[++tot].type = -1;
		a[tot].pl = a[x].pl;
		a[tot].vl = a[y].vl;
		a[tot].pr = a[y].pr;
		a[tot].vr = a[x].vr;
	}
	a[tot].sons.push_back(x);
	a[tot].sons.push_back(y);
	return tot;
}
int s[MAXN];
inline void buildTree() {
	for (int i = 1;i <= n; i++) {
		a[++tot].type = 0;
		a[tot].pl = a[tot].pr = i;
		a[tot].vl = a[tot].vr = rky[p[i]];
	}
	int top = 0;
	for (int i = 1;i <= n; i++) {
		bool fl = 1;
		s[++top] = i;
		while (fl) {
			fl = 0;
			if (top >= 2 && a[s[top - 1]].type == 1 && a[s[top - 1]].vr + 1 == a[s[top]].vl) {
				a[s[top - 1]].vr = a[s[top]].vr;
				a[s[top - 1]].pr = a[s[top]].pr;
				a[s[top - 1]].sons.push_back(s[top]);
				fl = 1;
				top--;
			} 
			else if (top >= 2 && a[s[top - 1]].type == -1 && a[s[top - 1]].vl - 1 == a[s[top]].vr) {
				a[s[top - 1]].vl = a[s[top]].vl;
				a[s[top - 1]].pr = a[s[top]].pr;
				a[s[top - 1]].sons.push_back(s[top]);
				fl = 1;
				top--;
			} 
			else if (top >= 2 && chk(a[s[top]],a[s[top-1]])) {
				int x,y;
				y = s[top--];
				x = s[top--];
				s[++top] = join(x, y);
				fl = 1;
			}
		}
	}
	for (int i = 1;i <= top; i++)
		work(s[i]);
}
bool cmpx(Point A,Point B) {
	return A.x < B.x;
}
bool cmpy(Point A,Point B) {
	return A.y < B.y;
}
int main() {
	scanf("%d",&n); 
	for (int i = 1;i <= n; i++) {
		scanf("%d%d",&point[i].x,&point[i].y),point[i].tag = i;
		x[i] = point[i].x;
		y[i] = point[i].y;
	}
	sort(point+1,point+n+1,cmpx);
	for (int i = 1;i <= n; i++)
		rkx[point[i].tag] = i;
		
	sort(point+1,point+n+1,cmpy);
	for (int i = 1;i <= n; i++)
		rky[point[i].tag] = i;
		
	for (int i = 1;i <= n; i++)
		p[rkx[i]] = i;
		
	buildTree();
	
	for (int i = 1; i <= n; i++)
		printf("%lld\n", ans[i]);
	return 0;
}

Compiler output:
a.cc:20:7: error: 'MAXN' was not declared in this scope
   20 | int l[MAXN], r[MAXN];
      |       ^~~~
a.cc:20:16: error: 'MAXN' was not declared in this scope
   20 | int l[MAXN], r[MAXN];
      |                ^~~~
a.cc: In function 'void work(int)':
a.cc:30:17: error: 'l' was not declared in this scope
   30 |                 l[i] = last; if (a[s[i]].type != 0) last = a[s[i]].pr + 1;
      |                 ^
a.cc:33:17: error: 'r' was not declared in this scope
   33 |                 r[i] = last; if (a[s[i]].type != 0) last = a[s[i]].pl - 1;
      |                 ^
a.cc:36:39: error: 'l' was not declared in this scope; did you mean 'pl'?
   36 |                 int dest = s[i], pl = l[i], pr = r[i];
      |                                       ^
      |                                       pl
a.cc:38:41: error: 'pr' was not declared in this scope; did you mean 'pl'?
   38 |                 if (pl == a[dest].pl && pr == a[dest].pr)
      |                                         ^~
      |                                         pl
a.cc:68:50: error: 'pr' was not declared in this scope; did you mean 'pl'?
   68 |                 dp[dest][0] -= a[dest].pl - pl + pr - a[dest].pr;
      |                                                  ^~
      |                                                  pl
a.cc: At global scope:
a.cc:96:7: error: 'MAXN' was not declared in this scope
   96 | int s[MAXN];
      |       ^~~~
a.cc: In function 'void buildTree()':
a.cc:106:17: error: 's' was not declared in this scope
  106 |                 s[++top] = i;
      |                 ^
a.cc:133:22: error: 's' was not declared in this scope
  133 |                 work(s[i]);
      |                      ^