当前位置:网站首页>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 mm transaction code vl04 create outbound delivery for sto
- Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;
- JS to determine whether the browser has opened the console
- Charles garbled code problem solving
- Debian10 LVM logical volumes
- MySQL - SQL execution process
- Debian10 configuring rsyslog+loganalyzer log server
- Pat class A - 1015 reversible primes
- Const defined variables and for of and for in in JS
- Day575: divide candy
猜你喜欢

SAP ui5 application development tutorial 102 - detailed trial version of print function implementation of SAP ui5 application

SQL programming task05 job -sql advanced processing

Day367: valid complete square
![Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;](/img/75/d2ad171d49611a6578faf2d390af29.jpg)
Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;

JS to paste pictures into web pages

Lexical Sign Sequence

E-R diagram

Autumn move script a
![[learning notes] roll back Mo team](/img/19/d374dd172b9609a3f57de50791b19e.png)
[learning notes] roll back Mo team

SAP ui5 application development tutorial 103 - how to consume third-party libraries in SAP ui5 applications
随机推荐
Dual cross domain: access allow origin header contains multiple values "*, *", but only one is allowed
Real topic of the 2020 Landbridge cup provincial competition - go square (dp/dfs)
leetcode 91. Decode ways (medium)
JS prevent the PC side from copying correct links
Vector 6 (inheritance)
Vscade personalization: let a cute girl knock the code with you
Xiaobai operates win10 to expand Disk C (allocate disk D memory to Disk C) and the test is valid for many times
You can also do NLP (classification)
SAP ui5 application development tutorial 103 - how to consume the trial version of the third-party library in SAP ui5 applications
Detailed explanation of clip attribute parameters
Overview of visual object detection technology based on deep learning
[22 summer reconstruction 1] codeworks round 791 (Div. 2)
Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;
SQL programming task03 job - more complex query
3D打印微组织
Similar to attention NLP
Pat class A - 1007 maximum subsequence sum
[Title Fine brushing] 2023 Hesai FPGA
Day575: divide candy
three. JS simulated driving tour art exhibition hall - creating super camera controller