当前位置:网站首页>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';
}
}
边栏推荐
- SAP ui5 application development tutorial 103 - how to consume the trial version of the third-party library in SAP ui5 applications
- 总体标准差和样本标准差
- Steps to implement a container global component
- Learn the specific technical learning experience of designing reusable software from the three levels of class, API and framework
- [hdu] p7058 ink on paper finding the maximum edge of the minimum spanning tree
- "Hearing" marketing value highlights, Himalaya ushers in a new situation
- three. JS simulated driving tour art exhibition hall - creating super camera controller
- Pat a - 1010 radical (thinking + two points)
- SAP ui5 application development tutorial 102 - detailed trial version of print function implementation of SAP ui5 application
- Extend your kubernetes API using the aggregation API
猜你喜欢

The devil cold rice # 099 the devil said to travel to the West; The nature of the boss; Answer the midlife crisis again; Specialty selection
![[learning notes] roll back Mo team](/img/19/d374dd172b9609a3f57de50791b19e.png)
[learning notes] roll back Mo team
![[sliding window] leetcode992 Subarrays with K Different Integers](/img/69/1ac0c54d33af0f7a9e3db3e82d076b.png)
[sliding window] leetcode992 Subarrays with K Different Integers

Ros2 summer school 2022 transfer-

Xiaobai operates win10 to expand Disk C (allocate disk D memory to Disk C) and the test is valid for many times

Read Amazon memorydb database based on redis

Sfod: passive domain adaptation and upgrade optimization, making the detection model easier to adapt to new data

On AI and its future trend | community essay solicitation

Lexical Sign Sequence

E-R diagram
随机推荐
Up the Strip
a++,++a,!,~
SQL programming task06 assignment - Autumn recruit secret script ABC
C#. Net universal database access encapsulation classes (access, sqlserver, Oracle)
LINQ 查詢
SQL programming task02 job - basic query and sorting
SQL programming task05 job -sql advanced processing
ERROR { err: YAMLException: end of the stream or a document separator is expected at line 6, colum
关于打算做一个web的问题看板,需要使用哪些方面语言及数据库知识!
Dual cross domain: access allow origin header contains multiple values "*, *", but only one is allowed
Debian10 configuring rsyslog+loganalyzer log server
Pat class A - 1015 reversible primes
Similar to attention NLP
Big guys, 2.2.1 the CDC monitoring SQLSEVER can only get the full amount of data. Why can't we get the incremental data in the later period
Pat class a 1016 phone bills (time difference)
C# SerializableDictionary序列化/反序列化
时间复杂度
Vector 3 (static member)
Day260: the number III that appears only once
Psychological analysis of the safest spot Silver