Advertisement
shamiul93

LightOJ 1088 - Points in Segments

Feb 28th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. /**
  2. @author - Rumman BUET CSE'15
  3. problem - 1088 - Points in Segments
  4.  
  5. link - http://www.lightoj.com/volume_showproblem.php?problem=1088
  6.  
  7. Concept -
  8.  
  9.     ** This problem uses a basic concept of Binary Search. - Upper bound & Lower bound.
  10.  
  11.     # Lower bound:
  12.         The number which is leftmost and either greater than or equal to our target number is called the lower bound
  13.         of that number.
  14.  
  15.     ** We know how to get lower bound.
  16.  
  17.  
  18.     # Upper bound:
  19.         The number which is the leftmost and strictly greater than our target number in a sorted array is called
  20.         the upper bound of the number.
  21.  
  22.     ** We know how to find lower bound. I used a trick to find upper bound.
  23.  
  24.     # Think , if we want to get an integer number which is obviously and strictly greater than our target number ,
  25.       we can increase the number by 1 and take the lower bound of that number.
  26.       Thus we can find upper and lower bound by only one function. There are some other ways. But I found it useful.
  27.       Have to test it more later.
  28.  
  29. */
  30.  
  31.  
  32. #include<bits/stdc++.h>
  33. using namespace std ;
  34. #define ll long long
  35. #define fo freopen("out.txt","w",stdout)
  36. #define fi freopen("in.txt","r",stdin)
  37. #define DEBUG printf("hi\n");
  38. #define DEBUG2 printf("bi\n");
  39.  
  40. using namespace std ;
  41.  
  42. vector<ll> v ;
  43. ll n, q ;
  44. ll f, l ;
  45.  
  46. void input()
  47. {
  48.     v.clear() ;
  49.     for(ll i = 0 ; i< n ; i++)
  50.     {
  51.         ll a ;
  52.         scanf("%lld",&a) ;
  53.         v.push_back(a) ;
  54.     }
  55.  
  56.     sort(v.begin(), v.end()) ;
  57. }
  58.  
  59. ll LowerBound(ll f)
  60. {
  61.     ll lo, hi, mid ;
  62.     hi = v.size() - 1 ;
  63.     lo = 0 ;
  64.     while(lo <= hi)
  65.     {
  66.         mid = (lo + hi)/2 ;
  67.  
  68.         if(f == v[mid])
  69.         {
  70.             hi = mid - 1 ;
  71.         }
  72.         else if(f > v[mid])
  73.         {
  74.             lo = mid + 1 ;
  75.         }
  76.         else
  77.         {
  78.             hi = mid - 1 ;
  79.         }
  80.     }
  81.  
  82.     return lo ;
  83. }
  84.  
  85. int main()
  86. {
  87.     ios::sync_with_stdio();
  88. //    fo ;
  89. //    fi ;
  90.  
  91.  
  92.     ll T, t = 0 ;
  93.  
  94.     cin >> T ;
  95.  
  96.     while(T--)
  97.     {
  98.  
  99.         t++ ;
  100.         printf("Case %lld:\n",t) ;
  101.         scanf("%lld %lld",&n, &q) ;
  102.  
  103.         input();
  104.  
  105.         while(q--)
  106.         {
  107.  
  108.             scanf("%lld %lld",&f, &l);
  109.  
  110.             if(f > l)
  111.             {
  112.                 swap(f, l) ;
  113.             }
  114.  
  115.             ll Firstidx, Lastidx, ans ;
  116.  
  117.             Firstidx = LowerBound(f) ;
  118.  
  119.             l++ ;
  120.  
  121.             Lastidx = LowerBound(l) ;
  122.  
  123. //            cout << f << " " << --l << endl ;
  124.  
  125. //            cout << Firstidx << " " << Lastidx << endl  ;
  126.             ans =  Lastidx - Firstidx   ;
  127.  
  128.             printf("%lld\n", ans) ;
  129. //            cout << endl ;
  130.         }
  131.     }
  132.  
  133.     return 0 ;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement