abinash_hstu

lower_bound - upper_bound

Jan 15th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. //Abinash Ghosh
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cctype>
  5. #include <cmath>
  6. #include <cstring>
  7. #include <iostream>
  8. #include <iomanip>
  9. #include <vector>
  10. #include <list>
  11. #include <stack>
  12. #include <queue>
  13. #include <map>
  14. #include <set>
  15. #include <string>
  16. #include <utility>
  17. #include <sstream>
  18. #include <algorithm>
  19. #include <ctime>
  20. #include <cassert>
  21. #include <limits>
  22. using  namespace  std;
  23.  
  24. #define PI acos(-1.0)
  25. #define mem(a,b) memset(a,b,sizeof(a))
  26. #define gcd(a,b) __gcd(a,b)
  27. #define pb push_back
  28. #define mp make_pair
  29. #define x first
  30. #define y second
  31. #define Sort(x) sort(x.begin(),x.end())
  32. #define FOR(i, b, e) for(int i = b; i <= e; i++)
  33. #define FORR(i, b, e) for(int i = b; i >= e; i--)
  34. #define FORI(i, s) for (__typeof ((s).end ()) i = (s).begin (); i != (s).end (); ++i)
  35. #define pr(x) cout<<x<<"\n"
  36. #define pr2(x,y) cout<<x<<" "<<y<<"\n"
  37. #define pr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<"\n";
  38. #define ppr(a) cout<<a.x<<" "<<a.y<<"\n"
  39. #define READ(f) freopen(f, "r", stdin)
  40. #define WRITE(f) freopen(f, "w", stdout)
  41.  
  42. typedef  long long ll;
  43. typedef  pair <int, int> pii;
  44. typedef  pair <double , double> pdd;
  45. typedef  pair <ll , ll > pll;
  46. typedef  vector <int> vi;
  47. typedef  vector <pii> vpii;
  48. typedef  vector <ll > vl;
  49.  
  50. //int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
  51. //int dx[]={1,1,0,-1,-1,-1,0,1};
  52. //int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
  53. //int dx[]={2,1,-1,-2,-2,-1,1,2};
  54. //int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
  55.  
  56. #define MAX 100007
  57. #define EPS 1e-9
  58.  
  59. int main()
  60. {
  61.     int myints[] = {10,10,10,20,20,20,30,30,40,50,50},lb,ub;
  62.     //               0  1  2  3  4  5  6  7  8  9 10
  63.     //               0  1 .....................n-1 n
  64.     vector<int> v(myints,myints+11);
  65.     ///lowerbound
  66.     {
  67.         /// if value exists then it returns  the first occurence position
  68.         lb=lower_bound(v.begin(),v.end(),10)-v.begin();
  69.         pr2(lb,v[lb]);///0 10
  70.  
  71.         /// if value does not exist and v[0]<value<v[n] then it return (minimum value) > value  position
  72.         lb=lower_bound(v.begin(),v.end(),21)-v.begin();
  73.         pr2(lb,v[lb]);///6 30
  74.  
  75.         /// if value does not exist and value<v[0] then it return 0th  position
  76.         lb=lower_bound(v.begin(),v.end(),6)-v.begin();
  77.         pr2(lb,v[lb]);///0 10
  78.  
  79.         /// if value does not exist and value>v[n] then it return (n+1)th position
  80.         lb=lower_bound(v.begin(),v.end(),51)-v.begin();
  81.         pr2(lb,v[lb]);///11 0
  82.     }
  83.     /// upperbound
  84.     /// Comment : return minimum value's position which > val
  85.     {
  86.         /// if value exists then it returns  the last occurence position+1
  87.         ub=upper_bound(v.begin(),v.end(),40)-v.begin();
  88.         pr2(ub,v[ub]);///9 50
  89.  
  90.         /// if value does not exist and v[0]<value<v[n] then it return (minimum value) > value  position
  91.         ub=upper_bound(v.begin(),v.end(),41)-v.begin();
  92.         pr2(ub,v[ub]);///9 50
  93.  
  94.         /// if value does not exist and value<v[0] then it return 0th  position
  95.         ub=upper_bound(v.begin(),v.end(),6)-v.begin();
  96.         pr2(ub,v[ub]);///0 10
  97.  
  98.         /// if value does not exist and value>v[n] then it return (n+1)th position
  99.         ub=upper_bound(v.begin(),v.end(),51)-v.begin();
  100.         pr2(ub,v[ub]);///11 0
  101.     }
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment