#include <cmath>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <iostream>
using namespace std;
long long const MAXN=1e5+10;
vector<long long>nei[MAXN]={};
long long mod=1e9;
long long ans=0;
vector<long long>dx={0,0,-1,1},dy={-1,1,0,0};
long long dis[MAXN]={};
bool vis[MAXN]={};
void bfs(long long x)
{
set<pair<int,int>>s;
s.insert({0,x});
dis[x]=0;
while (s.size())
{
auto y=*begin(s);
s.erase(y);
if (vis[y.second])
continue;
vis[y.second]=1;
for (auto j:nei[y.second])
{
if (!vis[j])
{
dis[j]=y.first+1;
s.insert({dis[j],j});
}
}
}
}
int DistanceSum(int N, int X[], int Y[])
{
map<long long,vector<long long>>d;
map<pair<long long,long long>,long long>ex,cnt;
for (long long i=0;i<N;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);
long long ans=0;
long long szx=0,sux=0,szy=0,suy=0;
for (long long 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++)
{
if (abs(X[i]-X[j])+abs(Y[i]-Y[j])==1)
nei[i].push_back(j);
}
}
for (long long i=0;i<N;i++)
dis[i]=0;
for (long long i=0;i<N;i++)
{
bfs(i);
for (long long j=i+1;j<N;j++)
{
dis[j]%=mod;
ans=(ans+dis[j])%mod;
}
for (long long j=0;j<N;j++)
dis[j]=vis[j]=0;
}
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<long long int>::size_type' {aka 'long unsigned int'} and '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long 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 |
3420 KB |
Output is correct |
2 |
Correct |
1 ms |
3420 KB |
Output is correct |
3 |
Incorrect |
1 ms |
3420 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
149 ms |
3420 KB |
Output is correct |
2 |
Correct |
147 ms |
3568 KB |
Output is correct |
3 |
Correct |
332 ms |
3600 KB |
Output is correct |
4 |
Correct |
321 ms |
3600 KB |
Output is correct |
5 |
Correct |
627 ms |
3640 KB |
Output is correct |
6 |
Correct |
645 ms |
3644 KB |
Output is correct |
7 |
Correct |
488 ms |
3576 KB |
Output is correct |
8 |
Correct |
613 ms |
3648 KB |
Output is correct |
9 |
Correct |
801 ms |
3652 KB |
Output is correct |
10 |
Correct |
794 ms |
3652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
3928 KB |
Output is correct |
2 |
Correct |
8 ms |
3928 KB |
Output is correct |
3 |
Correct |
20 ms |
4728 KB |
Output is correct |
4 |
Correct |
17 ms |
4684 KB |
Output is correct |
5 |
Correct |
35 ms |
5880 KB |
Output is correct |
6 |
Correct |
39 ms |
5780 KB |
Output is correct |
7 |
Correct |
38 ms |
5616 KB |
Output is correct |
8 |
Correct |
35 ms |
6236 KB |
Output is correct |
9 |
Correct |
38 ms |
5656 KB |
Output is correct |
10 |
Correct |
47 ms |
8276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1038 ms |
4776 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |