因为每行只有一个区域不能往下走,因此我们可以来分析一下从起点到整个矩形每个位置的最短路。可以发现每一行的最短路只与上一行的最短路有关,假设我们知道上一行的最短路,上一行不能往下走的区间在 \([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的更多相关文章

  1. 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 ...

  2. 干货 | 列生成VRPTW子问题ESPPRC( Elementary shortest path problem with resource constraints)介绍附C++代码

    00 前言 各位小伙伴大家好,相信大家已经看过前面column generation求解vehicle routing problems的过程详解.该问题中,子问题主要是找到一条reduced cos ...

  3. 【CF edu 27 G. Shortest Path Problem?】

    time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standa ...

  4. Codeforces 845G Shortest Path Problem?

    http://codeforces.com/problemset/problem/845/G 从顶点1dfs全图,遇到环则增加一种备选方案,环上的环不需要走到前一个环上作为条件,因为走完第二个环可以从 ...

  5. 线性基【CF845G】Shortest Path Problem?

    Description 给定一张 \(n\) 个点 \(m\) 条边的无向图,一开始你在点 \(1\),且价值为 \(0\) 每次你可以选择一个相邻的点,然后走过去,并将价值异或上该边权 如果在点 \ ...

  6. [CF845G]Shortest Path Problem?

    题目大意:同这道题,只是把最大值变成了最小值 题解:略 卡点:无 C++ Code: #include <cstdio> #define maxn 100010 #define maxm ...

  7. 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 ...

  8. 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 ...

  9. hdu-----(2807)The Shortest Path(矩阵+Floyd)

    The Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. C++多线程并发---异步编程

    线程同步主要是为了解决对共享数据的竞争访问问题,所以线程同步主要是对共享数据的访问同步化(按照既定的先后次序,一个访问需要阻塞等待前一个访问完成后才能开始).这篇文章谈到的异步编程主要是针对任务或线程 ...

  2. Docker如何制作镜像-Dockerfile的使用

    1:什么是Dockerfile Dockerfile是一个文本文档,可以通过docker build 命令构建成一个镜像. 我们可以在Dockerfile中定义一系列的命令,构建出我们想要的镜像. 想 ...

  3. html简单随机抽奖页面(在线抽奖、随机选取、自动挑选)

    下载: https://download.csdn.net/download/weixin_44893902/20366745 效果: 代码: <!doctype html> <ht ...

  4. Jsonschema2pojo从JSON生成Java类(命令行)

    1.说明 jsonschema2pojo工具可以从JSON Schema(或示例JSON文件)生成Java类型, 在文章Jsonschema2pojo从JSON生成Java类(Maven) 已经介绍过 ...

  5. 【JPA】使用JPA实现分页和模糊查询

    1.首先创建DAO层接口,实现JpaRepository和JpaSpecificationExecutor两个接口 JpaRepository<Admin, Integer> 泛型参数分别 ...

  6. SpringBoot集成MyBatis-Plus自定义SQL

    1.说明 本文介绍Spring Boot集成MyBatis-Plus框架后, 基于已经创建好的Spring Boot工程, 添加自定义的SQL实现复杂查询等操作. 自定义SQL主要有两种方式, 一种是 ...

  7. Python项目生成requirements.txt文件及pip升级问题解决及流程

    缘由:新项目使用Python, PC上的python包不全,需要通过requirements.txt文件指定安装所需包 pip安装遇到一些坑 一.直接使用pip包管理工具生成requirements. ...

  8. Shell 中的 expect 命令

    目录 expect 介绍 expect 安装 expect 语法 自动拷贝文件到远程主机 示例一 示例二 示例三 示例四 expect 介绍 借助 expect 处理交互的命令,可以将交互过程如 ss ...

  9. Centos6.9虚拟机环境搭建

    原文链接:https://www.toutiao.com/i6481534700216123918/ 一.准备工具 VMware Workstation CentOS-6.9-x86_64-minim ...

  10. RHCSA 第六天

    一.  创建下列用户.组和组成员资格: 1.创建名为 sysmgrs 的组 2.创建用户 natasha 同时指定sysmgrs作为natasha的附加组 3.创建用户 harry 同时指定 sysm ...