当前位置: 首页 > news >正文

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的元素的迭代器

#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;
}

http://www.lryc.cn/news/228954.html

相关文章:

  • Js 保留关键字
  • nodejs+vue+python+PHP+微信小程序-安卓-房产中介管理信息系统的设计与实现-计算机毕业设计
  • 【系统架构设计】架构核心知识: 3.5 Redis和ORM
  • linux时间同步
  • mysql++库connected与ping方法的区别
  • 拆位线段树 E. XOR on Segment
  • JVM及其垃圾回收机制(GC)
  • 友元的三种实现
  • 聊聊logback的DuplicateMessageFilter
  • WordPress 文档主题模板Red Line -v0.2.2
  • 网络和Linux网络_1(网络基础)网络概念+协议概念+网络通信原理
  • AI生成PPT工具——Gamma,结合GPT生成不错的效果
  • DcatAdmin使用模版文件时模板标签不生效
  • 【算法】算法题-20231114
  • 时序数据库 TDengine + 高级分析软件 Seeq,助力企业挖掘时序数据潜力
  • 【Rust 日报】2023-11-12 socketioxide
  • Redis快速入门(基础篇)
  • (三)七种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • SpringBoot--中间件技术-3:整合mongodb,整合ElasticSearch,附案例含代码(简单易懂)
  • matlab 二自由度操纵稳定性汽车模型
  • 超越任务调度的极致:初探分布式定时任务 XXL-JOB 分片广播
  • 设计模式-备忘录模式(Memento)
  • 【机器学习】正则化到底是什么?
  • Rust5.2 Generic Types, Traits, and Lifetimes
  • c 实用化的摄像头生成avi视频程序(加入精确的时间控制)
  • Web后端开发_01
  • 二十、泛型(6)
  • Java18新增特性
  • springboot容器
  • Windows 10 下使用Visual Studio 2017 编译CEF SDK