因为每行只有一个区域不能往下走,因此我们可以来分析一下从起点到整个矩形每个位置的最短路。可以发现每一行的最短路只与上一行的最短路有关,假设我们知道上一行的最短路,上一行不能往下走的区间在 \([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. Mind the Box: $\ell_1$-APGD for Sparse Adversarial Attacks on Image Classifiers

    目录 概 主要内容 Croce F. and Hein M. Mind the box: \(\ell_1\)-APGD for sparse adversarial attacks on image ...

  2. MySQL数据库基础(1)数据库基础

    目录 一.数据库简介 二.mysql数据库 三.客户端连接mysql服务 四.Navicat for mysql 一.数据库简介 1.概念 (1)数据:如文字.图形.图像.声音以及学生的档案记录等,这 ...

  3. Jedis 基本使用

    引入 jedis 依赖: <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <!-- Dec 15, 2 ...

  4. python 中的省略号

    在查看django源码时遇到下列内容:sweat: 这个省略号是什么意思? 来自为知笔记(Wiz)

  5. docker的无用镜像

    dangling images build 自己的 docker 镜像的时候,有时会遇到用一个甚至多个中间层镜像,这会一定程度上减少最终打包出来 docker 镜像的大小,但是会产生一些tag 为 n ...

  6. Powershell 【控制台常用方法】

    1 function Pause(){ 2 [System.Console]::Write('按任意键继续...') 3 [void][System.Console]::ReadKey(1) 4 } ...

  7. let var const 区别

    let es6 语法 let是作用域是块级的,即{}内的范围 如果未声明变量就使用的话,报错ReferenceError,而var则会报错undefined(不存在变量提升) 只要块级作用域内存在le ...

  8. Chromium Windows Build

    https://chromium.googlesource.com/chromium/src/+/refs/heads/main/docs/windows_build_instructions.md ...

  9. Zookeeper绍二(分布式锁介)

    一.为什么会有分布式锁? 在多线程环境下,由于上下文的切换,数据可能出现不一致的情况或者数据被污染,我们需要保证数据安全,所以想到了加锁. 所谓的加锁机制呢,就是当一个线程访问该类的某个数据时,进行保 ...

  10. 微软的Serialize和Newtonsoft的SerializeObject比较

    微软的序列化反序列化组件出来已有好几年了,刚出来的时候各种吐槽.最近在优化代码,比较了一下微软的Serialize和Newtonsoft的SerializeObject,感觉大部分场景下可以用微软的序 ...