fork download
  1. #include <bits/stdc++.h>
  2. #define NMAX 100005
  3. using namespace std;
  4. int n , k;
  5. int Male[NMAX] , Female[NMAX];
  6. int NumMale , NumFemale;
  7.  
  8. long long ans ;
  9.  
  10. void process()
  11. {
  12. cin>> n >> k;
  13. for(int i = 1 ; i <= n ; i++)
  14. {
  15. int height;
  16. cin>>height;
  17.  
  18. if(height > 0) Male[++NumMale] = height;
  19.  
  20. if(height <= 0 ) Female[++NumFemale] = -height;
  21. }
  22.  
  23. sort(Male + 1 , Male + NumMale + 1);
  24.  
  25. for(int i = 1 ; i <= NumFemale ; i++)
  26. {
  27. int l = lower_bound(Male + 1 , Male + NumMale + 1 , Female[i]) - Male;
  28.  
  29. int r = upper_bound(Male + 1 , Male + NumMale + 1 , Female[i] + k) - Male - 1;
  30.  
  31. if(l <= r )ans += r - l + 1;
  32. }
  33.  
  34. cout<<ans;
  35. }
  36. int main()
  37. {
  38. ios_base::sync_with_stdio(0);
  39. cin.tie(nullptr);
  40. cout.tie(nullptr);
  41. process();
  42. return 0;
  43. }
  44.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty