AT [ABC177F] I hate Shortest Path Problem
因为每行只有一个区域不能往下走,因此我们可以来分析一下从起点到整个矩形每个位置的最短路。可以发现每一行的最短路只与上一行的最短路有关,假设我们知道上一行的最短路,上一行不能往下走的区间在 \([L, R]\),那么可以发现的是 \([1, L - 1], [R + 1, m]\) 这些区间会直接从上一行走下来,因为假设存在一个更靠前的位置它走过来更优,那么前一行的最短路就会由这个位置走过来更新。同理,因为区间 \([L, R]\) 是不能从上面直接走下来的,所以其一定是从 \([1, L - 1]\) 的某个位置开始走然后走完整段区间,根据前面的理论,这个开始的点应该是 \(L - 1\) 这个位置。
于是我们每行最短路的变化就很清楚了,相当于将 \([L, R]\) 填上一段长度以 \(dis_{L - 1} + 1\) 开头的公差为 \(1\) 的等差数列,然后再整体加 \(1\)(因为所有点都需要向下走)。这个整体加 \(1\) 的操作可以直接处理,于是我们需要寻找到一种方法能将区间填上一段等差数列。
可以发现线段树是一个很好的选择,对于每个区间,我们维护这个区间的最小值,以及这个区间如果为(否则为 \(0\))等差数列时的开头数字(也是懒标记)。那么我们每次修改之前只需要提前下方完所有懒标记,然后再修改就没有错了。因为我们在提前下方懒标记时会保证当前添加的懒标记在后续下方时会最后下放,也就是作为最终覆盖。
一些坑点
- 修改时递归右区间等差数列的开头元素一定要想清楚是什么。
#include<bits/stdc++.h>
using namespace std;
#define ls (p << 1)
#define rs (p << 1 | 1)
#define mid (l + r) / 2
#define rep(i, l, r) for(int i = l; i <= r; ++i)
typedef long long ll;
const int N = 1200000 + 5;
struct tree{
int min, tag;
}t[N];
int n, m, a, b, fir;
int read(){
char c; int x = 0, f = 1;
c = getchar();
while(c > '9' || c < '0'){ if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
void lazy(int p, int k){
t[p].min = t[p].tag = k;
}
void down(int p, int l, int r){
if(!t[p].tag) return;
lazy(ls, t[p].tag), lazy(rs, t[p].tag + mid - l + 1);
t[p].tag = 0;
}
void update(int p, int l, int r, int x, int y, int k){
down(p, l, r);
if(l >= x && r <= y){ lazy(p, k); return;}
if(mid >= x) update(ls, l, mid, x, y, k);
if(mid < y) update(rs, mid + 1, r, x, y, k + max(0, mid - max(l, x) + 1));
t[p].min = min(t[ls].min, t[rs].min);
}
int query(int p, int l, int r, int x, int y){
if(l >= x && r <= y) return t[p].min;
down(p, l, r);
if(mid >= x) return query(ls, l, mid, x, y);
else return query(rs, mid + 1, r, x, y);
}
int main(){
n = read(), m = read();
rep(i, 1, n){
a = read(), b = read();
fir = (a == 1 ? m + 1 : query(1, 1, m, a - 1, a - 1) + 1);
update(1, 1, m, a, b, fir);
printf("%d\n", t[1].min >= m + 1 ? -1 : t[1].min + i);
}
return 0;
}
AT [ABC177F] I hate Shortest Path Problem的更多相关文章
- Codefroces Educational Round 27 845G Shortest Path Problem?
Shortest Path Problem? You are given an undirected graph with weighted edges. The length of some pat ...
- 干货 | 列生成VRPTW子问题ESPPRC( Elementary shortest path problem with resource constraints)介绍附C++代码
00 前言 各位小伙伴大家好,相信大家已经看过前面column generation求解vehicle routing problems的过程详解.该问题中,子问题主要是找到一条reduced cos ...
- 【CF edu 27 G. Shortest Path Problem?】
time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standa ...
- Codeforces 845G Shortest Path Problem?
http://codeforces.com/problemset/problem/845/G 从顶点1dfs全图,遇到环则增加一种备选方案,环上的环不需要走到前一个环上作为条件,因为走完第二个环可以从 ...
- 线性基【CF845G】Shortest Path Problem?
Description 给定一张 \(n\) 个点 \(m\) 条边的无向图,一开始你在点 \(1\),且价值为 \(0\) 每次你可以选择一个相邻的点,然后走过去,并将价值异或上该边权 如果在点 \ ...
- [CF845G]Shortest Path Problem?
题目大意:同这道题,只是把最大值变成了最小值 题解:略 卡点:无 C++ Code: #include <cstdio> #define maxn 100010 #define maxm ...
- Solve Longest Path Problem in linear time
We know that the longest path problem for general case belongs to the NP-hard category, so there is ...
- Why longest path problem doesn't have optimal substructure?
We all know that the shortest path problem has optimal substructure. The reasoning is like below: Su ...
- hdu-----(2807)The Shortest Path(矩阵+Floyd)
The Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
随机推荐
- toString()、String.valueOf、(String)强转
1.基本类型 (1)基本类型没有toString()方法 (2)推荐使用String.valueOf(); (3)无法强转 =========补========= (String)是标准的类型转换,将 ...
- Sublime Text 3菜单栏隐藏后怎么显示
Sublime Text 3如何显示菜单栏 今天在使用Sublime Text 3敲代码的时候不小心就把上侧的菜单栏隐藏了,自己鼓捣了半天才把菜单栏弄出来,下面我就给大家讲解一下我是如何操作的 键盘按 ...
- $\infty$-former: Infinite Memory Transformer
目录 概 主要内容 如何扩展? 实验细节 Martins P., Marinho Z. and Martins A. \(\infty\)-former: Infinite Memory Transf ...
- 「物流跟踪管理系统」 · Java Swing + MySQL JDBC开发,美和易思结业考试机试试题
目录 文档说明: 一.语言和环境 二.技术要求 三.功能要求 四.数据库设计 五.具体要求及推荐实现步骤 六.注意事项 实现代码: 一.数据库 二.Java Swing com.ynavc.Bean ...
- Spring进一步学习
目录 1.beans.xml编写 2.别名 (1)alias (2)name 3.依赖注入(DI) (1)数组注入 (2)List注入 (3)Map注入 (4)set注入 (5)Null注入 (6)p ...
- 深入 Laravel 内核之外观模式(门面模式)
门面模式核心内容: 客户端与子系统的通信通过外观对象进行: 外观对象封装一系列子系统的具体对应方法,对客户端只需暴露一个单一的入口方法: 客户端通过访问外观对象即可调用子系统的基础方法,无需关心子系统 ...
- CSS基础 CSS的三大特性以及选择器优先级计算方法
1.子元素默认会继承父元素的样式,但不是所有的元素都有继承 常见的继承父元素特点的元素有: 1.color 2.font-sytle.font-weight.font-size.font-family ...
- docker安装minio
目录 一.简介 二.docker安装 三.java中使用minio上传与下载 一.简介 MinIO 是在 GNU Affero 通用公共许可证 v3.0 下发布的高性能对象存储. 它是与 Amazon ...
- 我踩过的python的坑
1. string中Template用法 变量名不能是${tradeDate+1}, python无法识别其为变量,应改为 ${tradeDate1} 变量替换的语句:data_new = Templ ...
- Docker 安装mysql主从
安装docker 1.yum -y install docker 2.查看是否安装成功 docker -v 3.接着将docker后台启动 systemctl start docker.service ...