#include <vector>
#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#define pii pair<int,int>
#define F first
#define S second
using namespace std;
const int MOD = 1e9, maxN = 1e5 + 326;
const long long int INF = (1LL << 31) - 2;
int mp[maxN][maxN], vis[maxN], d[maxN], dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
queue<int> que;
vector<pii> pos;
inline int add(int a, int b){
return (a + b >= MOD ? a + b - MOD : a + b);
}
struct BIT{
int arr[maxN];
BIT(){}
void modify(int p, int x){
for(; p < maxN; p += (p & -p)) arr[p] += x;
}
inline int sum(int p){
int r = 0;
for(;p; p -= (p & -p)) r = add(r, arr[p]);
return r;
}
} cnt, xy;
int DistanceSum(int N, int *X, int *Y) {
if(N <= 2000){
if(N > maxN) return 0;
int minX = INF, minY = INF;
for(int i = 0; i < N; i++){
minX = min(minX, X[i]);
minY = min(minY, Y[i]);
}
for(int i = 0; i < N; i++){
X[i] -= minX - 1;
Y[i] -= minY - 1;
mp[X[i]][Y[i]] = i + 1;
}
long long int ans = 0;
for(int i = 0; i < N; i++){
fill(d, d + N, INF);
fill(vis, vis + N, false);
d[i] = 0;
que.push(i);
vis[i] = true;
//cout << "i = " << i << endl;
while(que.size()){
int id = que.front(); que.pop();
for(int j = 0; j < 4; j++){
if(mp[X[id] + dir[j][0]][Y[id] + dir[j][1]]){
int nid = mp[X[id] + dir[j][0]][Y[id] + dir[j][1]] - 1;
if(!vis[nid]){
d[nid] = d[id] + 1;
//cout << "d[" << i << "][" << nid << "] = " << d[nid] << endl;
ans = add(ans, d[nid]);
vis[nid] = true;
que.push(nid);
}
}
}
}
}
return ans / 2;
}
int minX = INF, minY = INF;
for(int i = 0; i < N; i++){
minX = min(minX, X[i]);
minY = min(minY, Y[i]);
}
for(int i = 0; i < N; i++){
X[i] -= minX - 1;
Y[i] -= minY - 1;
pos.emplace_back(X[i], Y[i]);
}
sort(pos.begin(), pos.end());
long long int ans = 0;
for(auto [x, y] : pos){
ans = add(ans, MOD - xy.sum(y));
ans = add(ans, cnt.sum(y) * add(x, y) % MOD);
cnt.modify(y, 1);
xy.modify(y, add(x, y));
}
return add(0, ans);
}
/*
int DistanceSum(int N, int *X, int *Y) {
if(N > maxN) return 0;
int minX = INF, minY = INF;
for(int i = 0; i < N; i++){
minX = min(minX, X[i]);
minY = min(minY, Y[i]);
}
for(int i = 0; i < N; i++){
X[i] -= minX - 1;
Y[i] -= minY - 1;
mp[X[i]][Y[i]] = i + 1;
}
long long int ans = 0;
for(int i = 0; i < N; i++){
fill(d, d + N, INF);
fill(vis, vis + N, false);
d[i] = 0;
que.push(i);
vis[i] = true;
//cout << "i = " << i << endl;
while(que.size()){
int id = que.front(); que.pop();
for(int j = 0; j < 4; j++){
if(mp[X[id] + dir[j][0]][Y[id] + dir[j][1]]){
int nid = mp[X[id] + dir[j][0]][Y[id] + dir[j][1]] - 1;
if(!vis[nid]){
d[nid] = d[id] + 1;
//cout << "d[" << i << "][" << nid << "] = " << d[nid] << endl;
ans = add(ans, d[nid]);
vis[nid] = true;
que.push(nid);
}
}
}
}
}
return ans / 2;
}
*/
Compilation message
/tmp/ccgN6fvS.o: In function `_GLOBAL__sub_I_mp':
city.cpp:(.text.startup+0x4): relocation truncated to fit: R_X86_64_PC32 against `.bss'
city.cpp:(.text.startup+0x23): relocation truncated to fit: R_X86_64_PC32 against `.bss'
/usr/lib/gcc/x86_64-linux-gnu/9/libstdc++.a(vterminate.o): In function `__gnu_cxx::__verbose_terminate_handler()':
(.text._ZN9__gnu_cxx27__verbose_terminate_handlerEv+0x1a): relocation truncated to fit: R_X86_64_PC32 against `.bss._ZZN9__gnu_cxx27__verbose_terminate_handlerEvE11terminating'
(.text._ZN9__gnu_cxx27__verbose_terminate_handlerEv+0x27): relocation truncated to fit: R_X86_64_PC32 against `.bss._ZZN9__gnu_cxx27__verbose_terminate_handlerEvE11terminating'
/usr/bin/ld: failed to convert GOTPCREL relocation; relink with --no-relax
collect2: error: ld returned 1 exit status