#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <iostream>
using namespace std;
int const MAXN=1e5+10;
vector<pair<int,int>>nei[MAXN]={};
int mod=1e9;
long long ans=0;
vector<int>dx={0,0,-1,1},dy={-1,1,0,0};
map<pair<int,int>,long long>dis;
map<pair<int,int>,long long>ex;
map<pair<int,int>,bool>vis;
void bfs(int i,int j)
{
set<pair<int,pair<int,int>>>s;
s.insert({0,{i,j}});
dis[{i,j}]=0;
while (s.size())
{
auto r=*begin(s);
s.erase(r);
if (vis[r.second])
continue;
vis[r.second]=1;
int x,y;
tie(x,y)=r.second;
for (int j=0;j<4;j++)
{
if (ex.find({x+dx[j],y+dy[j]})!=ex.end())
{
if (dis[{x+dx[j],y+dy[j]}]>r.first+1)
{
dis[{x+dx[j],y+dy[j]}]=r.first+1;
s.insert({r.first+1,{x+dx[j],y+dy[j]}});
}
}
}
}
}
int DistanceSum(int N, int X[], int Y[])
{
map<int,vector<int>>d;
pair<int,int>a[N];
int minX=((1ll<<31)-1),minY=((1ll<<31)-1);
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;
Y[i]-=minY;
a[i]={X[i],Y[i]};
ex[{X[i],Y[i]}]++;
d[X[i]].push_back(Y[i]);
}
bool subtask_3=1;
for (auto& i:d)
{
sort(begin(i.second),end(i.second));
if (i.second.size()!=i.second.back()-i.second[0]+1)
{
subtask_3=0;
break;
}
}
if (subtask_3)
{
sort(Y,Y+N);
sort(X,X+N);
int ans=0;
long long szx=0,sux=0,szy=0,suy=0;
for (int i=0;i<N;i++)
{
long long g=1ll*Y[i]*szy-suy;
g%=mod;
ans=(ans+g)%mod;
g=1ll*X[i]*szx-sux;
g%=mod;
ans=(ans+g)%mod;
szy++;
suy+=Y[i];
szx++;
sux+=X[i];
}
return ans;
}
for (int i=0;i<N;i++)
{
for (int j=0;j<N;j++)
dis[a[j]]=1e9+10;
bfs(a[i].first,a[i].second);
for (int j=i+1;j<N;j++)
ans=(ans+dis[a[j]]*ex[a[j]])%mod;
vis={};
}
return ans;
}
Compilation message
city.cpp: In function 'int DistanceSum(int, int*, int*)':
city.cpp:63:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} [-Wsign-compare]
63 | if (i.second.size()!=i.second.back()-i.second[0]+1)
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2648 KB |
Output is correct |
3 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
902 ms |
2984 KB |
Output is correct |
2 |
Correct |
854 ms |
2976 KB |
Output is correct |
3 |
Execution timed out |
1026 ms |
2904 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
4440 KB |
Output is correct |
2 |
Correct |
13 ms |
4444 KB |
Output is correct |
3 |
Correct |
32 ms |
7000 KB |
Output is correct |
4 |
Correct |
42 ms |
7176 KB |
Output is correct |
5 |
Correct |
62 ms |
11332 KB |
Output is correct |
6 |
Correct |
81 ms |
11376 KB |
Output is correct |
7 |
Correct |
64 ms |
11344 KB |
Output is correct |
8 |
Correct |
70 ms |
11356 KB |
Output is correct |
9 |
Correct |
73 ms |
11596 KB |
Output is correct |
10 |
Correct |
89 ms |
14060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1056 ms |
6996 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |