zoukankan      html  css  js  c++  java
  • 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: ",++count);
    while(m--) {
    scanf("%d",&k);
    i=seach1(0,n-1,k);
    printf("%d",i);
    j=seach2(0,n-1,k);
    printf("%d ",i-j);//相减
    }
    }
    return 0;
    }

  • 相关阅读:
    9- 遍历map集合的方法
    linux下修改了tomcat端口之后无法访问
    汪汪
    无题
    python之禅
    kettle连接oracle出现Error connecting to database: (using class oracle.jdbc.driver.OracleDriver)
    Android camera
    网站部署,网站要求需要支持mb_substring
    oracle笔记
    CSS jQuery 图片全屏切换
  • 原文地址:https://www.cnblogs.com/thefirstfeeling/p/4410924.html
Copyright © 2011-2022 走看看