# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
134603 | sealnot123 | Ideal city (IOI12_city) | C++14 | 32 ms | 12408 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define SZ(a) (int)(a).size()
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PLL;
typedef pair<int,int> PII;
typedef double D;
typedef long double LD;
const int N = 100005;
vector<int> compx, compy;
int lastx[N], lasty[N];
int orderx[N], ordery[N];
vector<int> g[N];
LL ans;
LL dp[N], sz[N];
int n;
int *XX, *YY;
void add_edge(int a, int b){
g[a].pb(b); g[b].pb(a);
}
bool cmpx(int a, int b){
return XX[a] < XX[b];
}
bool cmpy(int a, int b){
return YY[a] < YY[b];
}
void dfs(int u, int v){
sz[u] = 1;
for(int e : g[u]){
if(e == v) continue;
ans += (dp[e]+sz[u])*sz[u] + dp[u]*sz[e];
dp[u] += dp[e] + sz[u];
}
}
int DistanceSum(int nn, int *X, int *Y) {
n = nn;
XX = X;
YY = Y;
int i,j,k,l,a,b,c,d;
for(i=0;i<n;i++) compx.pb(X[i]), compy.pb(Y[i]);
sort(all(compx)); sort(all(compy));
compx.erase(unique(all(compx)), compx.end());
compy.erase(unique(all(compy)), compy.end());
for(i=0;i<n;i++) orderx[i] = ordery[i] = i;
sort(orderx,orderx+n,cmpx);
sort(ordery,ordery+n,cmpy);
memset(lastx, -1, sizeof lastx);
memset(lasty, -1, sizeof lasty);
for(i = 0; i < n; i++){
b = orderx[i];
c = lower_bound(all(compx), X[b]) - compx.end();
a = lower_bound(all(compy), Y[b]) - compy.end();
if(lasty[a] != -1) add_edge(lasty[a], c);
lasty[a] = c;
}
dfs(0,0);
for(i=0;i<n;i++) g[i].clear();
memset(dp, 0, sizeof dp);
memset(sz, 0, sizeof sz);
for(i = 0; i < n; i++){
b = ordery[i];
c = lower_bound(all(compy), Y[b]) - compy.end();
a = lower_bound(all(compx), X[b]) - compx.end();
if(lastx[a] != -1) add_edge(lastx[a], c);
lastx[a] = c;
}
dfs(0,0);
return ans;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |