//#include "grader.cpp"
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int mod = 1e9;
struct Point
{
long long x, y;
Point(){}
Point(long long x, long long y)
{
this->x = x;
this->y = y;
}
};
int n;
vector <Point> v;
void init()
{
int ind = 0;
set <int> s;
map <int, int> code;
for(Point p: v) s.insert(p.x);
for(int x: s) code[x] = ind, ind++;
for(Point &p: v) p.x = code[p.x];
//-----------------------
ind = 0;
s.clear();
code.clear();
for(Point p: v) s.insert(p.y);
for(int y: s) code[y] = ind, ind++;
for(Point &p: v) p.y = code[p.y];
}
namespace Subtask2
{
const vector <pair <int, int>> jumps = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
const int MAXN = 2005;
bool exists[MAXN][MAXN];
int dist[MAXN][MAXN];
void bfs(Point p)
{
queue <Point> q;
for(Point p: v) dist[p.x][p.y] = -1;
q.push(p);
dist[p.x][p.y] = 0;
while(q.empty()==false)
{
p = q.front();
q.pop();
for(pair <int, int> j: jumps)
{
if(p.x+j.first>=0 && p.y+j.second>=0
&& dist[p.x+j.first][p.y+j.second]==-1
&& exists[p.x+j.first][p.y+j.second]==true)
{
q.push(Point(p.x+j.first, p.y+j.second));
dist[p.x+j.first][p.y+j.second] = dist[p.x][p.y] + 1;
}
}
}
}
long long solve()
{
for(Point p: v) exists[p.x][p.y] = true;
long long answer = 0;
for(Point p: v)
{
bfs(p);
for(Point other: v) answer = (answer + dist[other.x][other.y])%mod;
}
return answer;
}
};
namespace Subtask3
{
long long solve()
{
long long answer = 0;
sort(v.begin(), v.end(),
[&](Point A, Point B)
{
if(A.x!=B.x) return A.x<B.x;
return A.y<B.y;
});
long long sum = 0;
for(int i = 0;i<n;i++)
{
answer = (answer + (i*1LL*v[i].x - sum))%mod;
sum += v[i].x;
}
sort(v.begin(), v.end(),
[&](Point A, Point B)
{
if(A.y!=B.y) return A.y<B.y;
return A.x<B.x;
});
sum = 0;
for(int i = 0;i<n;i++)
{
answer = (answer + (i*1LL*v[i].y - sum))%mod;
sum += v[i].y;
}
return answer;
}
};
int DistanceSum(int N, int *X, int *Y)
{
n = N;
for(int i = 0;i<n;i++) v.push_back(Point(X[i], Y[i]));
init();
//if(n<=2*1000) return Subtask2::solve()/2;
return Subtask3::solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
256 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
1148 KB |
Output is correct |
2 |
Correct |
13 ms |
1276 KB |
Output is correct |
3 |
Correct |
34 ms |
2300 KB |
Output is correct |
4 |
Correct |
35 ms |
2300 KB |
Output is correct |
5 |
Correct |
71 ms |
4208 KB |
Output is correct |
6 |
Correct |
71 ms |
4112 KB |
Output is correct |
7 |
Correct |
78 ms |
4208 KB |
Output is correct |
8 |
Correct |
68 ms |
4088 KB |
Output is correct |
9 |
Correct |
75 ms |
4080 KB |
Output is correct |
10 |
Correct |
98 ms |
6508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
1148 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |