因为每行只有一个区域不能往下走,因此我们可以来分析一下从起点到整个矩形每个位置的最短路。可以发现每一行的最短路只与上一行的最短路有关,假设我们知道上一行的最短路,上一行不能往下走的区间在 \([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. 云南农职《JavaScript交互式网页设计》 综合机试试卷⑥——简易旅游网

    本页面分为顶部导航.登录注册栏,中部图片展示.主体内容和底部反馈模板 一.导航栏部分 要求一:设置菜单栏(二级菜单)和登录注册模块 要求二:当鼠标悬停到菜单栏(一级菜单)时,二级菜单以滑动效果滑出显示 ...

  2. SpringBoot读取外部配置文件的方法

    SpringBoot读取外部配置文件的方法 Spring高级之注解@PropertySource详解(超详细) 1.@PropertySource(value = {"classpath:c ...

  3. 前端后端通信初步尝试(javascript - flask)

    在某项目中,需要使用python flask做后端功能开发,web提供功能入口. 此时需要使用Ajax通信. 由于以前从未接触过网络传输,记录了一些基础知识. 资料参考<HTML5+CSS3+J ...

  4. 了解C#的协变和逆变

    前言 在引用类型系统时,协变.逆变和不变性具有如下定义. 这些示例假定一个名为 Base 的基类和一个名为 Derived的派生类. Covariance 使你能够使用比原始指定的类型派生程度更大的类 ...

  5. django中使用支付宝

    一.注册 https://auth.alipay.com/login/ant_sso_index.htm?goto=https%3A%2F%2Fopenhome.alipay.com%2Fplatfo ...

  6. mysql 的 if 和 SQL server 的 iif

    在sql语句中,mysql 使用 if 而SQL server 使用iif 如 mysql : SELECT IF(1<2,'yes ','no'); sql server: SELECT II ...

  7. 在 CentOS 7 上安装 GitLab

    1. 安装和配置必要的依赖库 sudo yum install -y curl policycoreutils-python openssh-server # the commands below w ...

  8. spring cloud 与spring boot的版本对应总结

    1.前言 一开始不理解为什么使用 spring boot 高版本  ,却没有对应的spring cloud版本 ,还以为最高版本的 spring cloud 会向上兼容  . 这个坑 ,没有人告诉我, ...

  9. SpringBoot学习笔记三之表述层

    注:图片如果损坏,点击文章链接:https://www.toutiao.com/i6803355920697917965/ 首先配置learn-admin-webui中的web.xml文件 配置Con ...

  10. 动静分离、Rewirte、HTTPS

    目录 Nginx动静分离技术 示例搭建步骤 部署NFS 静态资源共享 部署代理服务器 Rewrite(重点) Rewrite基本概述 rewrite语法 rewrite标记Flag last和brea ...