当前位置:网站首页>C. Number of Pairs-Codeforces Round #725 (Div. 3)
C. Number of Pairs-Codeforces Round #725 (Div. 3)
2022-06-23 01:36:00 【Qin Yu】
C. Number of Pairs
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given an array aa of nn integers. Find the number of pairs (i,j)(i,j) (1≤i<j≤n1≤i<j≤n) where the sum of ai+ajai+aj is greater than or equal to ll and less than or equal to rr (that is, l≤ai+aj≤rl≤ai+aj≤r).
For example, if n=3n=3, a=[5,1,2]a=[5,1,2], l=4l=4 and r=7r=7, then two pairs are suitable:
- i=1i=1 and j=2j=2 (4≤5+1≤74≤5+1≤7);
- i=1i=1 and j=3j=3 (4≤5+2≤74≤5+2≤7).
Input
The first line contains an integer tt (1≤t≤1041≤t≤104). Then tt test cases follow.
The first line of each test case contains three integers n,l,rn,l,r (1≤n≤2⋅1051≤n≤2⋅105, 1≤l≤r≤1091≤l≤r≤109) — the length of the array and the limits on the sum in the pair.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).
It is guaranteed that the sum of nn overall test cases does not exceed 2⋅1052⋅105.
Output
For each test case, output a single integer — the number of index pairs (i,j)(i,j) (i<ji<j), such that l≤ai+aj≤rl≤ai+aj≤r.
Example
input
Copy
4 3 4 7 5 1 2 5 5 8 5 1 2 4 3 4 100 1000 1 1 1 1 5 9 13 2 5 5 1 1
output
Copy
2 7 0 1
The title has a point that brings people to the pit , That's it i<j, People dare not sort , Actually, think about it carefully , After the sorting ,a And a There is only a size relationship , The original order relation does not need at all ; Assume that the previous original number is greater than the following , What's ahead j Just go , vice versa . Who does it i,j Are all the same , there i<j Just let you not repeat the choice (i,j)(j,i) nothing more .
After sorting , For each of these a[i], Want to find l-a[i] To r-a[i] The number of ,lower_bound Find the first one greater than or equal to l-a[i] The location of
upper_bound Find the first one greater than r-a[i] The location of ( The former must be within a reasonable range )
To prevent repetition , namely a[i] Can find l-a[i] And r-a[i] Number between , Then you can also find a[i], So from i+1 Start looking for .
#include<iostream>
# include<cstring>
# include<algorithm>
# include<math.h>
# include<cmath>
# include<map>
using namespace std;
typedef long long int ll;
ll a[100000*2+10];
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
ll l,r;
scanf("%d%lld%lld",&n,&l,&r);
for(int i=1; i<=n; i++)
{
scanf("%lld",&a[i]);
}
sort(a+1,a+1+n);
ll sum=0;
for(int i=1; i<=n; i++)
{
ll cnt1=lower_bound(a+i+1,a+1+n,l-a[i])-a;
ll cnt2=upper_bound(a+i+1,a+1+n,r-a[i])-a;
sum+=(cnt2-cnt1);
}
cout<<sum<<'\n';
}
}
边栏推荐
- C# SerializableDictionary序列化/反序列化
- Debian10 configuring rsyslog+loganalyzer log server
- Pat class A - 1007 maximum subsequence sum
- Thead safety experience
- [initial launch] there are too many requests at once, and the database is in danger
- Vector 6 (inheritance)
- [22 summer reconstruction 1] codeworks round 791 (Div. 2)
- LeetCode 206. 反转链表(迭代+递归)
- [hdu] p2087 cut cloth strip
- 層次選擇器
猜你喜欢

Ros2 summer school 2022 transfer-

How about precious metal spot silver?

Day367: valid complete square
![[launch] redis Series 2: data persistence to improve availability](/img/f4/5bc7ca3e17c6656e71df515182842e.png)
[launch] redis Series 2: data persistence to improve availability

Local deployment and problem solving of IIS in ArcGIS JS 4.23

3DMAX modeling notes (I): introducing 3DMAX and creating the first model Hello World

Autumn move script C

3D打印微组织

three. JS simulated driving tour art exhibition hall - creating super camera controller

Detailed explanation of clip attribute parameters
随机推荐
Extend your kubernetes API using the aggregation API
Module 8 job
OOP multiple storage (class template)
[template] KMP
Voice network multiplayer video recording and synthesis support offline re recording | Nuggets technology solicitation
Get the start and end dates of the current week
Error reported when compiling basalt
[hdu] p2087 cut cloth strip
Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;
ERROR { err: YAMLException: end of the stream or a document separator is expected at line 6, colum
Vector 3 (static member)
C#.NET万能数据库访问封装类(ACCESS、SQLServer、Oracle)
Sélecteur de hiérarchie
SQL programming task05 job -sql advanced processing
关于打算做一个web的问题看板,需要使用哪些方面语言及数据库知识!
SAP ui5 application development tutorial 102 - detailed explanation of the print function of SAP ui5 applications
Requête linq
人民币的单位的大写
Add / get and delete cookies
Pat class A - 1013 battle over cities