Advertisement
Kali_prasad

element freq using 2bs

Mar 26th, 2022
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #pragma GCC optimize ("O3")
  2. #pragma GCC target ("sse4")
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7. typedef long long ll;
  8. typedef pair<int, int> pii;
  9. typedef pair<string,int> psi;
  10. typedef vector<int> vi;
  11. typedef vector<string> vs;
  12. typedef vector<ll> vll;
  13. typedef vector<vector<int>> vvi;
  14. typedef vector<vector<string>> vvs;
  15. typedef vector<vector<ll>> vvll;
  16.  
  17. #define FOR(i, a, b) for (ll i=a; i<=(b); i++)
  18. #define FORd(i,b,a) for (ll i =b; i >= a; i--)
  19. #define sz(x) (int)(x).size()
  20. #define mp make_pair
  21. #define pb push_back
  22. #define f first
  23. #define s second
  24. #define ins insert
  25.  
  26. const int MOD = 1000000007;
  27. //type functions here
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36. ll BS1(vi v,ll k)
  37. {
  38.     ll low=0,high=sz(v)-1,p1=0;
  39.     ll mid;
  40.     while(low<=high)
  41.     {
  42.         mid=(low+high)/2;
  43.         if(v[mid]<k)
  44.             low=mid+1;
  45.         else if(v[mid]>k)
  46.             high=mid-1;
  47.         else
  48.         {  p1=mid;
  49.             high=mid-1;
  50.          }
  51.     }
  52.     return p1;
  53. }
  54. ll BS2(vi v,ll k)
  55. {
  56.     ll low=0,high=sz(v)-1,p2=-1;//observe p2 value
  57.     ll mid;
  58.     while(low<=high)
  59.     {
  60.         mid=(low+high)/2;
  61.         if(v[mid]<k)
  62.             low=mid+1;
  63.         else if(v[mid]>k)
  64.             high=mid-1;
  65.         else
  66.         {  p2=mid;
  67.            low=mid+1;
  68.          }
  69.     }
  70.     return p2;
  71. }
  72.  
  73. int main() {
  74.     /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
  75.     int size,qsize;
  76.     cin>>size;
  77.     vi v;
  78.     FOR(i,1,size)
  79.     {
  80.         ll temp;
  81.         cin>>temp;
  82.         v.pb(temp);
  83.     }
  84.     sort(v.begin(),v.end());
  85.     cin>>qsize;
  86.     FOR(i,1,qsize)
  87.     {
  88.         ll temp;
  89.         cin>>temp;
  90.        
  91.         ll x=temp;
  92.          ll temp2= ((BS2(v,x))-(BS1(v,x))+1);
  93.         cout<<temp2<<endl;
  94.     }
  95.    
  96.    
  97.    
  98.     return 0;
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement