#include <cmath>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <iostream>
using namespace std;
int const MAXN=1e5+10;
vector<int>nei[MAXN]={};
int mod=1e9;
long long ans=0;
vector<int>dx={0,0,-1,1},dy={-1,1,0,0};
int dis[MAXN]={};
void bfs(int x)
{
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
pq.push({0,x});
dis[x]=0;
while (pq.size())
{
auto y=pq.top();
pq.pop();
for (auto j:nei[y.second])
{
if (dis[j]>y.first+1)
{
dis[j]=y.first+1;
pq.push({dis[j],j});
}
}
}
}
int DistanceSum(int N, int X[], int Y[])
{
map<int,vector<int>>d;
map<pair<int,int>,vector<int>>ex;
pair<int,int>a[N];
for (int i=0;i<N;i++)
{
a[i]={X[i],Y[i]};
ex[{X[i],Y[i]}].push_back(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 (auto i:ex)
{
int x=i.first.first,y=i.first.second;
for (int j=0;j<4;j++)
{
if (ex.find({x+dx[j],y+dy[j]})!=ex.end())
{
for (auto k:i.second)
for (auto l:ex[{x+dx[j],y+dy[j]}])
nei[k].push_back(l);
}
}
}
for (int i=0;i<N;i++)
dis[i]=1e9+10;
for (int i=0;i<N;i++)
{
bfs(i);
for (int j=i+1;j<N;j++)
{
if (a[i]==a[j])
continue;
ans=(ans+dis[j])%mod;
}
for (int j=0;j<N;j++)
dis[j]=1e9+10;
}
return ans;
}
Compilation message
city.cpp: In function 'int DistanceSum(int, int*, int*)':
city.cpp:48: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]
48 | if (i.second.size()!=i.second.back()-i.second[0]+1)
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2904 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
3180 KB |
Output is correct |
2 |
Correct |
54 ms |
3164 KB |
Output is correct |
3 |
Correct |
134 ms |
3252 KB |
Output is correct |
4 |
Correct |
127 ms |
3256 KB |
Output is correct |
5 |
Correct |
256 ms |
3164 KB |
Output is correct |
6 |
Correct |
226 ms |
3164 KB |
Output is correct |
7 |
Correct |
209 ms |
3348 KB |
Output is correct |
8 |
Correct |
221 ms |
3352 KB |
Output is correct |
9 |
Correct |
272 ms |
3164 KB |
Output is correct |
10 |
Correct |
273 ms |
3336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
5468 KB |
Output is correct |
2 |
Correct |
14 ms |
5724 KB |
Output is correct |
3 |
Correct |
33 ms |
9444 KB |
Output is correct |
4 |
Correct |
33 ms |
9560 KB |
Output is correct |
5 |
Correct |
79 ms |
16288 KB |
Output is correct |
6 |
Correct |
82 ms |
16216 KB |
Output is correct |
7 |
Correct |
82 ms |
16124 KB |
Output is correct |
8 |
Correct |
73 ms |
16124 KB |
Output is correct |
9 |
Correct |
83 ms |
16192 KB |
Output is correct |
10 |
Correct |
97 ms |
19008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1038 ms |
6488 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |