如图:二分思路==n是2e5

//二分的复杂度logn,常见和一个n的for循环结合成nlogn'
#include <bits/stdc++.h>
using namespace std;
int n,l,r;
const int N=2e5+3;
int a[N];
long long ans;
int main()
{
	// 优化 C++ 输入输出流,防止读入大数据时 TLE
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
  	cin>>n>>l>>r;
  	for(int i=1;i<=n;i++) cin>>a[i];
  	sort(a+1,a+1+n);
  //l<=ai+aj<<r=>aj>=l-ai&&aj<=r-ai
  for(int i=1;i<=n;i++){
  //	ans+=lower_bound(a+i+1,a+1+n,r-a[i])-lower_bound(a+i+1,a+1+n,l-a[i])+1;找<=时只能用uppper 
  		ans += upper_bound(a + i + 1, a + 1 + n, r - a[i]) - lower_bound(a + i + 1, a + 1 + n, l - a[i]);
  } 
  cout<<ans;
  return 0;
} 

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务