题解:CF1538C Number of Pairs
CF1538C Number of Pairs 题解
题目传送门
描述
给定一个序列
思路
先考虑暴力,可以枚举每个
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define re register
#define code ios::sync_with_stdio(false);
#define by cin.tie(nullptr);
#define lzacode cout.tie(nullptr);
int a[210101];
signed main()
{
code by lzacode
int t;
cin >> t;
while (t--)
{
int n, x, y;
cin >> n >> x >> y;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
sort(a+1, a+1+n);
int ans=0;
for (int i = 1; i <= n; i++)
{
int l=i+1, r=n, ldex=1e9, rdex=-1e9;
while (l<=r)
{
int mid=(l+r)/2;
if (a[i]+a[mid]<x) l=mid+1;
else if (a[i]+a[mid]>y) r=mid-1;
else
{
ldex=mid;
r=mid-1;
}
}
l=i+1, r=n;
while (l<=r)
{
int mid=(l+r)/2;
if (a[i]+a[mid]<x) l=mid+1;
else if (a[i]+a[mid]>y) r=mid-1;
else
{
rdex=mid;
l=mid+1;
}
}
if (ldex==1e9&&rdex==-1e9) continue;
ans+=rdex-ldex+1;
}
cout << ans << endl;
}
return 0;
}
:::: ::::success[code 2]
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define re register
#define code ios::sync_with_stdio(false);
#define by cin.tie(nullptr);
#define lzacode cout.tie(nullptr);
int a[210101];
signed main()
{
code by lzacode
int t;
cin >> t;
while (t--)
{
int n, x, y;
cin >> n >> x >> y;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
sort(a+1, a+1+n);
a[n+1]=0x3f3f3f3f;
int ans=0;
for (int i = 1; i <= n; i++)
{
int ldex=lower_bound(a+i+1, a+n+2, x-a[i])-a;
int rdex=upper_bound(a+i+1, a+n+2, y-a[i])-a;
rdex--;
if (rdex>=ldex) ans+=rdex-ldex+1;
}
cout << ans << endl;
}
return 0;
}
::::