牛客IOI周赛20-普及组D夹缝中求和
夹缝中求和
https://ac.nowcoder.com/acm/contest/8997/D
//x<=ai+aj<=y;
//x-ai<=aj aj<=y-ai;
//a个数的排列方式对最后的答案不会产生影响,那么就可以对a个值排序枚举每个ai,二分处理答案
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll a, x, y;
cin >> a >> x >> y;
vector<ll>v(a);
for (int i = 0; i < a; i++) cin >> v[i];//读入
sort(v.begin(), v.end());//排序
ll ans = 0;
for(int i=0;i<v.size();i++)
{
int S = lower_bound(v.begin() + i + 1, v.end(), x - v[i])-v.begin();
int E = upper_bound(v.begin() + i + 1, v.end(), y - v[i]) - v.begin();
ans += (E - S);
}
cout << ans << endl;
return 0;
}</ll>