\(\mathcal{Description}\)

  Link.

  平面上有 \(n\) 个互不重合的点 \((x_{1..n},y_{1..n})\),求其两两曼哈顿距离的前 \(m\) 小值。

  \(n,m\le2.5\times10^5\)。

\(\mathcal{Solution}\)

  会做,但不完全会做。

  数前 \(k\) 小的一种技术是:将解大致分类,在每类中维护最优解,一起放入堆中迭代。

  应用到本题,按 \((x,y)\) 的二维偏序排序后,对于每个点,尝试维护其前方的所有点与它构成的答案信息。具体地,对于当前 \((x,y)\),维护了 \((x',y')\) 与它的曼哈顿距离,必然有 \(x'\le x\),所以仅需考虑 \(y\) 与 \(y'\) 的关系,分别取正负号,用可持久化线段树维护一发即可。

  复杂度 \(\mathcal O((n+m)\log n)\),空间同阶。

\(\mathcal{Code}\)

/* Clearink */

#include <queue>
#include <cstdio>
#include <vector>
#include <cassert>
#include <iostream>
#include <algorithm> #define rep( i, l, r ) for ( int i = l, rep##i = r; i <= rep##i; ++i )
#define per( i, r, l ) for ( int i = r, per##i = l; i >= per##i; --i ) typedef long long LL;
#define int LL
typedef std::pair<int, int> PII;
typedef std::pair<LL, int> PLI;
#define fi first
#define se second inline int rint() {
int x = 0, f = 1, s = getchar();
for ( ; s < '0' || '9' < s; s = getchar() ) f = s == '-' ? -f : f;
for ( ; '0' <= s && s <= '9'; s = getchar() ) x = x * 10 + ( s ^ '0' );
return x * f;
} template<typename Tp>
inline void wint( Tp x ) {
if ( x < 0 ) putchar( '-' ), x = -x;
if ( 9 < x ) wint( x / 10 );
putchar( x % 10 ^ '0' );
} inline int iabs( const int a ) { return a < 0 ? -a : a; } const int MAXN = 2.5e5;
const LL IINF = 1ll << 60;
int n, m, mx, dy[MAXN + 5], root[MAXN + 5];
PII pt[MAXN + 5];
std::vector<int> ybuc[MAXN + 5];
std::priority_queue<PLI, std::vector<PLI>, std::greater<PLI> > heap; struct SegmentTree {
static const int MAXND = 1e7;
int node, ch[MAXND][2];
PII amn[MAXND], smn[MAXND]; SegmentTree() { amn[0] = smn[0] = { IINF, 0 }; } inline void copy( const int u, const int v ) {
ch[u][0] = ch[v][0], ch[u][1] = ch[v][1],
amn[u] = amn[v], smn[u] = smn[v];
} inline void pushup( const int u ) {
amn[u] = std::min( amn[ch[u][0]], amn[ch[u][1]] );
smn[u] = std::min( smn[ch[u][0]], smn[ch[u][1]] );
} inline void modify( int& u, const int l, const int r,
const int x, const int y ) {
int v = u, mid = l + r >> 1; copy( u = ++node, v );
if ( l == r ) {
if ( x == IINF ) u = 0;
else amn[u] = { -x + dy[y], l }, smn[u] = { -x - dy[y], l };
return ;
}
if ( y <= mid ) modify( ch[u][0], l, mid, x, y );
else modify( ch[u][1], mid + 1, r, x, y );
pushup( u );
} inline PII query( const int u, const int l, const int r,
const int ql, const int qr, const bool type ) const {
if ( !u ) return { IINF, 0 };
if ( ql <= l && r <= qr ) return type ? amn[u] : smn[u];
int mid = l + r >> 1; PII ret( IINF, 0 );
if ( ql <= mid ) {
ret = std::min( ret, query( ch[u][0], l, mid, ql, qr, type ) );
}
if ( mid < qr ) {
ret = std::min( ret, query( ch[u][1], mid + 1, r, ql, qr, type ) );
}
return ret;
} inline PLI calc( const int rt, const int x, const int y ) {
PII up( query( rt, 1, mx, y, mx, true ) ),
dn( query( rt, 1, mx, 1, y, false ) );
LL u = 0ll + x - dy[y] + up.fi, d = 0ll + x + dy[y] + dn.fi;
if ( u <= d ) return PLI( u, up.se );
else return PLI( d, dn.se );
}
} sgt; signed main() {
n = rint(), m = rint();
rep ( i, 1, n ) pt[i].fi = rint(), dy[i] = pt[i].se = rint(); std::sort( pt + 1, pt + n + 1 );
std::sort( dy + 1, dy + n + 1 );
mx = std::unique( dy + 1, dy + n + 1 ) - dy - 1;
rep ( i, 1, n ) {
pt[i].se = std::lower_bound( dy + 1, dy + mx + 1, pt[i].se ) - dy;
ybuc[pt[i].se].push_back( pt[i].fi );
} rep ( i, 1, mx ) std::sort( ybuc[i].begin(), ybuc[i].end() ); rep ( i, 2, n ) {
root[i] = root[i - 1];
sgt.modify( root[i], 1, mx, pt[i - 1].fi, pt[i - 1].se );
heap.push( { sgt.calc( root[i], pt[i].fi, pt[i].se ).fi, i } );
} while ( m-- ) {
PLI p( heap.top() ); heap.pop();
wint( p.fi ), putchar( '\n' ); int yv( sgt.calc( root[p.se], pt[p.se].fi, pt[p.se].se ).se );
int x = 0ll + iabs( dy[pt[p.se].se] - dy[yv] ) + pt[p.se].fi - p.fi;
int id = std::lower_bound( ybuc[yv].begin(), ybuc[yv].end(), x )
- ybuc[yv].begin(), nx = id ? ybuc[yv][id - 1] : IINF; sgt.modify( root[p.se], 1, mx, nx, yv );
heap.push( { sgt.calc( root[p.se], pt[p.se].fi, pt[p.se].se ).fi,
p.se } );
}
return 0;
}

Solution -「JOISC 2021」「LOJ #3491」道路建设的更多相关文章

  1. Solution -「JOISC 2021」「LOJ #3489」饮食区

    \(\mathcal{Description}\)   Link.   呐--不想概括题意,自己去读叭~ \(\mathcal{Solution}\)   如果仅有 1. 3. 操作,能不能做?    ...

  2. Solution -「JOISC 2021」「LOJ #3495」聚会 2

    \(\mathcal{Description}\)   Link.   给定一棵含 \(n\) 个结点的树.称点集 \(S\) 到结点 \(u\) 的会合距离为 \(\sum_{v\in S}\ope ...

  3. Solution -「JOISC 2021」古老的机器

    \(\mathcal{Description}\)   Link.   这是一道通信题.   对于长度为一个 \(n\),仅包含字符 X, Y, Z 的字符串 \(s\),将其中 \(n\) 个字符按 ...

  4. Loj #2731 「JOISC 2016 Day 1」棋盘游戏

    Loj 2731 「JOISC 2016 Day 1」棋盘游戏 JOI 君有一个棋盘,棋盘上有 \(N\) 行 \(3\) 列 的格子.JOI 君有若干棋子,并想用它们来玩一个游戏.初始状态棋盘上至少 ...

  5. 【LOJ】#3036. 「JOISC 2019 Day3」指定城市

    LOJ#3036. 「JOISC 2019 Day3」指定城市 一个点的可以dp出来 两个点也可以dp出来 后面的就是在两个点的情况下选一条最长的链加进去,用线段树维护即可 #include < ...

  6. 【LOJ】#3034. 「JOISC 2019 Day2」两道料理

    LOJ#3034. 「JOISC 2019 Day2」两道料理 找出最大的\(y_{i}\)使得\(sumA_{i} + sumB_{y_i} \leq S_{i}\) 和最大的\(x_{j}\)使得 ...

  7. 【LOJ】#3032. 「JOISC 2019 Day1」馕

    LOJ#3032. 「JOISC 2019 Day1」馕 处理出每个人把馕切成N段,每一段快乐度相同,我们选择第一个排在最前的人分给他的第一段,然后再在未选取的的人中选一个第二个排在最前的切一下,并把 ...

  8. 【LOJ】#3033. 「JOISC 2019 Day2」两个天线

    LOJ#3033. 「JOISC 2019 Day2」两个天线 用后面的天线更新前面的天线,线段树上存历史版本的最大值 也就是线段树需要维护历史版本的最大值,后面的天线的标记中最大的那个和最小的那个, ...

  9. 【LOJ】#3031. 「JOISC 2019 Day1」聚会

    LOJ#3031. 「JOISC 2019 Day1」聚会 听说随机可过? 我想了很久想了一个不会被卡的做法,建出前\(u - 1\)个点的虚树,然后找第\(u\)个点的插入位置,就是每次找一条最长链 ...

随机推荐

  1. spring boot & maven 多模块 ---心得

    1.前言 有个名字叫 多模块企业级项目  ,其实就是一个父级maven工程里面有着多个子级maven工程的项目 ,甚至在子级maven 里面还有多个子级maven, 这用到了 maven多模块开发的使 ...

  2. C语言 运算符优先级和结合方向

    运算符优先级和结合方向 初级运算符( ).[ ].->..  高于  单目运算符  高于  算数运算符(先乘除后加减)  高于  关系运算符  高于  逻辑运算符(不包括!)  高于  条件运算 ...

  3. 灵雀云发布云原生制品仓库Harbor企业版(Alauda Registry Service for Harbor)

      灵雀云发布云原生制品仓库Harbor企业版(Alauda Registry Service for Harbor) 近日,国内领先的云原生全栈私有云提供商灵雀云宣布,推出企业版云原生制品仓库Ala ...

  4. 5大最新云原生镜像构建工具全解析,3个来自Google,你了解几个?

    1云原生大背景下的镜像构建在分享开始,我想先跟大家简单聊一下云原生,可能不会详细展开,而是带领大家了解一下云原生对镜像构建方面的影响.第一,在接触云原生相关的技术时,无论是要解决开发.测试环境的问题, ...

  5. zip方式安装MySQL提示找不到 MSVCP120.dll的解决方法

    电脑重装系统后,用zip的方式安装MySQL,在执行mysqld --initialize的时候提示 由于找不到 MSVCP120.dll,无法继续执行代码.重新安装程序可能会解决此问题. 解决的方法 ...

  6. Win7升级Win11升级记录及教程 【错误码(0×8004242d)】

    hellow,大家好,我是公众号棱镜Prism K的[K君].家中电脑因为一些原因不得不进行升级,下面是我对这次电脑升级所进行的记录. step 1.打开微软官网,找到对应的WIN11下载模块,这里注 ...

  7. 【刷题-LeetCode】209. Minimum Size Subarray Sum

    Minimum Size Subarray Sum Given an array of n positive integers and a positive integer s, find the m ...

  8. 【刷题-LeetCode】203. Remove Linked List Elements

    Remove Linked List Elements Remove all elements from a linked list of integers that have value *val* ...

  9. 【重构前端知识体系之HTML】讲讲对HTML5的一大特性——语义化的理解

    [重构前端知识体系之HTML]讲讲对HTML5的一大特性--语义化的理解 引言 在讲什么是语义化之前,先看看语义化的背景. 在之前的文章中提到HTML最重要的特性,那就是标签.但是项目一大,标签多的看 ...

  10. 搭服务器之centos-ipv6源--配置各虚拟机系统的ipv6网络安装源。

    在2g内存的台式机里安装了三台虚拟机,跑起来好可以,就是swap用的比较多,图见上一篇随笔.现在平台基本有了,自己笔记本算总控,实验室台式机跑着4台机器(一实三虚),加上一台服务器,可以做很多事情了, ...