# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1018556 |
2024-07-10T06:48:54 Z |
pcc |
Ideal city (IOI12_city) |
C++17 |
|
1000 ms |
4056 KB |
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define fs first
#define sc second
#define ll long long
const int mod = 1e9;
const int mxn = 1e5+10;
vector<int> paths[mxn];
vector<pii> v;
int N;
int mad(int a,int b){
a %= mod;
a += b;
return a>=mod?a%mod:a;
}
int mub(int a,int b){
return mad(a,mod-b%mod);
}
namespace SUB1{
int dist[mxn];
queue<int> q;
int BFS(int now){
memset(dist,-1,sizeof(dist));
dist[now] = 0;
q.push(now);
while(!q.empty()){
auto now = q.front();
q.pop();
for(auto nxt:paths[now]){
if(dist[nxt] == -1){
dist[nxt] = dist[now]+1;
q.push(nxt);
}
}
}
int re = 0;
for(int i = 0;i<N;i++)re = (re+dist[i])%mod;
return re;
}
int GO(int NN,int* xx,int *yy){
N = NN;
for(int i = 0;i<N;i++){
v.push_back(pii(xx[i],yy[i]));
}
vector<int> perm;
for(int i = 0;i<N;i++)perm.push_back(i);
sort(perm.begin(),perm.end(),[](int a,int b){return v[a].fs == v[b].fs?v[a].sc<v[b].sc:v[a].fs<v[b].fs;});
for(int i = 1;i<N;i++){
int now = perm[i],pre = perm[i-1];
if(v[now].fs == v[pre].fs&&v[now].sc == v[pre].sc+1){
paths[now].push_back(pre);
paths[pre].push_back(now);
}
}
sort(perm.begin(),perm.end(),[](int a,int b){return v[a].sc == v[b].sc?v[a].fs<v[b].fs:v[a].sc<v[b].sc;});
for(int i = 1;i<N;i++){
int now = perm[i],pre = perm[i-1];
if(v[now].sc == v[pre].sc&&v[now].fs == v[pre].fs+1){
paths[now].push_back(pre);
paths[pre].push_back(now);
}
}
int ans = 0;
for(int i = 0;i<N;i++){
ans += BFS(i);
ans %= mod;
}
assert(ans%2 == 0);
return ans>>1;
}
}
namespace SUB2{
map<int,int> mx,my;
int GO(int NN,int *X,int *Y){
N = NN;
for(int i = 0;i<N;i++){
mx[X[i]]++;
my[Y[i]]++;
}
int ans = 0;
int pref = 0,suf = 0,cnt = 0;
for(auto &i:mx)suf = mad(suf,1ll*i.fs*i.sc%mod);
for(auto &i:mx){
int t1 = mub(1ll*i.fs*cnt%mod,pref);
int t2 = mub(suf,1ll*i.fs*(N-cnt)%mod);
t1 = 1ll*t1*i.sc%mod;t2 = 1ll*t2*i.sc%mod;
ans = mad(ans,t1);ans = mad(ans,t2);
pref = mad(pref,1ll*i.fs*i.sc%mod);
cnt = mad(cnt,i.sc);
suf = mub(suf,1ll*i.fs*i.sc%mod);
}
pref = suf = cnt = 0;
for(auto &i:my)suf = mad(suf,1ll*i.fs*i.sc%mod);
for(auto &i:my){
int t1 = mub(1ll*i.fs*cnt%mod,pref);
int t2 = mub(suf,1ll*i.fs*(N-cnt)%mod);
t1 = 1ll*t1*i.sc%mod;t2 = 1ll*t2*i.sc%mod;
ans = mad(ans,t1);ans = mad(ans,t2);
pref = mad(pref,1ll*i.fs*i.sc%mod);
cnt = mad(cnt,i.sc);
suf = mub(suf,1ll*i.fs*i.sc%mod);
}
assert(ans%2 == 0);
return ans>>1;
}
}
int DistanceSum(int N, int *X, int *Y) {
return SUB1::GO(N,X,Y);
return SUB2::GO(N,X,Y);
if(N<=2000)return SUB1::GO(N,X,Y);
else return SUB2::GO(N,X,Y);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3160 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
1 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Correct |
2 ms |
3164 KB |
Output is correct |
6 |
Correct |
2 ms |
3164 KB |
Output is correct |
7 |
Correct |
3 ms |
3164 KB |
Output is correct |
8 |
Correct |
3 ms |
3164 KB |
Output is correct |
9 |
Correct |
2 ms |
3164 KB |
Output is correct |
10 |
Correct |
2 ms |
3164 KB |
Output is correct |
11 |
Correct |
3 ms |
3164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
3160 KB |
Output is correct |
2 |
Correct |
19 ms |
3160 KB |
Output is correct |
3 |
Correct |
48 ms |
3164 KB |
Output is correct |
4 |
Correct |
41 ms |
3164 KB |
Output is correct |
5 |
Correct |
65 ms |
3164 KB |
Output is correct |
6 |
Correct |
66 ms |
3272 KB |
Output is correct |
7 |
Correct |
76 ms |
3164 KB |
Output is correct |
8 |
Correct |
73 ms |
3160 KB |
Output is correct |
9 |
Correct |
72 ms |
3164 KB |
Output is correct |
10 |
Correct |
68 ms |
3268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1034 ms |
4056 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1039 ms |
4052 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |