D - Interval query
Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88748#problem/D

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

HINT

题意

给你n个线段

m次询问,问你在ab区间内,最多放下几个不能相互重叠的线段

题解

直接n*m暴力

首先,我们按照右端点排序,然后能放进去就放进去,这种策略肯定是最优的

然后就暴力吧……15s

有两个想一想好像并咩有什么用的剪枝:

预处理出来第一个能放进区间的线段

预处理出哪些线段永远都不可能选的

代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#define maxn 110000
using namespace std; struct node
{
int x,y;
};
bool cmp(node a,node b)
{
if(a.y==b.y)
return a.x<b.x;
return a.y<b.y;
}
node a[maxn];
node b[maxn];
node qq[maxn];
int cc[maxn];
//node b[maxn];
int tot=;
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
tot=;
memset(a,,sizeof(a));
memset(b,,sizeof(b));
memset(qq,,sizeof(qq));
memset(cc,,sizeof(cc));
for(int i=;i<n;i++)
scanf("%d %d",&a[i].x,&a[i].y);
sort(a,a+n,cmp);
for(int i=;i<n;i++)
{
int flag=;
for(int j=i+;j<n;j++)
{
if(a[j].y>a[i].y)
break;
if(a[j].x<a[i].x)
continue;
flag=;
break;
}
if(!flag)
b[tot++]=a[i];
}
int x,y,ans;
for(int i=;i<m;i++)
{
scanf("%d%d",&qq[i].x,&qq[i].y);
int l=,r=tot-;
x = qq[i].x;
y = qq[i].y;
while(l<=r)
{
int mid=(l+r)>>;
if(b[mid].y<=x)
l=mid+;
else
r=mid-;
}
cc[i]=l;
}
for(int i=;i<m;i++)
{
x = qq[i].x,y = qq[i].y;
ans=;
int tmp=x;
int l = cc[i];
for(int j=l;j<tot;j++)
{
if(b[j].y<=y)
{
if(b[j].x<tmp)
continue;
tmp = b[j].y;
ans++;
}
else
break;
}
printf("%d\n",ans);
}
}
}

HDU 4343 D - Interval query 二分贪心的更多相关文章

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

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

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

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

  3. HDU 4343 贪心

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

  4. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  5. hdu 3433 A Task Process 二分+dp

    A Task Process Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  6. 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心

    /** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...

  7. 【bzoj2097】[Usaco2010 Dec]Exercise 奶牛健美操 二分+贪心

    题目描述 Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的小路上奔跑.这些奶牛的路径集合可以被表示成一个点集和一些连接 两个顶点的双向路,使得每对点之间恰好有一条简单路径. ...

  8. HDU 3622 Bomb Game(二分+2-SAT)

    Bomb Game Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. Codeforces_732D_(二分贪心)

    D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

随机推荐

  1. Java Error和Exception区别

    Error和Exception都继承自Throwable: 二者不同之处: Exception: 1.可以是可被控制(checked)或者不可控制(unchecked): 2.表示一个由程序员导致的错 ...

  2. 浅析android下如何通过jni监控wifi网络连接、dhcpcd执行和power电源控制

    libs/android_runtime/android_net_wifi_Wifi.cpp部分jni接口static JNINativeMethod gWifiMethods[] = {{ &quo ...

  3. hdu 3172 Virtual Friends(并查集)University of Waterloo Local Contest 2008.09

    题目比较简单,但作为长久不写题之后的热身题还是不错的. 统计每组朋友的朋友圈的大小. 如果a和b是朋友,这个朋友圈的大小为2,如果b和c也是朋友,那么a和c也是朋友,此时这个朋友圈的大小为3. 输入t ...

  4. 完美完全卸载Oracle 11g数据库

    Oracle 11g可在开始菜单中卸载,然后同时需要删除注册表中相关内容. 操作系统:windows10专业版. 卸载步骤: 1.停用oracle服务:进入计算机管理,在服务中,找到oracle开头的 ...

  5. Conversion to Dalvik format failed: Unable to execute dex: null

    [2013-11-19 14:18:48 - Dex Loader] Unable to execute dex: java.nio.BufferOverflowException. Check th ...

  6. 10、TV UI

     TV UI布局 1. 为大屏幕提供适当的布局源文件. 2. 确保UI在一定距离仍然可以看清. 3. 为高清电视提供高分辨率的图标和图像. 1. 把屏幕上的导航控制菜单放在屏幕的左边或者右边,并且将 ...

  7. Delphi 操作Word怎么控制光标的位置

    unit ControlWordS; interface uses Classes, Sysutils, Word97; type  TControlWord = class(TComponent)  ...

  8. (原创)LAMP教程5-配置VirtualBox虚拟机中centos6.4的网卡

    (原创)LAMP教程5-配置VirtualBox虚拟机中centos6.4的网卡 是的,今天我们要讲的是如何配置VirtualBox虚拟机中centos6.4的网卡,毕竟我们是要做网站开发的,没有网络 ...

  9. 3D 矩阵旋转

    如图,需要将点(向量) v(x, y, 0) 绕 z 轴旋转角度 θ,求旋转后的点(向量) v'(x', y', 0). 大概思路: 1. 将 v(x, y, 0) 分解, v(x, y, 0) = ...

  10. Unity中Instantiate一个prefab时需要注意的问题

    在调用Instantiate()方法使用prefab创建对象时,接收Instantiate()方法返回值的变量类型必须和声明prefab变量的类型一致,否则接收变量的值会为null.   比如说,我在 ...