题目:

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. LeetCode-177:第N高的薪水

    第N高的薪水与第二高的薪水,解题思路是一样的,只要对LeetCode-176的SQL做一下变形,便可以满足这题,详见:https://www.cnblogs.com/zouqf/p/10282392. ...

  2. ethereum(以太坊)(三)--合约单继承与多继承

    pragma solidity ^0.4.0; // priveta public internal contract Test{ //defualt internal uint8 internal ...

  3. 阿里云Linux服务器,挂载硬盘并将系统盘数据迁移到数据盘

    因为之前用宝塔上线,宝塔只挂载了系统盘50G,打开阿里云云盘列表发现系统盘无法直接升级,故另买一块数据盘挂载到Linux服务器下,下面根据网上教程再结合我实际情况讲解一下实际操作,其实非常easy l ...

  4. python中的文件操作小结2

    ''' #-----------文件修改---------- f=open("test_1",'r',encoding="utf-8") f2=open(&qu ...

  5. 779. K-th Symbol in Grammar

    class Solution { public: int kthGrammar(int N, int K) { return helper(N, K, false); } int helper(int ...

  6. POJ:2100-Graveyard Design(尺取)

    Graveyard Design Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 8504 Accepted: 2126 Cas ...

  7. [Codeforces958E2]Guard Duty (medium)(区间DP)

    Description 题目链接 Solution 可以把题目转化一下模型,将间隔取出来,转化为N-1个数,限制不能取相邻两个数,求取K个数的最小价值 设DP[i][j]表示前i个数取j个最大价值(第 ...

  8. HNU暑假训练第一场C.Ninja Map

    一.题目大意 Intersections of Crossing Path City are aligned to a grid. There are N east-west streets whic ...

  9. Pandas库入门

    pandas库的series类型

  10. WPF图片预览之移动、旋转、缩放

    原文:WPF图片预览之移动.旋转.缩放 RT,这个功能比较常见,但凡涉及到图片预览的都跑不了,在说自己的实现方式前,介绍一个好用的控件:Extended.Toolkit中的Zoombox,感兴趣的同学 ...