CF1175E Minimal Segment Cover
题意
给出n条线段。m次询问,每次询问给出一个区间\([l,r]\)问最少需要多少条线段才能覆盖区间\([l,r]\)。
所有坐标\(\le 5\times 10^5\)。\(n,m\le 2\times 10^ 5\)
思路
其实是比较经典的线段覆盖问题。
\(f[i][j]\)表示从i开始走\(2^j\)条线段最远到达的位置。
然后对于每次询问都走一遍即可。
代码
/*
* @Author: wxyww
* @Date: 2019-06-06 10:55:48
* @Last Modified time: 2019-06-06 14:54:02
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<ctime>
using namespace std;
typedef long long ll;
const int N = 1000000 + 100,logN = 23;
ll read() {
ll x=0,f=1;char c=getchar();
while(c<'0'||c>'9') {
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9') {
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
int f[N][logN + 1];
int query(int l,int r) {
ll ans = 0;
for(int i = logN - 1;i >= 0;--i) {
if(f[l][i] < r) {
l = f[l][i];
ans += (1 << i);
}
}
l = f[l][0];ans++;
if(l < r) return -1;
return ans;
}
int main() {
int n = read(),m = read();
int mx = 0;
for(int i = 1;i <= n;++i) {
int l = read() + 1,r = read() + 1;
f[l][0] = max(f[l][0],r);
mx = max(mx,r);
}
for(int i = 1;i <= mx;++i) f[i][0] = max(f[i][0],max(i,f[i - 1][0]));
for(int j = 1;j < logN;++j)
for(int i = 1;i <= mx;++i)
f[i][j] = f[f[i][j - 1]][j - 1];
while(m--) {
int l = read() + 1,r = read() + 1;
printf("%d\n",query(l,r));
}
return 0;
}
CF1175E Minimal Segment Cover的更多相关文章
- CF1175E Minimal Segment Cover 题解
题意:给出\(n\)个形如\([l,r]\)的线段.\(m\)次询问,每次询问区间\([x,y]\),问至少选出几条线段,使得区间\([x,y]\)的任何一个部位都被至少一条线段覆盖. 首先有一个显然 ...
- Codeforces 1175E Minimal Segment Cover
题意: 有\(n\)条线段,区间为\([l_i, r_i]\),每次询问\([x_i, y_i]\),问要被覆盖最少要用多少条线段. 思路: \(f[i][j]\)表示以\(i\)为左端点,用了\(2 ...
- CodeForces - 1175E Minimal Segment Cover (倍增优化dp)
题意:给你n条线段[l,r]以及m组询问,每组询问给出一组[l,r],问至少需要取多少个线段可以覆盖[l,r]区间中所有的点. 如果贪心地做的话,可以求出“从每个左端点l出发选一条线段可以到达的最右端 ...
- codeforces1175E Minimal Segment Cover 倍增
题目传送门 题意:给出n条平行于x轴的线段,q次询问,每次询问一个区间最少要几条线段来覆盖,若不能覆盖则输出-1. 思路:先考虑贪心,必定是先找到,所有左端点小于等于$x$的线段的右端点最大在哪里,然 ...
- Codeforces Edu Round 66 A-E
A. From Hero to Zero 通过取余快速运行第一步即可.由于\(a \% b (a >= b) <= \frac{a}{2}\).所以总复杂度不超过\(O(log_2n)\) ...
- uva.10020 Minimal coverage(贪心)
10020 Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose t ...
- 【区间覆盖问题】uva 10020 - Minimal coverage
可以说是区间覆盖问题的例题... Note: 区间包含+排序扫描: 要求覆盖区间[s, t]; 1.把各区间按照Left从小到大排序,如果区间1的起点大于s,则无解(因为其他区间的左起点更大):否则选 ...
- UVa 10020 - Minimal coverage(区间覆盖并贪心)
Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose the min ...
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li, ...
随机推荐
- html点击弹出QQ添加好友的窗口
<a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=1377732948&q ...
- Practical Go: Real world advice for writing maintainable Go programs
转自:https://dave.cheney.net/practical-go/presentations/qcon-china.html?from=timeline 1. Guiding pri ...
- go中interface空指针不为nil判断方法
interface空指针不为nil 当把一个空指针对象赋值给一个interface后,再判断!= nil就不再成立了 代码如下 package main import "fmt" ...
- dedecms用runphp功能,写for循环,@me输出不出来
今天在{dede:field name='typeid' runphp='yes'}中写for循环,出现@me输出不了内容,把for循环删掉之后,就可以输出.死了几十万脑细胞,没有解决,后来把循环 f ...
- OpenGL入门1.3:着色器 GLSL
前言 经过之前一段时间的学习(渲染管线简介)我们已经知道了着色器(Shader)是运行在GPU上的程序,这些小程序为图形渲染管线的某个特定部分而运行,着色器只是一种把输入转化为输出的程序,着色器也是一 ...
- dubbo入门学习
官方网址:http://dubbo.apache.org/zh-cn/index.html 学习可以参考官网中文文档:http://dubbo.apache.org/zh-cn/docs/user/q ...
- 2019-6-15-WPF-触摸到事件
原文:2019-6-15-WPF-触摸到事件 title author date CreateTime categories WPF 触摸到事件 lindexi 2019-06-15 08:58:54 ...
- C#循环结构
一.背景: 因编程的基础差,因此最近开始巩固学习C#基础,后期把自己学习的东西,总结相应文章中,有不足处请大家多多指教. 二.简介 有的时候,可能需要多次执行同一块代码.一般情况下,语句是顺序执行的: ...
- [笔记] vs code 设置终端
设置文件: setting.json 1 设置自定义终端 cmd "terminal.integrated.shell.windows": "C:\\WINDOWS\\S ...
- 微信小程序 自定义头部导航栏和导航栏背景图片 navigationStyle
这两天因为要做一个带背景的小程序头,哭了,小程序导航栏有背景也就算了,还得让导航栏上的背景顺下来,心态小崩.现在可以单独设置一个页面的小程序头了,但是前提是要微信7.0以上的版本,考虑到兼容性问题 ...