#pragma GCC optimize("O3")
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#define ordered_set tree<int, null_type,less<int >, rb_tree_tag,tree_order_statistics_node_update>
#define eps 1e-9
#define MOD1 998244353
#define MOD2 1000000007
#define INV_10 299473306
#define INF 1000000000
#define PI 3.14159265358979323846
using namespace std;
int DistanceSum(int N, int *X, int *Y)
{
if(N <= 2000)
{
vector<int>v[N];
for(int j = 0; j < N; j++)
{
for(int k = j+1; k< N; k++)
{
long long x1=abs(X[j]-X[k]), y1=abs(Y[j]-Y[k]);
long long dis=x1+y1;
if(dis==1)
{
v[k].push_back(j);
v[j].push_back(k);
}
}
}
long long ans=0;
for(int i = 0; i < N; i++)
{
//cout << i << ' ' << X[i] << ' ' << Y[i] << '\n';
unordered_set<int>s;
int nb=1, idx=0;
vector<int>bfs;
bfs.push_back(i);
bool ok=1;
s.insert(i);
while(bfs.size()!=N)
{
//cout << nb << '\n';
int l=bfs.size();
for(int j = idx; j < l; j++)
{
//cout << bfs[j].first << ' ' << bfs[j].second << '\n';
for(int k = 0; k < v[bfs[j]].size(); k++)
{
if(!s.count(v[bfs[j]][k]))
{
s.insert(v[bfs[j]][k]);
if(v[bfs[j]][k] < i)
ans+=nb;
bfs.push_back(v[bfs[j]][k]);
}
}
}
nb++;
idx=l;
}
ans%= 1000000000;
}
//ans*=( 1000000000);
ans%= 1000000000;
return ans;
}
else
{
int x_min=2147483646, x_max=0, y_min=2147483646, y_max=0;
for(int i = 0; i < N; i++)
{
x_min=min(x_min, X[i]);
x_max=max(x_max, X[i]);
y_min=min(y_min, Y[i]);
y_max=max(y_max, Y[i]);
}
/*int a[y_max-y_min+1];
int b[y_max-y_min+1];
for(int i = 0; i < y_max-y_min+1; i++)
{
a[i]=-1;
}*/
vector<pair<int, long long> >v[x_max-x_min+1];
for(int i = 0; i < N; i++)
{
v[X[i]].push_back(make_pair(Y[i], 0LL));
}
for(int i = 0; i < x_max-x_min+1; i++)
sort(v[i].begin(), v[i].end());
long long ans=0;
long long nb=0;
for(int i = x_min; i <= x_max; i++)
{
for(int j = 0; j < v[i-x_min].size(); j++)
{
long long cu=j, cu1=(v[i-x_min].size()-j-1);
cu1*=(cu1+1);
cu*=(j+1);
cu/=2;
cu1/=2;
ans+=cu;
ans%=1000000000;
long long cu2=cu+cu1;
cu2%=1000000000;
v[i-x_min][j].second=cu2;
if(i!=x_min)
{
vector<pair<int, long long> >::iterator it=lower_bound(v[i-x_min-1].begin(), v[i-x_min-1].end(), make_pair(v[i-x_min][j].first, 0LL));
if(it==v[i-x_min-1].end())
it=prev(it);
long long dis=1+abs(v[i-x_min][j].first-(*it).first);
long long ans+=dis*nb+(*it).second;
ans%=1000000000;
v[i-x_min][j].second+=(dis*nb+(*it).second);
}
}
nb+=v[i-x_min].size();
}
return ans;
}
}
Compilation message
city.cpp: In function 'int DistanceSum(int, int*, int*)':
city.cpp:41:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
41 | while(bfs.size()!=N)
| ~~~~~~~~~~^~~
city.cpp:48:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for(int k = 0; k < v[bfs[j]].size(); k++)
| ~~^~~~~~~~~~~~~~~~~~
city.cpp:39:9: warning: unused variable 'ok' [-Wunused-variable]
39 | bool ok=1;
| ^~
city.cpp:95:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for(int j = 0; j < v[i-x_min].size(); j++)
| ~~^~~~~~~~~~~~~~~~~~~
city.cpp:113:19: error: expected initializer before '+=' token
113 | long long ans+=dis*nb+(*it).second;
| ^~