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\)个点的插入位置,就是每次找一条最长链 ...
随机推荐
- nuxt中报window is not defined
1.如果是引用插件报错的话,原因是在服务端渲染时找不到window,这样在插件引入位置把ssr设置为false即可. plugins: [ { src: '@/plugins/iview', ssr: ...
- 拉普拉斯平滑(Laplacian smoothing)
概念 零概率问题:在计算事件的概率时,如果某个事件在观察样本库(训练集)中没有出现过,会导致该事件的概率结果是 $0$ .这是不合理的,不能因为一个事件没有观察到,就被认为该事件一定不可能发生(即该 ...
- LCT小记
不用说了,直接上怎么 die( 千万不要和 Treap 一样写左旋 zig 和右旋 zag,莫名死亡.Splay 只支持一个 rotate 上旋一个节点即可. splay() 之前记得弄一个栈存储 u ...
- 为什么char类型输入遇空格会结束,int类型必须要空格才能输出
char类型与int类型输入时的区别: 在C语言的规则中,规定了scanf函数在接收字符串时--遇到空格或回车就认为前面的输入已经完成且有效! 而对于int类型:表示整数,输入时需要用空格隔开,以确认 ...
- 安装DataX的管理控制台(转)
原文地址 https://github.com/WeiYe-Jing/datax-web/blob/master/doc/datax-web/datax-web-deploy.md 环境准备 1)基础 ...
- manjora20不小心卸载,重新安装terminal,软件商店/软件中心linux类似
问题 重新安装老版本gnome-shell 如果突然死机可能卸载完了terminal和软件商店,但是没有安装新的. 此时没有terminal也没有软件商店 无法安装软件 解决方案 terminal c ...
- vue 快速入门 系列 —— 实例方法(或 property)和静态方法
其他章节请看: vue 快速入门 系列 实例方法(或 property)和静态方法 在 Vue(自身) 项目结构 一文中,我们研究了 vue 项目自身构建过程,也知晓了 import Vue from ...
- elasticsearch启动错误解决办法
1.max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144] 解决: [r ...
- Linux下查找软件,rpm命令 dpkg命令 apt命令
centos: 1.查询一个包是否被安装 rpm -q < package name> 2.列出已安装软件相关的所有包 rpm -qa < package name> ubun ...
- 计算机网络-5-9-TCP拥塞控制
TCP拥塞控制 拥塞控制的一般原理 在计算机网络中的链路容量(带宽),交换节点中的缓存和处理机等,都是网络的资源,在某段时间,若对网络中某一资源的需求超过该资源所能提供的可用部分,网络性能就会变坏,这 ...