题目:
找出处于和r,l范围内的一对数的个数。
题解:
用lower_bound和upper_bound函数,【l,r】内对数等于【0,r】减去
【0,l-1】内对数个数。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int a[N];
int main()
{
int t; cin >> t; while (t--) {
int n, l, r;
cin >> n >> l >> r;
for (int i = 0; i < n; ++i)
{
cin >> a[i];
}
sort(a, a + n);
ll cnt = 0;
//[l, r] == [0, r] - [0, l - 1]
for (int i = 0; i < n; ++i)
{
int x = l - a[i];
int y = r - a[i];
int tx = lower_bound(a + i + 1, a + n, x) - a;
int ty = upper_bound(a + i + 1, a + n, y) - a;
cnt += ty - tx;
}
cout << cnt << endl;
}
return 0;
}