Language: C++

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define mod 1000000007
#include<string.h>
#define inf 7000000000000000000
#define maxn 300006
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
#define pi 3.14159265359
#define endl '\n'
#define mapint_iterator map<int,int> :: iterator
#define mapll_iterator map<ll,ll> :: iterator
#define setint_iterator set<int> :: iterator
#define setll_iterator set<ll> :: iterator
#define setpint_iterator set<pint> :: iterator
#define setpll_iterator set<pll> :: iterator
#define trace(x) cout<<#x<<": "<<x<<" "<<endl
#define trace2(x,y) cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x,y,z) cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a,b,c,d) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define cout1(a) cout<<a<<endl
#define cout2(a,b) cout<<a<<' '<<b<<endl
#define cout3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl
#define cout4(a,b,c,d) cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl
#define vcout(v,i) cout<<v[i].fi<<" "<<v[i].se<<endl
typedef priority_queue<ll,vector<ll>,greater<ll> > pqset;
typedef priority_queue<pll,vector<pll>,greater<pll> > pqset_ll;
#define pb push_back
#define pf push_front
#define fi first
#define se second
ll dxk[]={0,0,1,-1,1,1,-1,-1};
ll dyk[]={1,-1,0,0,1,-1,1,-1};
/*
#include <ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> os;
typedef tree<pll,null_type,less<pll>,rb_tree_tag,tree_order_statistics_node_update> os_pair;
*/
#define acc (ios::sync_with_stdio(false),cin.tie(0))
#define rep(i,n) for(ll i=0;i<n;i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define per1(i,n) for(ll i=n;i>0;i--)
#define repeat(i,start,n) for(ll i=start;i<n;i++)
#define print(a,n) for(ll i=0;i<n;i++){ cout<<a[i]<<" ";}
ll ll_max(ll a,ll b,ll c){return max(a,max(b,c));}
int int_max(int a,int b,int c){return max(a,max(b,c));}
ll ll_min(ll a,ll b,ll c){return min(a,min(b,c));}
int int_min(int a,int b,int c){return min(a,min(b,c));}
ll max(int a,ll b){ return max((ll)a,b);}
ll min(int a,ll b){ return min((ll)a,b);} 
ll min(ll a,int b){ return min(a,(ll)b);}
ll max(ll a,int b){ return max(a,(ll)b);}
ll dx[]={0,0,1,-1};
ll dy[]={1,-1,0,0};
ll power(ll a,ll b){
	if(a==1)
		return 1;
	if(b==0)
		return 1;
	ll c=power(a,b/2);
	ll res=1;
	if(b%2){
		res=(c*c)%mod;
		res*=a;
		res%=mod;
	}
	else
		res=((c*c)%mod);
	return res;
}
ll modInv(ll a){return power(a,mod-2)%mod;}
ll fact[1],inv[1];
void factorial(ll n){
	fact[0]=1;
	for(ll i=1;i<=n;i++){
		fact[i]=fact[i-1]*i;
		fact[i]%=mod;
	}
}
void InvFactorial(ll n){
	inv[0]=1;
	for(ll i=1;i<=n;i++)
		inv[i]=modInv(fact[i]);
}
ll ncr(ll n,ll r){
	if(n<r||n<0||r<0)
		return 0;
	ll b=inv[n-r];
	ll c=inv[r];
	ll a=fact[n]*b;
	a%=mod;
	a*=c;
	a%=mod;
	return a;
}
//ifstream cin("b_read_on.txt"); ofstream cout("output3.txt");
//Chronology: Binary Search,Greedy,dp,dfs and similar
//Use (<<) for multiplication
//Use (>>) for division
//If memory limit exceeds in top-down , try bottom up dp
//ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);cout<<fixed;cerr.tie(NULL);
// find_by_order -> value at index
// order_of_key -> index of value
// while using (1<<i) use ((ll)1<<(ll)i) 
// if global declared initialise per testcase
// in Floyd-Warshall Algo, k is outer loop 
//pair is initialised as {0,0} 
//value of dp array is not the same in top-down & bottom-up 
// If an element was not initially in map and if asked mp[a],the element gets inserted 
// Dont use unordered_map until necessary
// func(int i,int j) with 0 loop has same complexity as func(int i) with 1 loop
//use hashing when no other way available
// a%=mod take a lot of time... try to use it minimum and use memset as it reduces a lot of time usage
// use if(a>=mod) a%=mod
//cout<<(double) can be harmful , always use printf(%.9llf)
int a[250001],size[250001];
int l[250001],r[250001],u[250001],d[250001],x[250001],y[250001],val[501][501];
int find(int x){
	if(a[x]==-1)
		return x;
	return find(a[x]);
}
void insert(int x,int y){
	//cout2(x,y);
	x=find(x);
	y=find(y);
	if(x==y)
		return;
	if(size[x]<=size[y]){
		size[y]+=size[x]+1;
		a[x]=y;
		l[y]=min(l[y],l[x]);
		r[y]=max(r[y],r[x]);
		u[y]=min(u[y],u[x]);
		d[y]=max(d[y],d[x]);
		return;
	}
	size[x]+=size[y]+1;
	a[y]=x;
	l[x]=min(l[y],l[x]);
	r[x]=max(r[y],r[x]);
	u[x]=min(u[y],u[x]);
	d[x]=max(d[y],d[x]);
}
void solve(int countu){
	int n;
	scanf("%d",&n);
	memset(a,-1,sizeof(a));
	int count=1;
	repeat(i,1,n+1){
		repeat(j,1,n+1){
			x[count]=i;
			y[count]=j;
			val[i][j]=count;
			u[count]=i;
			d[count]=i;
			l[count]=j;
			r[count]=j;
			count++;
		}
	}
	int ans=0;
	set<pint> s;
	rep(i,n*n){
		int d1;
		scanf("%d",&d1);
		rep(j,4){
			int h=x[d1]+dx[j];
			int h1=y[d1]+dy[j];
			if(s.find({h,h1})!=s.end())
				insert(d1,val[h][h1]);
		}
		s.insert({x[d1],y[d1]});
		int j=find(d1);
		ans+=min(u[j]-1,int_min(n-d[j],l[j]-1,n-r[j]));
	}
	cout1(ans);
}
int main(){
	//ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);cout<<fixed;cerr.tie(NULL);
	int t=1;
	//cin>>t;
	int countu=1;
    while(t--){
    	solve(countu);
    	countu++;
    }
}


Compiler output:
a.cc: In function 'void insert(int, int)':
a.cc:133:12: error: reference to 'size' is ambiguous
  133 |         if(size[x]<=size[y]){
      |            ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:120:15: note:                 'int size [250001]'
  120 | int a[250001],size[250001];
      |               ^~~~
a.cc:133:21: error: reference to 'size' is ambiguous
  133 |         if(size[x]<=size[y]){
      |                     ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:120:15: note:                 'int size [250001]'
  120 | int a[250001],size[250001];
      |               ^~~~
a.cc:134:17: error: reference to 'size' is ambiguous
  134 |                 size[y]+=size[x]+1;
      |                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:120:15: note:                 'int size [250001]'
  120 | int a[250001],size[250001];
      |               ^~~~
a.cc:134:26: error: reference to 'size' is ambiguous
  134 |                 size[y]+=size[x]+1;
      |                          ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:120:15: note:                 'int size [250001]'
  120 | int a[250001],size[250001];
      |               ^~~~
a.cc:142:9: error: reference to 'size' is ambiguous
  142 |         size[x]+=size[y]+1;
      |         ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:120:15: note:                 'int size [250001]'
  120 | int a[250001],size[250001];
      |               ^~~~
a.cc:142:18: error: reference to 'size' is ambiguous
  142 |         size[x]+=size[y]+1;
      |                  ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:120:15: note:                 'int size [250001]'
  120 | int a[250001],size[250001];
      |               ^~~~