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 ...
随机推荐
- hbase构建二级索引解决方案
关注公众号:大数据技术派,回复"资料",领取1024G资料. 1 为什么需要二级索引 HBase的一级索引就是rowkey,我们仅仅能通过rowkey进行检索.假设我们相对Hbas ...
- Java EE数据持久化框架 • 【第4章 MyBatis动态SQL】
全部章节 >>>> 本章目录 4.1 MyBatis动态标签 4.1.1 MyBatis动态标签介绍 4.1.2 < if >标签 4.1.3 update语 ...
- Java面向对象笔记 • 【第7章 集合】
全部章节 >>>> 本章目录 7.1 集合概述 7.1.1 Java集合体系概述 7.1.2 实践练习 7.2 List集合 7.2.1 ArrayList实现类 7.2. ...
- 新环境chart包helmlint校验
在iot目录内可以执行helm lint iot-api 去校验
- Linux查看RAM内存信息
1.查看/proc/meminfo文件 查看RAM使用情况最简单的方法是通过/proc/meminfo. 这个动态更新的虚拟文件列出了详细的内存使用情况. cat /proc/meminfo 命令输出 ...
- CSS基础 常见的元素显示模式
1.块级元素 属性:display:block 特点:1.一行只能显示一个元素 2.宽度默认是父元素的,高度是有内容撑开 3.可以设置宽.高常见块元素:div,p,h系列,ul.li,dl.dt.dd ...
- Pytest_Hook函数pytest_addoption(parser):定义自己的命令行参数(14-1)
考虑场景: 我们的自动化用例需要支持在不同测试环境运行,有时候在dev环境运行,有时候在test环境运行: 有时候需要根据某个参数不同的参数值,执行不同的业务逻辑: 上面的场景我们都可以通过" ...
- 初识python 之 爬虫:正则表达式
python中正则表达式功能由 re 模块提供: import re 两个主要函数: match 匹配第一个字符(从第一个字符开始匹配) search 匹配整个字符串 一.匹配单个字符 1.匹配某个 ...
- 在quasar 注册全局filter
A common use case for Quasar applications is to run code before the root Vue app instance is instant ...
- VUE3 之 全局组件与局部组件
1. 概述 老话说的好:忍耐是一种策略,同时也是一种性格磨炼. 言归正传,今天我们来聊聊 VUE 的全局组件与局部组件. 2. 全局组件 2.1 不使用组件的写法 <body> < ...