题目:

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. 关于IT人的一些消遣区

    https://www.csdn.net/http://www.51cto.com/http://bestcbooks.com/http://www.jobbole.com/http://www.co ...

  2. vue 改变我们插值的符号{{}}改为${}

    delimiters的作用是改变我们插值的符号.Vue默认的插值是双大括号{{}}.但有时我们会有需求更改这个插值的形式. delimiters:['${','}'] 现在我们的插值形式就变成了${} ...

  3. Python全栈day 04

    Python全栈day 04 一.解释器/编译器 补充:编译型语言和解释型语言? # 编译型:代码写完后,编译器将其变成成另外一个文件,然后交给计算机执行. c c++,c# ,java # 解释型: ...

  4. http虚拟主机的简单配置训练

    http的虚拟主机 对于某些web访问站点而言,每天的访问量很少,因此真正的放一台服务器去进行web站点是很 浪费资源的,因此我们选择了虚拟主机 web处理模块的分类(MPM) 1.perfork 一 ...

  5. 硬盘安装Windows Server 2008(解决系统盘符变成D盘)

    硬盘安装Windows 2008系统方法 操作系统最好用的无疑是server 2003,但是现在Server 2003支持的软件越来越少,很多是故意不支持Server 2003了, 像php5.5以上 ...

  6. 1016-02-首页17-添加转发微博控件-计算转发配图的 Frame-------打印出 被转发微博的模型

    说明:HWStatus为微博模型,_retweeted_status 为返回的数据( 一条微博模型)里面的一个属性,_retweeted_status 不为空表示此微博是否转发了其他微博._retwe ...

  7. [Codeforces947D]Riverside Curio(思维)

    Description 题目链接 Solution 设S[i]表示到第i天总共S[i]几个标记, 那么满足S[i]=m[i]+d[i]+1 m[i]表示水位上的标记数,d[i]表示水位下的标记数 那么 ...

  8. Unity Occlusion Culling 遮挡剔除研究

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/52684127 作者:car ...

  9. centos 6.X 关闭selinux

    SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统.在这种访问控制体系的限制下,进程只能访问那 ...

  10. leetcode 【 Merge Two Sorted Lists 】 python 实现

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...