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). 思路:这个题目在老人家的大白鼠里出现过,不过那个是求两个下标,并且相同取更小值.——传的东西更多,判断也稍微繁琐一 ...
随机推荐
- poj3181 Dollar Dayz
Description Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of to ...
- 基于django做增删改查组件,分页器组件
增删改查组件 一.Djangoadmin的启发 二.基于Djangoadmin实现数据的增删改查 分页器组件 分页器组件的介绍以及源码解读 补充:源码下载,
- 谈谈加载(Loading)的那点事
谈谈加载(Loading)的那点事 2013/10/12 | 分类: 设计 | 0 条评论 | 标签: 交互设计, 加载 分享到:33 原文出处: 搜狐焦点 对于加载(loading),想必大家都不陌 ...
- funhub 独立游戏团队诚邀策划,美术,技术,QA 大大加入(可远程办公)
我们刚成立的的独立游戏团队,base:广州,团队陆陆续续已经有 6 个成员了,现在还缺的岗位有策划,美术.不过有其 他岗位的仁人志士也可加入. 另外,我们支持远程办公,这是互联网行业的天然优势,一定要 ...
- Hibernate中的HQL语言
一.HQL语言简介 HQL全称是Hibernate Query Language,它提供了是十分强大的功能,它是针对持久化对象,直接取得对象,而不进行update,delete和insert等操作.而 ...
- 九度OJ 1129:Skew数 (大数运算)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:734 解决:548 题目描述: 在 skew binary表示中, 第 k 位的值xk表示xk*(2k+1-1). 每个位上的可能数字是0 ...
- 九度OJ 1107:搬水果 (贪心)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5190 解决:1747 题目描述: 在一个果园里,小明已经将所有的水果打了下来,并按水果的不同种类分成了若干堆,小明决定把所有的水果合成一堆 ...
- Java Virtual Machine (JVM) objects 虚拟机实例的产生 退出 两种线程
Apache Spark is built around a distributed collection of immutable Java Virtual Machine (JVM) object ...
- a completely rewritten architecture of Hadoop cluster
https://www.ibm.com/developerworks/library/bd-yarn-intro/
- Angular中的内置指令和自定义指令
NG中的指令,到底是什么(what)? 为什么会有(why)?以及怎样使用(how)? What: 在NG中,指令扩展HTML功能,为 DOM 元素调用方法.定义行为绑定数据等. Why: 最大程度减 ...