Codeforces 1175E Minimal Segment Cover
题意:
有\(n\)条线段,区间为\([l_i, r_i]\),每次询问\([x_i, y_i]\),问要被覆盖最少要用多少条线段。
思路:
\(f[i][j]\)表示以\(i\)为左端点,用了\(2^j\)条线段,最远到哪里。
然后从大到小贪心即可,类似于倍增找LCA的过程。
代码:
#include <bits/stdc++.h>
using namespace std;
#define N 200010
#define M 500010
#define D 20
int n, q;
int l[N], r[N];
int f[M][D];
int work(int x, int y) {
int ans = 0;
for (int i = D - 1; i >= 0; --i) {
if (f[x][i] < y) {
x = f[x][i];
ans |= (1 << i);
}
}
x = f[x][0]; ++ans;
if (x < y) ans = -1;
return ans;
}
int main() {
while (scanf("%d%d", &n, &q) != EOF) {
for (int i = 1; i <= n; ++i) {
scanf("%d%d", l + i, r + i);
++l[i], ++r[i];
}
memset(f, 0, sizeof f);
for (int i = 1; i <= n; ++i) {
f[l[i]][0] = max(f[l[i]][0], r[i]);
}
for (int i = 1; i < M; ++i) {
f[i][0] = max(f[i][0], max(i, f[i - 1][0]));
for (int j = 1; j < D; ++j) {
f[i][j] = max(f[i][j], max(f[i][j - 1], f[i - 1][j]));
}
}
for (int j = 1; j < D; ++j) {
for (int i = 1; i < M; ++i) {
f[i][j] = max(f[i][j], f[f[i][j - 1]][j - 1]);
}
}
int x, y;
while (q--) {
scanf("%d%d", &x, &y);
++x, ++y;
printf("%d\n", work(x, y));
}
}
return 0;
}
Codeforces 1175E Minimal Segment Cover的更多相关文章
- CodeForces - 1175E Minimal Segment Cover (倍增优化dp)
题意:给你n条线段[l,r]以及m组询问,每组询问给出一组[l,r],问至少需要取多少个线段可以覆盖[l,r]区间中所有的点. 如果贪心地做的话,可以求出“从每个左端点l出发选一条线段可以到达的最右端 ...
- CF1175E Minimal Segment Cover 题解
题意:给出\(n\)个形如\([l,r]\)的线段.\(m\)次询问,每次询问区间\([x,y]\),问至少选出几条线段,使得区间\([x,y]\)的任何一个部位都被至少一条线段覆盖. 首先有一个显然 ...
- CF1175E Minimal Segment Cover
题目链接 题意 给出n条线段.m次询问,每次询问给出一个区间\([l,r]\)问最少需要多少条线段才能覆盖区间\([l,r]\). 所有坐标\(\le 5\times 10^5\).\(n,m\le ...
- codeforces1175E Minimal Segment Cover 倍增
题目传送门 题意:给出n条平行于x轴的线段,q次询问,每次询问一个区间最少要几条线段来覆盖,若不能覆盖则输出-1. 思路:先考虑贪心,必定是先找到,所有左端点小于等于$x$的线段的右端点最大在哪里,然 ...
- Codeforces 797C - Minimal string
C. Minimal string 题目链接:http://codeforces.com/problemset/problem/797/C time limit per test 1 second m ...
- CodeForces 797C Minimal string:贪心+模拟
题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...
- Codeforces 1092E Minimal Diameter Forest
Minimal Diameter Forest 首先我们找出每个连通块中的特殊点, 特殊点的定义是到各种个连通块中距离的最大值最小的点, 每个连通块肯定通过特殊点连到其他连通块, 我们把有最大值的特殊 ...
- Codeforces 825E Minimal Labels - 拓扑排序 - 贪心
You are given a directed acyclic graph with n vertices and m edges. There are no self-loops or multi ...
- Codeforces 1073 E - Segment Sum
E - Segment Sum 思路: 数位dp 我们平时做的数位dp都是求满足条件的数的个数, 这里要求满足条件的数的和 只要在原来的基础上求每一位的贡献就可以了,所以传参数时要传两个 代码: #p ...
随机推荐
- SAS学习笔记10 宏变量
一个宏变量存放的值保持不变直到被修改(全局变量) 引用时,变量名前加上"&" 宏变量在引用时放在双引号之间会被解读(单引号不会被解读) 用户定义的宏变量,有三种方式: %l ...
- 一个无法解析的外部命令and无法解析的外部符号
出现错误: 解决: 因为修改了函数定义的参数,但是没有修改函数声明的参数 把参数统一一下就行了
- 【解决方法】You seem to have the current working directory in your LD_LIBRARY_PATH environment variable.
参考地址:https://blog.csdn.net/qq_24755999/article/details/78722788 You seem to have the current working ...
- c#入门学习笔记
Hello World //打印语句 Console.WriteLine("Hello World"); //暂停 Console.ReadKey(); 数据类型 1.值类型 by ...
- css3 transform实现水平和垂直居中
代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- JS 详解 Cookie、 LocalStorage 与 SessionStorage-转载
记录一下这些知识,有时候用到会忘记,对原文作者表达感谢. 附上原文链接:JS 详解 Cookie. LocalStorage 与 SessionStorage 基本概念 Cookie Cookie 是 ...
- pythoth 中常用的魔法方法
Python魔法方法(magic method),顾名思义,魔法总是带有一些神奇色彩,就跟魔术似的.它也是有自己的规律,在这里或者说规则更合适一些. 魔法方法有一个非常鲜明的特征,就是总是被双下划线所 ...
- Vs2017 FrameWork EF Mysql 控制台应用
1 运行环境 vs2017 Net FromWork 4.6.2 手动版 没有 ado.net 实体数据模型 2 NuGet MySql.Data.Entity 6.10.9, MySq ...
- PAT Basic 1080 MOOC期终成绩 (25 分)
对于在中国大学MOOC(http://www.icourse163.org/ )学习“数据结构”课程的学生,想要获得一张合格证书,必须首先获得不少于200分的在线编程作业分,然后总评获得不少于60分( ...
- maven jar 包不在项目中
maven update project maven build