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 a� of n� integers. Find the number of pairs (i,j)(�,�) (1≤i<j≤n1≤�<�≤�) where the sum of ai+aj��+�� is greater than or equal to l� and less than or equal to r� (that is, l≤ai+aj≤r�≤��+��≤�).

For example, if n=3�=3, a=[5,1,2]�=[5,1,2], l=4�=4 and r=7�=7, then two pairs are suitable:

  • i=1�=1 and j=2�=2 (4≤5+1≤74≤5+1≤7);
  • i=1�=1 and j=3�=3 (4≤5+2≤74≤5+2≤7).

Input

The first line contains an integer t� (1≤t≤1041≤�≤104). Then t� test cases follow.

The first line of each test case contains three integers n,l,r�,�,� (1≤n≤2⋅1051≤�≤2⋅105, 1≤l≤r≤1091≤�≤�≤109) --- the length of the array and the limits on the sum in the pair.

The second line contains n� integers a1,a2,...,an�1,�2,...,�� (1≤ai≤1091≤��≤109).

It is guaranteed that the sum of n� 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�<�), such that l≤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

解题说明:此题是一道数学题,为了找到这样数字组合,可以先对数列进行排序,排序后,用lower_bound和upper_bound找到边界,然后中间的就都是满足条件的数字对。

lower_bound返回第一个大于等于val的元素的迭代器

upper_bound返回第一个大于val的元素的迭代器

cpp 复制代码
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		long long int n, l, r, k = 0;
		cin >> n >> l >> r;
		int a[200001];
		for (int i = 0; i < n; i++)
		{
			cin >> a[i];
		}
		sort(a, a + n);
		for (int i = 0; i < n; i++)
		{
			int j = lower_bound(a + i + 1, a + n, l - a[i]) - a;
			int p = upper_bound(a + i + 1, a + n, r - a[i]) - a;
			k = k + p - j;
		}
		cout << k << endl;
	}
	return 0;
}
相关推荐
Dxy1239310216几秒前
Python在图片上画矩形:从简单边框到复杂标注的全攻略
开发语言·python
white-persist4 分钟前
【vulhub shiro 漏洞复现】vulhub shiro CVE-2016-4437 Shiro反序列化漏洞复现详细分析解释
运维·服务器·网络·python·算法·安全·web安全
独自破碎E13 分钟前
面试官:你有用过Java的流式吗?比如说一个列表.stream这种,然后以流式去处理数据。
java·开发语言
꯭爿꯭巎꯭13 分钟前
python下载手机版(python3手机版(免费))
开发语言·python·智能手机
网域小星球22 分钟前
C++ 从 0 入门(六)|C++ 面试必知:运算符重载、异常处理、动态内存进阶(终极补充)
开发语言·c++·面试
FL162386312932 分钟前
基于C#winform部署软前景分割DAViD算法的onnx模型实现前景分割
开发语言·算法·c#
郭wes代码40 分钟前
大三Java课设:一行行敲出来的贪吃蛇,老师以为我是CV的
java·开发语言
John.Lewis1 小时前
C++进阶(12)附加学习:STL之空间配置器(了解)
开发语言·c++·笔记
独小乐1 小时前
019.ADC转换和子中断|千篇笔记实现嵌入式全栈/裸机篇
linux·c语言·驱动开发·笔记·嵌入式硬件·mcu·arm
baizhigangqw1 小时前
启发式算法WebApp实验室:从搜索策略到群体智能的能力进阶
算法·启发式算法·web app