//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <set>
#include <queue>
#define ll long long
#define maxn 100005
#define mod 1000000000
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
vector<pii> a;
vector<int> px[maxn], py[maxn];
struct seg{
int ind = 0, l = 0, r = 0;
seg(int a, int b, int c) {
ind = a, l = b, r = c;
}
seg();
};
vector<seg> gx[maxn], gy[maxn];
vector<int> adj[maxn];
ll cnt[maxn], siz[maxn];
ll ans = 0, tot;
void dfs(int n, int par) {
//cout << n << " " << par << endl;
siz[n] = cnt[n];
for (int v:adj[n]) {
if (v != par) {
dfs(v, n);
siz[n] += siz[v];
}
}
ans += siz[n] * (tot - siz[n]);
}
int DistanceSum(int N, int *X, int *Y) {
tot = N;
int mx = 2147483647, my = 2147483647;
for (int i = 0;i < N;i++) mx = min(mx, X[i]), my = min(my, Y[i]);
for (int i = 0;i < N;i++) {
a.push_back(make_pair(X[i] - mx, Y[i] - my));
//cout << X[i] - mx << " " << Y[i] - my << endl;
px[X[i] - mx].push_back(Y[i] - my);
py[Y[i] - my].push_back(X[i] - mx);
}
int ind = 0, st = 0, num = 0;
for (int i = 0;i < N;i++) {
sort(px[i].begin(), px[i].end());
for (int j = 0;j < px[i].size();j++) {
if (!(j && px[i][j] == px[i][j - 1] + 1)) {
if (j) {
gx[i].push_back(seg(ind, st, px[i][j - 1]));
cnt[ind++] = num;
num = 0;
}
st = px[i][j];
}
num++;
}
if (px[i].size() ) {
gx[i].push_back(seg(ind, st, px[i].back()));
cnt[ind++] = num;
num = 0;
}
}
for (int i = 0;i < N;i++) {
int cur = 0;
//cout << i << endl;
for (seg j: gx[i]) {
//cout << j.ind << " " << j.l << " " << j.r << ' ' << cnt[j.ind] << endl;
while (cur < gx[i + 1].size() && gx[i + 1][cur].l <= j.r) {
if (gx[i + 1][cur].r >= j.l) {
adj[j.ind].push_back(gx[i + 1][cur].ind);
adj[gx[i + 1][cur].ind].push_back(j.ind);
}
cur++;
}
cur--;
}
}
dfs(0, -1);
for(int i = 0;i < N;i++) adj[i].clear(), cnt[i] = 0, siz[i] = 0;
ind = 0, st = 0, num = 0;
for (int i = 0;i < N;i++) {
sort(py[i].begin(), py[i].end());
for (int j = 0;j < py[i].size();j++) {
if (!(j && py[i][j] == py[i][j - 1] + 1)) {
if (j) {
gy[i].push_back(seg(ind, st, py[i][j - 1]));
cnt[ind++] = num;
num = 0;
}
st = py[i][j];
}
num++;
}
if (py[i].size() ) {
gy[i].push_back(seg(ind, st, py[i].back()));
cnt[ind++] = num;
num = 0;
}
}
for (int i = 0;i < N;i++) {
int cur = 0;
//cout << i << endl;
for (seg j: gy[i]) {
//cout << j.ind << " " << j.l << " " << j.r << ' ' << cnt[j.ind] << endl;
while (cur < gy[i + 1].size() && gy[i + 1][cur].l <= j.r) {
if (gy[i + 1][cur].r >= j.l) {
adj[j.ind].push_back(gy[i + 1][cur].ind);
adj[gy[i + 1][cur].ind].push_back(j.ind);
}
cur++;
}
cur--;
}
}
dfs(0, -1);
return ans;
}
/*
int main() {
io
int n;
cin >> n;
int x[n], y[n];
for (int i = 0;i < n;i++) cin >> x[i];
for (int i = 0;i <n ;i++) cin >> y[i];
cout << DistanceSum(n, x, y);
}
/*
11
2 2 3 3 4 4 4 4 5 5 5
5 6 3 6 3 4 5 6 3 4 6
6
0 0 1 1 1 1
0 1 1 2 3 4
*/
Compilation message
city.cpp:143:1: warning: "/*" within comment [-Wcomment]
143 | /*
|
city.cpp: In function 'int DistanceSum(int, int*, int*)':
city.cpp:59:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for (int j = 0;j < px[i].size();j++) {
| ~~^~~~~~~~~~~~~~
city.cpp:81:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<seg>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | while (cur < gx[i + 1].size() && gx[i + 1][cur].l <= j.r) {
| ~~~~^~~~~~~~~~~~~~~~~~
city.cpp:97:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | for (int j = 0;j < py[i].size();j++) {
| ~~^~~~~~~~~~~~~~
city.cpp:119:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<seg>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
119 | while (cur < gy[i + 1].size() && gy[i + 1][cur].l <= j.r) {
| ~~~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12012 KB |
Output is correct |
2 |
Incorrect |
8 ms |
12012 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
12140 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
13292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
13684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |