Solution -「JOISC 2021」「LOJ #3491」道路建设
\(\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」道路建设的更多相关文章
- Solution -「JOISC 2021」「LOJ #3489」饮食区
\(\mathcal{Description}\) Link. 呐--不想概括题意,自己去读叭~ \(\mathcal{Solution}\) 如果仅有 1. 3. 操作,能不能做? ...
- Solution -「JOISC 2021」「LOJ #3495」聚会 2
\(\mathcal{Description}\) Link. 给定一棵含 \(n\) 个结点的树.称点集 \(S\) 到结点 \(u\) 的会合距离为 \(\sum_{v\in S}\ope ...
- Solution -「JOISC 2021」古老的机器
\(\mathcal{Description}\) Link. 这是一道通信题. 对于长度为一个 \(n\),仅包含字符 X, Y, Z 的字符串 \(s\),将其中 \(n\) 个字符按 ...
- Loj #2731 「JOISC 2016 Day 1」棋盘游戏
Loj 2731 「JOISC 2016 Day 1」棋盘游戏 JOI 君有一个棋盘,棋盘上有 \(N\) 行 \(3\) 列 的格子.JOI 君有若干棋子,并想用它们来玩一个游戏.初始状态棋盘上至少 ...
- 【LOJ】#3036. 「JOISC 2019 Day3」指定城市
LOJ#3036. 「JOISC 2019 Day3」指定城市 一个点的可以dp出来 两个点也可以dp出来 后面的就是在两个点的情况下选一条最长的链加进去,用线段树维护即可 #include < ...
- 【LOJ】#3034. 「JOISC 2019 Day2」两道料理
LOJ#3034. 「JOISC 2019 Day2」两道料理 找出最大的\(y_{i}\)使得\(sumA_{i} + sumB_{y_i} \leq S_{i}\) 和最大的\(x_{j}\)使得 ...
- 【LOJ】#3032. 「JOISC 2019 Day1」馕
LOJ#3032. 「JOISC 2019 Day1」馕 处理出每个人把馕切成N段,每一段快乐度相同,我们选择第一个排在最前的人分给他的第一段,然后再在未选取的的人中选一个第二个排在最前的切一下,并把 ...
- 【LOJ】#3033. 「JOISC 2019 Day2」两个天线
LOJ#3033. 「JOISC 2019 Day2」两个天线 用后面的天线更新前面的天线,线段树上存历史版本的最大值 也就是线段树需要维护历史版本的最大值,后面的天线的标记中最大的那个和最小的那个, ...
- 【LOJ】#3031. 「JOISC 2019 Day1」聚会
LOJ#3031. 「JOISC 2019 Day1」聚会 听说随机可过? 我想了很久想了一个不会被卡的做法,建出前\(u - 1\)个点的虚树,然后找第\(u\)个点的插入位置,就是每次找一条最长链 ...
随机推荐
- ajax 异步 提交 含文件的表单
1.前言 需求是使用 jquery 的 ajax 异步提交表单,当然,不是简单的数据,而是包含文件数据的表单.于是我想到了 new FormData() 的用法, 可是仍然提交失败,原来是ajax的属 ...
- Windows 10 安装 Git 与初次运行前的配置
Windows 10 安装 Git 与初次运行前的配置 在 Windows 上安装 初次运行 Git 前的配置 用户信息 文本编辑器 检查配置信息 获取 Git 仓库 在现有目录中初始化仓库 克隆现有 ...
- Linux环境下的Docker的安装和部署、学习-一
CentOS Docker 安装Docker支持以下的CentOS版本:CentOS 7 (64-bit)CentOS 6.5 (64-bit) 或更高的版本 前提条件目前,CentOS 仅发行版本中 ...
- Git在实际生产中的使用
文章目录 Git在实际生产中的使用 简单情况下的代码提交 Fetch and Pull 仅获取某分支的代码 远程仓库已经合并了别人的代码 冲突产生原因与解决办法 不恰当的多个Commit合并为一个 G ...
- Parallel.For实现
static class MyParallel { //4.0及以上用Task, Task的背后的实现也是使用了线程池线程 //static List<Task> tasks = new ...
- winform控件拖动
示例代码 using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Form ...
- 使用idea时jsp中使用out.print();时报错的解决办法
在用Maven创建web项目时 在jsp页面中out.print();老是报错 这边print显示红色出错因为这边使用的是JSP的API并不是Servlet的,但是可以运行,所以我们只要导包就完事其实 ...
- 【记录一个问题】android下opencl中的event.getProfilingInfo()测速时间并不准确
使用了类似的代码来做android下opencl的时间测试: cl::CommandQueue queue(context, devices[0], CL_QUEUE_PROFILING_ENABLE ...
- django_url配置
前言 我们在浏览器访问一个网页是通过url地址去访问的,django管理url配置是在urls.py文件.当一个页面数据很多时候,通过会有翻页的情况,那么页数是不固定的,如:page=1.也就是url ...
- html图像 表格 列表
创建图像映射 <img src="/demo/planets.gif" width="145" height="126" alt=&q ...