#pragma once
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <assert.h>
#include <iostream>
#include <cmath>
#include <queue>
using namespace std;
/*
1 11 N <= 2e2 O(n^2)
2 21 N <= 2e3 O(n^2)
3 23 N <= 1e5
4 45 N <= 1e5
For 3 any two cell such x[i]<=x[j] then for all x such that x[i]<=x<=x[j] there is non empty cell there
*/
const int N=2001;
vector<int> ma[N];
bool vis[N];
int h[N];
int DistanceSum(int n, int x[], int y[])
{
if(n<=2000)
{
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if((abs(x[i]-x[j])+abs(y[i]-y[j]))==1)
{
ma[i].push_back(j);
ma[j].push_back(i);
}
}
}
long long ans1=0;
long long mod=1e9;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
h[j]=0;
vis[j]=0;
}
queue<int> q;
q.push(i);
vis[i]=1;
long long ans=0;
while(q.size())
{
int x=q.front();
ans+=(long long)h[x];
q.pop();
for(int y:ma[x])
{
if(!vis[y])
{
vis[y]=1;
h[y]=h[x]+1;
q.push(y);
}
}
}
ans1+=ans;
}
ans1/=2;
return ans1%mod;
}
long long other=0;
long long mod=1e9;
sort(x,x+n);
sort(y,y+n);
long long tot=0;
for(int i=0;i<n;i++)
tot+=x[i];
long long pr=0;
for(int i=0;i<n;i++)
{
pr+=x[i];
tot-=x[i];
other+=(((i+1)*x[i])-pr);
other+=(tot-((n-i-1)*x[i]));
}
long long sm=(other/2)%mod;
tot=0;
pr=0;
other=0;
for(int j=0;j<n;j++)
{
tot+=y[j];
}
for(int i=0;i<n;i++)
{
pr+=y[i];
tot-=y[i];
other+=(((i+1)*y[i])-pr);
other+=(tot-((n-i-1)*y[i]));
}
other/=2;
other%=mod;
return (other+sm)%mod;
}
Compilation message
city.cpp:1:9: warning: #pragma once in main file
1 | #pragma once
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
344 KB |
Output is correct |
2 |
Correct |
19 ms |
516 KB |
Output is correct |
3 |
Correct |
38 ms |
344 KB |
Output is correct |
4 |
Correct |
44 ms |
344 KB |
Output is correct |
5 |
Correct |
75 ms |
348 KB |
Output is correct |
6 |
Correct |
80 ms |
348 KB |
Output is correct |
7 |
Correct |
75 ms |
344 KB |
Output is correct |
8 |
Correct |
94 ms |
348 KB |
Output is correct |
9 |
Correct |
81 ms |
344 KB |
Output is correct |
10 |
Correct |
80 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
600 KB |
Output is correct |
2 |
Correct |
4 ms |
604 KB |
Output is correct |
3 |
Correct |
10 ms |
948 KB |
Output is correct |
4 |
Correct |
11 ms |
860 KB |
Output is correct |
5 |
Correct |
21 ms |
1340 KB |
Output is correct |
6 |
Correct |
21 ms |
1116 KB |
Output is correct |
7 |
Correct |
22 ms |
1116 KB |
Output is correct |
8 |
Correct |
20 ms |
1112 KB |
Output is correct |
9 |
Correct |
20 ms |
1116 KB |
Output is correct |
10 |
Incorrect |
21 ms |
1116 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |