fork download
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. int lessthank(vector<int> &nums,int k){
  7. int i=0;
  8. int j = nums.size()-1;
  9. int ans=0;
  10.  
  11. while(i<j){
  12. int sum = nums[i]+nums[j];
  13.  
  14. if(sum<=k){
  15. ans = ans + (j-i);
  16. i++;
  17. }
  18. j--;
  19. }
  20.  
  21. return ans;
  22.  
  23. }
  24.  
  25. int main() {
  26. // your code goes here
  27. int n,l,r;
  28. cin>>n>>l>>r;
  29. vector<int> nums(n);
  30. for(int i=0;i<n;i++){
  31. cin>>nums[i];
  32. }
  33.  
  34. sort(nums.begin(),nums.end());
  35. int fr = lessthank(nums,r);
  36. int fl = lessthank(nums,l-1);
  37. cout<<fr-fl<<endl;
  38.  
  39.  
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0s 5324KB
stdin
7 3 6 
0 1 2 3 4 5 6
stdout
10