Interval query
题意:
给出数轴上的N个区间,M个询问"QUERY(a, b)", 意为[a, b]之间不相交的集合的最大数量是多少。
解法:
考虑 $O(n)$ 的贪心做法,预处理出对于每一个位置$i$,满足$i \leq L_j$ 的 $R_j$的最小值
这样暴力向后找即可。
用倍增优化这个过程 $O(nlogn)$
#include <bits/stdc++.h> #define N 100010 using namespace std; struct node
{
int l,r;
}a[N],q[N]; int n,m,tot0;
int a0[N<<];
int minR[N<<][]; bool cmp(node a,node b)
{
return a.l<b.l;
} int ask(int l,int r)
{
int x = l,ans = ;
for(int i=;~i;i--)
if(minR[x][i] <= r)
x = minR[x][i], ans += (<<i);
return ans;
} int main()
{
while(~scanf("%d%d",&n,&m))
{
a0[] = ;
for(int i=;i<=n;i++)
{
scanf("%d%d",&a[i].l,&a[i].r);
a0[++a0[]] = a[i].l;
a0[++a0[]] = a[i].r;
}
for(int i=;i<=m;i++)
{
scanf("%d%d",&q[i].l,&q[i].r);
a0[++a0[]] = q[i].l;
a0[++a0[]] = q[i].r;
}
sort(a0+,a0+a0[]+);
tot0=;
for(int i=;i<=a0[];i++) if(a0[i]!=a0[i-]) a0[++tot0] = a0[i];
for(int i=;i<=n;i++)
{
a[i].l = lower_bound(a0+,a0+tot0+,a[i].l) - a0;
a[i].r = lower_bound(a0+,a0+tot0+,a[i].r) - a0;
}
sort(a+,a+n+,cmp);
int j=n,tmpR = tot0+;
for(int i=tot0;i>=;i--)
{
while(j> && a[j].l >= i)
{
tmpR = min(tmpR, a[j].r);
j--;
}
minR[i][] = tmpR;
}
for(int t=;t<=;t++)
for(int i=;i<=tot0;i++)
{
if(minR[i][t-]<=tot0)
minR[i][t] = minR[minR[i][t-]][t-];
else minR[i][t] = tot0+;
}
for(int i=;i<=m;i++)
{
q[i].l = lower_bound(a0+,a0+tot0+,q[i].l) - a0;
q[i].r = lower_bound(a0+,a0+tot0+,q[i].r) - a0;
printf("%d\n",ask(q[i].l,q[i].r));
}
}
return ;
}
Interval query的更多相关文章
- HDU 4343 D - Interval query 二分贪心
D - Interval queryTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest ...
- 刷题总结——Interval query(hdu4343倍增+贪心)
题目: Problem Description This is a very simple question. There are N intervals in number axis, and M ...
- 【HDOJ】4343 Interval query
最大不相交集合的数量.思路是dp[i][j]表示已经有i个不相交集合下一个不相交集合的最右边界.离散化后,通过贪心解. /* 4343 */ #include <iostream> #in ...
- 【HDU 4343】Interval query(倍增)
BUPT2017 wintertraining(15) #8D 题意 给你x轴上的N个线段,M次查询,每次问你[l,r]区间里最多有多少个不相交的线段.(0<N, M<=100000) 限 ...
- HDU 4343 Interval query(贪心 + 倍增)
题目链接 2012多校5 Problem D 题意 给定$n$个区间,数字范围在$[0, 10^{9}]$之间,保证左端点严格大于右端点. 然后有$m$个询问,每个询问也为一个区间,数字范围在$[ ...
- UvaLA 3938 "Ray, Pass me the dishes!"
"Ray, Pass me the dishes!" Time Limit: 3000MS Memory Limit: Unkn ...
- LA 3938 动态最大连续和 线段树
题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- HDU 4343 贪心
D - Interval queryTime Limit: 1.5 Sec Memory Limit: 256 MB Description This is a very simple questio ...
- GSS系列(1)——GSS1&&GSS3
题意:询问一个区间内的最大连续子段和(GSS1),并且有单点修改的操作(GSS2). 思路:这个题目在老人家的大白鼠里出现过,不过那个是求两个下标,并且相同取更小值.——传的东西更多,判断也稍微繁琐一 ...
随机推荐
- VueJS标签消息显示HTML:v-html
HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...
- Android通过JNI实现与C语言的串口通讯操作蓝牙硬件模块
一直想写一份技术文档,但因为自感能力有限而无从下笔,近期做了个关于Android平台下实现与C语言的通讯来操作蓝牙模块的项目,中间碰到了很多问题,也在网上查了很多资料,在完毕主要功能后.也有一些人在网 ...
- Num 34 : HDOJ : 1205 吃糖果 [ 狄利克雷抽屉原理 ]
抽屉原理: 桌上有十个苹果,要把这十个苹果放到九个抽屉里,不管如何放,我们会发现至少会有一个抽屉里面至少放两个苹果. 这一现象就是我们所说的" ...
- Django-form组件补充
首先来看一个用户登录的实例 from django.forms import Form from django.forms import fields from django.forms import ...
- VMware安装ubuntu学习笔记(只是笔记)
VMware安装ubuntu开机黑屏/死机 1- Edit Ubuntu VM Configuration file (.vmx) 2- Add the following line cpuid.1. ...
- redis文档翻译_LRU缓存
Using Redis as an LRU cache使用Redis作为LRU缓存 出处:http://blog.csdn.net/column/details/redisbanli.html Whe ...
- MVC3 类型 System.Web.Mvc.ModelClientValidationRule 同时存在
用文本编辑器打开 工程名称 .csproj 找到 1. <Reference Include="System.Web.WebPages" /> 2. <Refe ...
- g++: command not found的解决
G++没有装或却没有更新 以下方法都可以试试: centos: yum -y update gcc yum -y install gcc+ gcc-c++ ubuntu: apt-get up ...
- EasyDarwin开源流媒体服务器提供的TS切片/HLS直播打包库
EasyHLS Github:https://github.com/EasyDarwin/EasyHLS EasyHLS是什么? EasyHLS是EasyDarwin开源流媒体社区开发的一款HLS打 ...
- bootstrap-table 行内编辑
1.文件引入 <link rel="stylesheet" href="bootstrap.css"> <link rel="sty ...