题目

Analysis

曼哈顿距离($L1$ metric)最小生成树。

Implementation

下面的代码参考了 gispzjz 在比赛中的提交

#include <bits/stdc++.h>

using namespace std;
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x <<": " << (x) << endl
#define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#ifdef LOCAL
#define see(x) cout << #x << ": " << (x) << endl
#endif
#ifndef LOCAL
#define see(x)
#endif #define rep(n) for(int _ = 0; _ != (n); ++_)
//#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define Rng(i, n) for(int i = 0; i != (n); ++i)
#define rng(i, a, b) for(int i = (a); i < (b); ++i)
#define rno(i, b) for(int i = 0; i<(b); ++i)
#define rnc(i, a, b) for(int i = (a); i<=(b); ++i)
#define RNG(i, a) for(auto &i: (a))
#define dwn(i, r, l) for(int i = (r); i>=(l); i--) namespace std {
template<class T>
T begin(std::pair<T, T> p)
{
return p.first;
}
template<class T>
T end(std::pair<T, T> p)
{
return p.second;
}
} #if __cplusplus < 201402L
template<class Iterator>
std::reverse_iterator<Iterator> make_reverse_iterator(Iterator it)
{
return std::reverse_iterator<Iterator>(it);
}
#endif template<class Range>
std::pair<std::reverse_iterator<decltype(begin(std::declval<Range>()))>, std::reverse_iterator<decltype(begin(std::declval<Range>()))>> make_reverse_range(Range &&r)
{
return std::make_pair(make_reverse_iterator(::begin(r)), make_reverse_iterator(::end(r)));
} #define RRNG(x, cont) for (auto &x: make_reverse_range(cont)) template<class T> int sign(const T &a) { return a == 0 ? 0 : a > 0 ? 1 : -1; }
template<class T> inline T min(T a, T b, T c){return min(min(a, b), c);}
template<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}
template<class T> void Min(T &a, const T &b){ a = min(a, b); }
template<class T> void Max(T &a, const T &b){ a = max(a, b); } template<typename T> void println(const T &t) { cout << t << '\n'; }
template<typename T, typename ...Args> void println(const T &t, const Args &...rest) { cout << t << ' '; println(rest...); } template<typename T> void print(const T &t) { cout << t << ' '; } template<typename T, typename ...Args> void print(const T &t, const Args &...rest) { cout << t; print(rest...); } // this overload is chosen when there's only one argument
template<class T> void scan(T &t) { cin >> t; }
template<class T, class ...Args> void scan(T &a, Args &...rest) { cin >> a; scan(rest...); } using ll = long long;
using ull = unsigned long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pip = pair<int, pii>;
using szt = size_t;
using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using pli = pair<ll,int>;
using wg = vector<vpii>; //weighted graph int cas;
const double pi = acos(-1);
ll mod = 1e9 + 7;
//要求:0<=a<mod, 0<=b<=mod
template<class T>
inline void add_mod(T &a, const T &b) {
a += b;
if (a >= mod) a -= mod;
}
template<class T>
void sub_mod(T &a, const T &b){
a -= b;
if (a < 0) a += mod;
}
auto bo=[](int x){
bitset<5> a(x);
cout << a << endl;
}; //返回值:a中比k小的元素有多少个?
template<class V, class Cont>
int get_rank(const V &k, const Cont &a){
return std::lower_bound(all(a), k) - a.begin();
} mat operator*(const mat &a, const mat &b) {
mat c(a.size(), vec(b[0].size()));
for (size_t i = 0; i < a.size(); i++) {
for (size_t j = 0; j < a[0].size(); j++) {
if (a[i][j]) { // optimization for sparse matrix
for (size_t k = 0; k < b[0].size(); k++) {
add_mod(c[i][k], a[i][j] * b[j][k] % mod);
}
}
}
}
return c;
} vec operator*(const mat &a, const vec &b) {
vec c(a.size());
for (size_t i = 0; i < a.size(); i++) {
for (size_t j = 0; j < a[0].size(); j++) {
add_mod(c[i], a[i][j] * b[j] % mod);
}
}
return c;
} mat pow(mat a, ull n) {
mat res(a.size(), vec(a[0].size()));
for (size_t i = 0; i < a.size(); i++) {
res[i][i] = 1;
}
while (n) {
if (n & 1) {
res = res * a;
}
a = a * a;
n >>= 1;
}
return res;
} // Codeforces does not support __int128
//std::ostream& operator<<(std::ostream& os, __int128 T) {
// if (T<0) os<<"-";
// if (T>=10 ) os<<T/10;
// if (T<=-10) os<<(-(T/10));
// return os<<( (int) (T%10) >0 ? (int) (T%10) : -(int) (T%10) ) ;
//}
//
//__int128 LPOW(__int128 x, ll n) {
// __int128 res = 1;
// for (; n; n /= 2, x *= x, x %= mod) {
// if (n & 1) {
// res *= x;
// res %= mod;
// }
// }
// return res;
//} ll POW(ll x, ll n){
ll res = 1;
for (; n; n /= 2, x *= x, x %= mod) {
if (n & 1) {
res *= x;
res %= mod;
}
}
return res;
} ll INV(ll x) {
return POW(x, mod - 2);
} ll inv(ll x){
// see(x);
return x == 1? 1: (mod - mod/x * inv(mod%x) % mod);
} // 2D rotation
void rotate(double &x, double &y, double theta) {
double tx = cos(theta) * x - sin(theta) * y;
double ty = sin(theta) * x + cos(theta) * y;
x = tx, y = ty;
} struct dsu{
vector<int> par;
explicit dsu(int n){ // 0-indexed
par.resize(n);
rng(i, 0, n){
par[i] = i;
}
}
bool same(int x, int y){
return root(x) == root(y);
}
int root(int x){
return par[x] == x ? x : par[x] = root(par[x]);
}
void unite(int x, int y){
x = root(x);
y = root(y);
par[x] = y;
}
}; struct bit {
vector<int> a;
vector<int> id; // id[i]:键区间(i-lowbit(i), i]中,x+y取最小值的点的编号
bit(int n, int v = 0, bool manhattan = false) {
a.resize(n + 1);
for (int i = 1; i <= n; ++i) a[i] = v;
if(manhattan){
id.resize(n+1);
rng(i, 1, n+1) id[i] = -1;
}
} ll sum(int x) {
ll res = 0;
while (x) {
res += a[x];
x -= x & -x;
}
return res;
} ll sum(int l, int r) {
if (l > r) return 0;
return sum(r) - sum(l - 1);
} void add(int x, ll v) {
while (x < a.size()) {
a[x] += v;
x += x & -x;
}
} void min(int x, int v) {
while (x < a.size()) {
a[x] = std::min(a[x], v);
x += x & -x;
}
} int manhattan_min(int x){ // 返回x+y最小的点的编号
int ans = -1;
int res = INT_MAX;
while(x){
if(a[x] < res){
res = a[x];
ans = id[x];
}
x -= x & - x;
}
return ans;
} void manhattan_min(int x, int v, int i){
while(x < a.size()){
if(a[x] > v){
a[x] = v;
id[x] = i;
}
x += x & - x;
}
} void max(int x, int v) {
while (x < a.size()) {
a[x] = std::max(a[x], v);
x += x & -x;
}
} int min(int x) {
int res = INT_MAX;
while (x) {
res = std::min(res, a[x]);
x -= x & -x;
}
return res;
} int max(int x) {
int res = INT_MIN;
while (x) {
res = std::max(res, a[x]);
x -= x & -x;
}
return res;
}
}; namespace util{
int len(ll x){return snprintf(nullptr, 0, "%lld", x);}
vi get_d(ll x){
vi res;
while(x) {
res.pb(x%10);
x /= 10;
}
reverse(all(res));
return res;
}
template <class T> T parity(const T &a){
return a & 1;
}
template <class T>
void out (const vector<T> &a){
std::copy(a.begin(), a.end(), std::ostream_iterator<T>(std::cout, ", "));
cout << endl;
};
} using namespace util; #include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> using order_statistic_tree = __gnu_pbds::tree<
int,
__gnu_pbds::null_type,
greater<int>,
__gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update>; const ll LINF = LLONG_MAX/10;
const int INF = INT_MAX/10;
const int M = 5005; const int N = 1e5+5; int a[N]; struct point{
int x, y, id;
bool operator<(const point &rhs)const{
return x < rhs.x || (x == rhs.x && y < rhs.y);
}
int dis(const point rhs) {
return abs(x - rhs.x) + abs(y - rhs.y);
};
}; struct edge{
int u, v, l;
bool operator<(const edge &rhs) {
return l < rhs.l;
};
}; ll kruskal(vector<edge> &e, int n) { // 0-indexed
dsu a(n);
vi size(n);
fill(all(size), 1); sort(all(e)); ll sum = 0;
int cnt = 0;
RNG(x, e) {
int u = a.root(x.u), v = a.root(x.v);
if(u != v){
sum += 1LL * x.l/2 * size[u] * size[v];
a.par[u] = v;
size[v] += size[u];
++cnt;
if(cnt == n - 1) break;
}
}
return sum;
}; void R1(vector<point>& a, vector<edge> &e){
// 离散化
map<int, int> ls; RNG(p, a) {
ls[p.x - p.y];
} int cnt = 0; RNG(x, ls) {
x.second = ++cnt;
} bit b(cnt, INT_MAX, true); sort(all(a)); dwn(i, a.size() - 1, 0) {
// 为了好写,规定区域 R_1(s) 为 x >= x_s, x - y <= x_s - y_s
int pos = ls[a[i].x- a[i].y];
int j = b.manhattan_min(pos);
if (j != -1) {
e.pb({a[i].id, a[j].id, a[i].dis(a[j])});
}
b.manhattan_min(pos, a[i].x + a[i].y, i);
}
} ll manhattan(vector<point> &a){
vector<edge> e;
rng(i, 0, 4){
if(i == 1 || i == 3){ // 交换x,y坐标
RNG(j, a){
swap(j.x, j.y);
}
}
else if(i == 2){
RNG(j, a){
j.x = - j.x;
}
}
R1(a, e);
} return kruskal(e, a.size());
} int main() {
// Single Cut of Failure taught me
cout << std::fixed;
cout << setprecision(10);
ios::sync_with_stdio(false);
cin.tie(nullptr); #ifdef LOCAL
freopen("main.in", "r", stdin);
// freopen("main.out", "w", stdout);
#endif int n;
scan(n);
vector<point> p(n); rng(i, 0, n){
scan(p[i].x, p[i].y);
p[i].id = i;
} println(manhattan(p)); #ifdef LOCAL
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}

备忘

$R_1(s)\colon x \ge x_s, x - y \le x_s - y_s$,

$R_2(s)\colon y \ge y_s, x - y \ge x_s - y_s$,

$R_3(s) \colon y \le y_s, x + y \ge x_s + y_s$,

$R_4(s) \colon x \ge x_s, x+y \le x_s + y_s $ 。

$R_2$ 变换到 $R_1$: $(x,y) \to (y, x)$

$R_3$ 变换的 $R_1$: $(x, y) \to (-y, x)$

$R_4$ 变换到 $R_1$:$(x, y) \to (x, -y)$

$R_1, R_2$ 的离散化部分可共用, $R_3, R_4$ 的离散化部分可共用,但上面给出的实现并未这样做,这是一个可以优化的点。

扩展

欧几里得距离最小生成树也可用类似的划分平面的方法解决。

仍然可以按 $45^\circ$ 度划分平面。$\forall a, b \in R_1(s)$,在 $\triangle sab$ 中,有 $\angle{s} < \max(\angle{a}, \angle{b})$,于是由正弦定理可知 $|ab| < \max(|sa|,|sb|)$ 。

最新文章

  1. 先定一个小目标,自己封装个ajax
  2. C#软件设计——小话设计模式原则之:开闭原则OCP
  3. php常见问题
  4. HideProcess
  5. 在WindowsServer2008服务器上安装SQLServer2008R2
  6. maven小项目注册服务(三)--web模块
  7. 各种电子面单_Api接口
  8. everything搜索工具小技巧
  9. Java中线程顺序执行
  10. iOS_11_tableViewCell使用alertView变更数据
  11. ASP.NET 5 Beta8 发布
  12. jquery给html元素添加内容
  13. 黄油刀ButterKnife的使用
  14. HTML5 CSS3 诱人的实例 : 网页加载进度条的实现,下载进度条等
  15. c文件操作
  16. 1006 Tick and Tick
  17. sql server导出大批量数据
  18. java AOP使用注解@annotation方式实践
  19. Managing Hierarchical Data in MySQL(邻接表模型)[转载]
  20. 城市里的间谍B901

热门文章

  1. 【洛谷4884】多少个1?(BSGS)
  2. MRCA|Wright–Fisher population genetic model|SNP rate
  3. THinkPHP 5.0 域名路由
  4. 在maven项目中 配置代理对象远程调用crm
  5. ML.NET技术研究系列1-入门篇
  6. 【算法】Fibonacci(斐波那契数列)相关问题
  7. C++ 学习笔记 (六) 继承- 子类与父类有同名函数,变量
  8. 2018.11.5 Nescafe26 T1 小猫爬山
  9. python2, 3环境变量配置(win10下)
  10. h5获取摄像头拍照功能