Advertisement
wojiaocbj

lowerbound

Jun 13th, 2022
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.44 KB | None | 0 0
  1. /*
  2.  Author: 曹北健
  3.  Result: AC    Submission_id: 3474027
  4.  Created at: Wed May 12 2021 16:24:46 GMT+0800 (China Standard Time)
  5.  Problem_id: 4335    Time: 333    Memory: 7468
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <ctype.h>
  10. #include <math.h>
  11. #include <string.h>
  12. #include <assert.h>
  13. #pragma warning(disable:4996)
  14. #define MAX(a,b) (((a)>(b))?(a):(b))
  15. #define MIN(a,b) (((a)<(b))?(a):(b))
  16. typedef long long LL;
  17. typedef unsigned long long ULL;
  18. int compare(const void *p,const void *q){
  19.     int a = *(int *)p,b = *(int *)q;
  20.     if(a < b)return -1;
  21.     else if(a > b)return 1;
  22.     else return 0;
  23. }
  24. void *lowerbound(void *base,void *key,size_t count,size_t itemsize,int(*compare)(const void *,const void *)){
  25.     assert(base && compare);
  26.     size_t lo = 0,hi = count - 1,mid = 0;
  27.     if(compare(key,(char *)base + hi * itemsize) > 0){
  28.         return (char *)base + count * itemsize;
  29.     }
  30.     char *pmid = 0;
  31.     while(lo < hi){
  32.         mid = (lo + hi) >> 1;
  33.         pmid = ((char *)base) + mid * itemsize;
  34.         if(compare(pmid,key) < 0){
  35.             lo = mid + 1;
  36.         }
  37.         else{
  38.             hi = mid;
  39.         }
  40.     }
  41.     return (char *)base + lo * itemsize;
  42. }
  43. void *upperbound(void *base,void *key,size_t count,size_t itemsize,int(*compare)(const void *,const void *)){
  44.     assert(base && compare);
  45.     size_t lo = 0,hi = count - 1,mid;
  46.     if(compare(key,(char *)base + hi * itemsize) >= 0){
  47.         return (char *)base + count * itemsize;
  48.     }
  49.     char *pmid = 0;
  50.     while(lo < hi){
  51.         mid = (lo + hi) >> 1;
  52.         char *pmid = ((char *)base) + mid * itemsize;
  53.         if(compare(pmid,key) <= 0){
  54.             lo = mid + 1;
  55.         }
  56.         else{
  57.             hi = mid;
  58.         }
  59.     }
  60.     return (char *)base + lo * itemsize;
  61. }
  62. int a[1919810] = {0};
  63. int main(){
  64. #ifdef _DEBUG
  65.     freopen("input.txt","r",stdin);
  66. #ifdef OFILE
  67.     freopen("test.txt","w",stdout);
  68. #endif
  69. #endif
  70.     int i,n,m,t;
  71.     scanf("%d%d",&n,&m);
  72.     for(i = 0;i < n;i++){
  73.         scanf("%d",a + i);
  74.     }
  75.     while(m--){
  76.         scanf("%d",&t);
  77.         int *p = (int *)lowerbound(a,&t,n,sizeof(int),compare);
  78.         //printf("%d ",*p);
  79.         if(*p != t){
  80.             puts("-1");
  81.         }
  82.         else{
  83.             printf("%d\n",p - a + 1);
  84.         }
  85.     }
  86. #ifdef _DEBUG
  87.     freopen("CON","r",stdin);
  88. #ifdef OFILE
  89.     freopen("CON","w",stdout);
  90. #endif
  91.     system("pause");
  92. #endif
  93.     return 0;
  94.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement