#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 = 2326;
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) {
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);
}
/*
inline int add(int a, int b){
return (a + b >= MOD ? a + b - MOD : a + b);
}
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
city.cpp: In function 'int DistanceSum(int, int*, int*)':
city.cpp:50:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
50 | for(auto [x, y] : pos){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
876 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
876 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |