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). 思路:这个题目在老人家的大白鼠里出现过,不过那个是求两个下标,并且相同取更小值.——传的东西更多,判断也稍微繁琐一 ...
随机推荐
- .Net 平台WebService的创建、部署和使用介绍
.NET平台内建了对Web Service的支持,包括Web Service的构建和使用.与其它开发平台不同,使用.NET平台,你不需要其他的工具或者SDK就可以完成Web Service的开发了.. ...
- Linux中的du和df命令
现在也将前阵子学习到du/df两个命令总结一下吧.前阵子测试工作中有遇到过由于磁盘空间满导致程序无法执行到情况,所以使用了df和du两个命令. du查看目录大小,df查看磁盘使用情况.我常使用的命令( ...
- ORACLE 36进制和10进制,互相转换函数
第一部分 --36转10进制 create or replace function f_36to10 (str varchar) return int is returnValue int; s ...
- SAP-ABAP系列 第二篇SAP ABAP开发基础
第二章SAP ABAP开发基础 1.ABAP数据类型及定义 ABAP程序中共包含8种基本数据类型定义, 类型名称 描述 属性 C Character Text (字符类型) 默认长度=1,默认值 = ...
- 主题:iframe高度的自适应
在项目开发中,遇到的一个问题.弹出的页面中有iframe.例 <iframe src="www.baidu.html" width="100%" char ...
- 【BZOJ2216】[Poi2011]Lightning Conductor 决策单调性
[BZOJ2216][Poi2011]Lightning Conductor Description 已知一个长度为n的序列a1,a2,...,an.对于每个1<=i<=n,找到最小的非负 ...
- Hackerspace
Hackerspace Software - HackerspaceWiki https://wiki.hackerspaces.org/Hackerspace_Software Hackerspac ...
- Angular中的内置指令和自定义指令
NG中的指令,到底是什么(what)? 为什么会有(why)?以及怎样使用(how)? What: 在NG中,指令扩展HTML功能,为 DOM 元素调用方法.定义行为绑定数据等. Why: 最大程度减 ...
- 使用MSSQL同步&发布数据库快照遇到错误:对路径“xxxxx”访问被拒绝的解决方法
使用MSSQL同步 数据库同步做后后测试:先在同步那台服务器(服务器A)数据库里修改里面数据库,然后再去被同步那台服务器(服务器B)看下数据有没被同步过去 发布数据库快照遇到错误:对路径“xxxxx” ...
- Ubuntu Firefox没有声音的解决方案
安装ubuntu-restricted-extras sudo apt-get install ubuntu-restricted-extras 参考博文:解决ubuntu中firefox没有声音的问 ...