题目:

Problem Description

This is a very simple question. There are N intervals in number axis, and M queries just like “QUERY(a,b)” indicate asking the maximum number of the disjoint intervals between (a,b) .

Input

There are several test cases. For each test case, the first line contains two integers N, M (0<N, M<=100000) as described above. In each following N lines, there are two integers indicate two endpoints of the i-th interval. Then come M lines and each line contains two integers indicating the endpoints of the i-th query.
You can assume the left-endpoint is strictly less than the right-endpoint in each given interval and query and all these endpoints are between 0 and 1,000,000,000.

Output

For each query, you need to output the maximum number of the disjoint intervals in the asked interval in one line.

Sample input

3 2
1 2
2 3
1 3
1 2
1 3

Sample Output

1
2

Author

HIT

Source

 

题解:

  首先容易想到那些覆盖了其他较小区间的区间是可以忽略的··因为对于每个询问要尽量多取区间···

  然后考虑对于每个询问···我们肯定会采取贪心策略,从询问范围的左端开始找··如果找到一个区间就取该区间··然后跳到该区间的右端··再继续向右边找下一个区间··这个方法肯定是最优的····

  至于如何寻找,我们考虑倍增(现在发现关于区间的一些查询问题如果用数据结构解决不了的话基本上就是倍增了··),用g[i][j]表示从i点出发··在找到2^j个互不相交的区间后到达的最右端点··首先预处理g[i][0],然后再一次处理g[i][1],g[i][2]....即可

  最后对于每个询问我们像找lca那样跳区间··边跳边更新答案就可以了

代码:

  

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int N=2e5+;
inline int R(){
char c;int f=;
for(c=getchar();c<''||c>'';c=getchar());
for(;c<=''&&c>='';c=getchar()) f=(f<<)+(f<<)+c-'';
return f;
}
struct node{int l,r;}a[N],q[N],c[N];
int b[N*],cnt,n,m,stack[N],top=,bel[N*],g[N*][];
inline void lsh(){
sort(b+,b+cnt+);
cnt=unique(b+,b+cnt+)-b-;
for(int i=;i<=n;i++){
a[i].l=lower_bound(b+,b+cnt+,a[i].l)-b;
a[i].r=lower_bound(b+,b+cnt+,a[i].r)-b;
}
for(int i=;i<=m;i++){
q[i].l=lower_bound(b+,b+cnt+,q[i].l)-b;
q[i].r=lower_bound(b+,b+cnt+,q[i].r)-b;
}
}
inline bool cmp(node a,node b){
if(a.l==b.l) return a.r>b.r;
else return a.l<b.l;
}
inline void pre(){
sort(a+,a+n+,cmp);
for(int i=;i<=n;i++){
while(top&&a[i].l>=a[stack[top]].l&&a[i].r<=a[stack[top]].r) top--;
stack[++top]=i;
}
n=;
for(int i=;i<=top;i++) c[++n]=a[stack[i]];
int tail=;
for(int i=;i<=n;i++){
while(tail<=c[i].l) g[tail][]=c[i].r,tail++;
}
}
inline int query(int l,int r){
int ans=,i=l;
for(int j=;j>=;j--)
if(g[i][j]&&g[i][j]<=r) i=g[i][j],ans+=(<<j);
return ans;
}
int main(){
//freopen("a.in","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF){
top=cnt=;memset(g,,sizeof(g));
for(int i=;i<=n;i++) a[i].l=R(),a[i].r=R(),b[++cnt]=a[i].l,b[++cnt]=a[i].r;
for(int i=;i<=m;i++) q[i].l=R(),q[i].r=R(),b[++cnt]=q[i].l,b[++cnt]=q[i].r;
lsh();pre();
for(int i=;i<=;i++)
for(int j=;j<=cnt;j++) g[j][i]=g[g[j][i-]][i-];
for(int i=;i<=m;i++) cout<<query(q[i].l,q[i].r)<<endl; }
return ;
}

刷题总结——Interval query(hdu4343倍增+贪心)的更多相关文章

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

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

  2. NOI题库刷题日志 (贪心篇题解)

    这段时间在NOI题库上刷了刷题,来写点心得和题解 一.寻找平面上的极大点 2704:寻找平面上的极大点 总时间限制:  1000ms  内存限制:  65536kB 描述 在一个平面上,如果有两个点( ...

  3. C#LeetCode刷题-贪心算法

    贪心算法篇 # 题名 刷题 通过率 难度 44 通配符匹配   17.8% 困难 45 跳跃游戏 II   25.5% 困难 55 跳跃游戏   30.6% 中等 122 买卖股票的最佳时机 II C ...

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

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

  5. 好像leeceode题目我的博客太长了,需要重新建立一个. leecode刷题第二个

    376. Wiggle Subsequence               自己没想出来,看了别人的分析. 主要是要分析出升序降序只跟临近的2个决定.虽然直觉上不是这样. 455. 分发饼干     ...

  6. 【刷题记录】BZOJ-USACO

    接下来要滚去bzoj刷usaco的题目辣=v=在博客记录一下刷题情况,以及存一存代码咯.加油! 1.[bzoj1597][Usaco2008 Mar]土地购买 #include<cstdio&g ...

  7. NOIp2018停课刷题记录

    Preface 老叶说了高中停课但是初中不停的消息后我就为争取民主献出一份力量 其实就是和老师申请了下让我们HW的三个人听课结果真停了 那么还是珍惜这次机会好好提升下自己吧不然就\(AFO\)了 Li ...

  8. OI刷题录——hahalidaxin

    16-3-25  —— bzoj 2049 [Sdoi2008]Cave 洞穴勘测:LCT入门 bzoj 2002 [Hnoi2010]Bounce 弹飞绵羊:LCT Tsinsen A1303. t ...

  9. PTA刷题记录

    考虑到PAT甲级考试和开学后的XCPC比赛,决定寒假把PAT (Advanced Level) Practice刷完,进度条会在这篇博客下更新.由于主要以记录为主,大体上不会像单篇题解那么详细,但是对 ...

随机推荐

  1. 谈谈对bug的一点想法,说说做好日志记录的重要性

    说起程序猿,总绕不开的一个话题就是bug,估计每个程序猿听到某某测试跑过来一脸淫笑的告诉你这个功能有个bug的时候,总有种恨不得掐死他的想法.其实程序猿跟bug的关系,感觉有点像父亲和儿子的关系,自己 ...

  2. MySQL5.6基于mysql-proxy实现读写分离

    已经搭建好MySQL主从架构 10.205.22.185 #mysql-proxy 10.205.22.186 #master 10.205.22.187 #slave 1.安装mysql-proxy ...

  3. Struts2之类范围拦截器和方法拦截器

    1.Struts2拦截器的体系结构 Struts2拦截器最大的特点是其透明性,即用户感觉不到它的存在,但我们在使用Struts2框架时,拦截器时时刻刻都在帮助我们处理很多事情. 包括: 文件上传 表单 ...

  4. OracleWeblogic12C安装教程

    一,安装WebLogic Server 1. 双击exe安装文件 2.准备安装文件 3. 生成向导序列 4. 选择安装路径 5. 开始安装 经过以上步骤,weblogic已经成功安装到了你的电脑上,但 ...

  5. php-5.6.26源代码 - PHP文件汇编成opcode(require、include的差异)

    文件 php-5.6.26/Zend/zend_language_scanner.c ZEND_API zend_op_array *compile_file(zend_file_handle *fi ...

  6. Numpy数据存取与函数

    数据的CSV文件存取 多维数据的存取 NumPy的随机数函数 NumPy的统计函数 NumPy的梯度函数

  7. Myeclipse代码格式化的样式和保存自动格式化

    第一种方法:下载格式化代码样式文件,参考这位老兄的方法(包含了保存自动格式化):http://blog.csdn.net/u010028869/article/details/49780515   下 ...

  8. asp.net 常用几种下载方式

    protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法TransmitFile来解决使 ...

  9. IIS相关服务和无法启动服务W3SVC错误提示

    首先,打开“服务”查看下面的服务是否启动.(下面的两个服务就是跟IIS相关的服务,计算机(右键)->管理->服务和应用程序->服务) Windows Process Activati ...

  10. Remote使用出现的问题及解决办法

    最近尝试跟着虫师的OP模式所写的bbs代码,应用自己的项目尝试修改,在第一步Remote启动Firefox上便出错,当前selenium2.53,firefox47.1,selenium server ...