#include <bits/stdc++.h>
using namespace std;
const int Mod = 1e9, Nmax = 1e5 + 5;
typedef long long ll;
int n, x[Nmax], y[Nmax], A[2005][2005], d[2005][2005];
class AIB
{
ll a[Nmax]; int b[Nmax];
int ub(int x) { return (x&(-x)); }
public:
int query(int x)
{
ll sum1 = 0, sum2 = 0;
int cnt1 = 0, cnt2 = 0;
int i;
for(i=x; i; i-=ub(i))
sum1 += a[i], cnt1 += b[i];
for(i=n; i; i-=ub(i))
sum2 += a[i], cnt2 += b[i];
sum2 -= sum1, cnt2 -= cnt1;
sum1 = (ll) x * cnt1 - sum1;
sum2 = sum2 - (ll) x * cnt2;
return (sum1 + sum2) % Mod;
}
void update(int pos)
{
int i;
for(i=pos; i<=n; i+=ub(i))
a[i] += pos, b[i] ++;
}
} aib;
int solve1()
{
int i;
vector<int> p[Nmax];
for(i=0; i<n; ++i) p[x[i]].push_back(y[i]);
ll sumx = 0, cnt = 0, ans = 0;
for(i=1; i<=n; ++i)
for(auto it : p[i])
{
ans += aib.query(it);
ans %= Mod;
aib.update(it);
ans += (ll) i * cnt - sumx;
ans %= Mod;
++cnt; sumx += i;
}
return (int)ans;
}
void bfs(int x, int y)
{
queue< pair<int,int> > q;
q.push({x, y});
d[x][y] = 0;
while(q.size())
{
tie(x, y) = q.front();
q.pop();
if(A[x-1][y] && d[x-1][y] == -1)
{
d[x-1][y] = d[x][y] + 1;
q.push({x-1, y});
}
if(A[x+1][y] && d[x+1][y] == -1)
{
d[x+1][y] = d[x][y] + 1;
q.push({x+1, y});
}
if(A[x][y-1] && d[x][y-1] == -1)
{
d[x][y-1] = d[x][y] + 1;
q.push({x, y-1});
}
if(A[x][y+1] && d[x][y+1] == -1)
{
d[x][y+1] = d[x][y] + 1;
q.push({x, y+1});
}
}
}
int DistanceSum(int N, int *X, int *Y)
{
int i, j, xmin = 2e9, ymin = 2e9; n = N;
for(i=0; i<N; ++i)
{
x[i] = X[i];
y[i] = Y[i];
xmin = min(xmin, x[i]);
ymin = min(ymin, y[i]);
}
for(i=0; i<N; ++i)
x[i] -= xmin - 1, y[i] -= ymin - 1;
if(N > 2000)
return solve1();
// cerr << solve1() << '\n';
ll ans = 0;
for(i=0; i<n; ++i) A[x[i]][y[i]] = 1;
for(i=0; i<n; ++i)
{
for(j=0; j<n; ++j) d[x[j]][y[j]] = -1;
bfs(x[i], y[i]);
for(j=i+1; j<n; ++j) ans += d[x[j]][y[j]];
ans %= Mod;
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
512 KB |
Output is correct |
7 |
Correct |
3 ms |
512 KB |
Output is correct |
8 |
Correct |
3 ms |
640 KB |
Output is correct |
9 |
Correct |
4 ms |
512 KB |
Output is correct |
10 |
Correct |
4 ms |
512 KB |
Output is correct |
11 |
Correct |
4 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
872 KB |
Output is correct |
2 |
Correct |
35 ms |
816 KB |
Output is correct |
3 |
Correct |
85 ms |
1144 KB |
Output is correct |
4 |
Correct |
77 ms |
1024 KB |
Output is correct |
5 |
Correct |
167 ms |
1152 KB |
Output is correct |
6 |
Correct |
114 ms |
1192 KB |
Output is correct |
7 |
Correct |
184 ms |
1552 KB |
Output is correct |
8 |
Correct |
101 ms |
1576 KB |
Output is correct |
9 |
Correct |
107 ms |
1012 KB |
Output is correct |
10 |
Correct |
89 ms |
1016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
3200 KB |
Output is correct |
2 |
Correct |
15 ms |
3456 KB |
Output is correct |
3 |
Correct |
18 ms |
4312 KB |
Output is correct |
4 |
Correct |
28 ms |
4296 KB |
Output is correct |
5 |
Correct |
44 ms |
5880 KB |
Output is correct |
6 |
Correct |
49 ms |
5716 KB |
Output is correct |
7 |
Correct |
50 ms |
5852 KB |
Output is correct |
8 |
Correct |
37 ms |
5852 KB |
Output is correct |
9 |
Correct |
30 ms |
5624 KB |
Output is correct |
10 |
Correct |
67 ms |
6236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
3284 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |