hdu 4325



//用二分查找,先找到x小于等于m的有几个数这些数有可能y大于u即为符合。
//然后找这些数中y小于u的即为排除
//相减即可
#include<stdio.h>
#include<stdlib.h>
#define N 100100
struct node {
int x,y;
}a[N],b[N];
int n;
int cmp1(const void *a,const void *b) {//对x小到大排序
if((*(struct node *)a).x==(*(struct node *)b).x)
return (*(struct node *)a).y-(*(struct node *)b).y;
return (*(struct node *)a).x-(*(struct node *)b).x;
}
int cmp2(const void *a,const void *b) {//对y从小到大排序
if((*(struct node *)a).y==(*(struct node *)b).y)
return (*(struct node *)a).x-(*(struct node *)b).x;
return (*(struct node *)a).y-(*(struct node *)b).y;
}
int seach1(int start,int end,int u) {
int mid;
if(a[start].x>u)//最小的都大于u那么肯定没有一个了
return 0;
if(a[end].x<=u)//最大的都小于u那么肯定有end+1中可能符合
return end+1;
while(end>=start) {
          mid=(start+end)/2;
 if(a[mid].x>u)
 end=mid-1;
 else 
 start=mid+1;
 }
return start;//找到个数
}
int seach2(int start,int end,int u) {
int mid;
if(b[start].y>=u)
return 0;
if(b[end].y<u)
return end+1;
while(end>=start) {
          mid=(start+end)/2;
 if(b[mid].y>=u)
 end=mid-1;
 else 
 start=mid+1;//找到个数
 }
return start;
}
int main() {
int i,j,k,m,t,count=0;
scanf("%d",&t);
while(t--) {
scanf("%d%d",&n,&m);
for(i=0;i<n;i++) {
scanf("%d%d",&a[i].x,&a[i].y);
b[i].x=a[i].x;b[i].y=a[i].y;
}
qsort(a,n,sizeof(a[0]),cmp1);
qsort(b,n,sizeof(b[0]),cmp2);
printf("Case #%d:\n",++count);
while(m--) {
scanf("%d",&k);
i=seach1(0,n-1,k);
printf("%d",i);
j=seach2(0,n-1,k);
printf("%d\n",i-j);//相减
}
}
return 0;
}

转载于:https://www.cnblogs.com/thefirstfeeling/p/4410924.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值