题意:

给出数轴上的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的更多相关文章

  1. HDU 4343 D - Interval query 二分贪心

    D - Interval queryTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest ...

  2. 刷题总结——Interval query(hdu4343倍增+贪心)

    题目: Problem Description This is a very simple question. There are N intervals in number axis, and M ...

  3. 【HDOJ】4343 Interval query

    最大不相交集合的数量.思路是dp[i][j]表示已经有i个不相交集合下一个不相交集合的最右边界.离散化后,通过贪心解. /* 4343 */ #include <iostream> #in ...

  4. 【HDU 4343】Interval query(倍增)

    BUPT2017 wintertraining(15) #8D 题意 给你x轴上的N个线段,M次查询,每次问你[l,r]区间里最多有多少个不相交的线段.(0<N, M<=100000) 限 ...

  5. HDU 4343 Interval query(贪心 + 倍增)

    题目链接  2012多校5 Problem D 题意  给定$n$个区间,数字范围在$[0, 10^{9}]$之间,保证左端点严格大于右端点. 然后有$m$个询问,每个询问也为一个区间,数字范围在$[ ...

  6. UvaLA 3938 "Ray, Pass me the dishes!"

                            "Ray, Pass me the dishes!" Time Limit: 3000MS   Memory Limit: Unkn ...

  7. LA 3938 动态最大连续和 线段树

    题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  8. HDU 4343 贪心

    D - Interval queryTime Limit: 1.5 Sec Memory Limit: 256 MB Description This is a very simple questio ...

  9. GSS系列(1)——GSS1&&GSS3

    题意:询问一个区间内的最大连续子段和(GSS1),并且有单点修改的操作(GSS2). 思路:这个题目在老人家的大白鼠里出现过,不过那个是求两个下标,并且相同取更小值.——传的东西更多,判断也稍微繁琐一 ...

随机推荐

  1. Error: Cannot find module 'webpack'错误解决

    $ npm install webpack -g $ npm install webpack-cli -g 全局安装webpack $ npm run dev Error: Cannot find m ...

  2. Spring AOP(转载)

    此前对于AOP的使用仅限于声明式事务,除此之外在实际开发中也没有遇到过与之相关的问题.最近项目中遇到了以下几点需求,仔细思考之后,觉得采用AOP 来解决.一方面是为了以更加灵活的方式来解决问题,另一方 ...

  3. MFC学习之Radio---MFC Radio按钮组的使用例子

    首先我们要完成一个功能,在一个添加新用户的场景里,通过Radio按钮来判断用户选择的是管理员还是普通用户. 要使用Radio组的功能首先我们必须作如下设置: 1.2个Radio按钮的ID号不同,但是他 ...

  4. Apache/2.4.9启动错误:AH01630: client denied by server configuration

    在升级Yii框架1.11->2.0beta时,PHP升级到5.5.顺带升级Apache2.2.x到2.4.9. 把原有vhost配置移植过来,出现Apache启动错误: AH01630: cli ...

  5. Delphi的类方法不是静态方法

    Delphi中,类方法不是你理解的静态方法 Delphi中的类方法与C++类中的static方法完全没有可比性.Delphi中的类方法是有Self的,而Self就是类本身(注意不是对象),而这个Sel ...

  6. 03-树2 List Leaves(25 point(s)) 【Tree】

    03-树2 List Leaves(25 point(s)) Given a tree, you are supposed to list all the leaves in the order of ...

  7. Mac终端基本指令,一些实用命令的收集.

    基本命令1.列出文件ls 参数 目录名        例: 看看驱动目录下有什么:ls /System/Library/Extensions参数 -w 显示中文,-l 详细信息, -a 包括隐藏文件 ...

  8. Docker容器的网络连接:

    yw1989@ubuntu:~$ ifconfig docker0 Link encap:Ethernet HWaddr 02:42:97:61:42:9f inet addr:172.17.0.1 ...

  9. 一个selenium笔试题——去哪网首页获取符合要求的url并保存

    今天在群里看到这样一个笔试题:请使用任何熟悉的面向对象编程语言,编写代码,获取http://www.qyer.com页面中,所有</a>标签"href"属性值包含英文单 ...

  10. 花了5天时间,终于解决了一个bug,心情非常愉快,憋了这么久,不吐不快

    http://www.cnweblog.com/fly2700/archive/2011/12/06/318916.html (转载) 花了5天时间,终于解决了一个bug,心情非常愉快,憋了这么久,不 ...