이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
const int mod = 1e9;
typedef pair<int, int> pii;
int n;
int x[maxn], y[maxn];
int prefX[maxn], prefY[maxn];
// -------- BFS STUFF --------
int dist[maxn];
vector<int> grafo[maxn];
int linha[] = {-1, 1, 0, 0};
int coluna[] = {0, 0, -1, 1};
// ---------------------------
void bfs(int u)
{
queue<int> fila;
dist[u] = 0, fila.push(u);
while (!fila.empty())
{
int u = fila.front();
fila.pop();
for (auto v: grafo[u])
if (dist[v] == -1)
dist[v] = dist[u]+1, fila.push(v);
}
}
int solve_small(void)
{
int ans = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (j == i) continue;
if (abs(x[i]-x[j])+abs(y[i]-y[j]) == 1)
grafo[i].push_back(j);
}
}
for (int i = 1; i <= n; i++)
{
memset(dist, -1, sizeof dist);
bfs(i);
for (int j = 1; j < i; j++)
ans = (ans+dist[j])%mod;
}
return ans;
}
int DistanceSum(int N, int *X, int *Y)
{
n = N;
for (int i = 1; i <= n; i++)
x[i] = X[i-1], y[i] = Y[i-1];
if (n <= 2000) return solve_small();
sort(x+1, x+n+1); sort(y+1, y+n+1);
for (int i = 1; i <= n; i++)
{
prefX[i] = (prefX[i-1] + x[i])%mod;
prefY[i] = (prefY[i-1] + y[i])%mod;
}
int ans = 0;
for (int i = 1; i <= n; i++)
{
long long soma = (1ll*(i-1)*x[i] - 1ll*prefX[i-1] + 1ll*mod)%mod;
ans = (ans+soma)%mod;
soma = (1ll*(i-1)*y[i] - 1ll*prefY[i-1] + 1ll*mod)%mod;
ans = (ans+soma)%mod;
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |