fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int z;
  6. cin>>z;
  7. int liczby[z];
  8. for(int i=0; i<z; i++)
  9. cin>>liczby[i];
  10. int kl,q;
  11. cin>>q;
  12. for(int i=0; i<q; i++){
  13. cin>>kl;
  14.  
  15.  
  16.  
  17. int a=0;
  18. int b=z-1;
  19. while(a<b){
  20. long long s=(a+b)/2;
  21. if(liczby[s]>=kl)
  22. b=s;
  23. else a=s+1;
  24. }
  25.  
  26.  
  27. int c=0;
  28. int d=z-1;
  29. while(c<d){
  30. long long s=(c+d+1)/2;
  31. if(liczby[s]<=kl)
  32. c=s;
  33. else d=s-1;
  34. }
  35.  
  36.  
  37. if(liczby[a]==kl)
  38. cout<<c-a+1<<endl;
  39. else cout<<0<<endl;
  40.  
  41. }
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 0.01s 5312KB
stdin
5
1 1 2 4 5
3
1 
2
3
stdout
2
1
0