思路:这道题利用二分和不等式的性质。1<i<j<=n且L<=a[i] + a[j] <= R ==> L - a[i] <= a[j] <= R - a[i]。遍历找出大于等于L - a[i] 和 大于 R - a[i] 的区间,区间长度即为当前i对应的下标对数。所有对数累加即为满足条件的下标对数量。
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5+5;
ll a[N];
int main()
{
int n;cin >> n;
ll L, R;
cin >> L >> R;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a+1,a+1+n);
ll res = 0;
for (int i = 1;i <= n; i++)
{
ll l = L - a[i],r = R - a[i];
res += upper_bound(a+i+1,a+1+n,r) - lower_bound(a+i+1,a+1+n,l);
}
cout << res << '\n';
return 0;
}