#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();
if (y.first>dis[y.second])
continue;
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;
for (int i=0;i<N;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++)
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 |
2904 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 |
59 ms |
3160 KB |
Output is correct |
2 |
Correct |
54 ms |
3164 KB |
Output is correct |
3 |
Correct |
134 ms |
3240 KB |
Output is correct |
4 |
Correct |
125 ms |
3160 KB |
Output is correct |
5 |
Correct |
259 ms |
3160 KB |
Output is correct |
6 |
Correct |
228 ms |
3164 KB |
Output is correct |
7 |
Correct |
206 ms |
3164 KB |
Output is correct |
8 |
Correct |
224 ms |
3332 KB |
Output is correct |
9 |
Correct |
268 ms |
3328 KB |
Output is correct |
10 |
Correct |
270 ms |
3408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
5464 KB |
Output is correct |
2 |
Correct |
14 ms |
5464 KB |
Output is correct |
3 |
Correct |
35 ms |
9036 KB |
Output is correct |
4 |
Correct |
38 ms |
9296 KB |
Output is correct |
5 |
Correct |
78 ms |
15556 KB |
Output is correct |
6 |
Correct |
77 ms |
15440 KB |
Output is correct |
7 |
Correct |
75 ms |
15320 KB |
Output is correct |
8 |
Correct |
84 ms |
15480 KB |
Output is correct |
9 |
Correct |
88 ms |
15472 KB |
Output is correct |
10 |
Correct |
125 ms |
18256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1033 ms |
6164 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |