灵茶之二分02

灵茶之二分02

题目链接

https://codeforces.com/problemset/problem/1538/C

题目大意

输入 T(≤104) 表示 T 组数据。所有数据的 n 之和 ≤2e5。 每组数据输入 n(1≤n≤2e5) L R(1≤L≤R≤1e9) 和长为 n 的数组 a(1≤a[i]≤1e9)。 输出有多少对 (i, j) 满足 i < j 且 L <= a[i] + a[j] <= R。

代码

from bisect import bisect_left,bisect_right
for _ in range(int(input())):
    n,L,R = map(int,input().split())
    a = [*map(int,input().split())]
    a.sort()
    ans = 0
    for i,x in enumerate(a):
        ans += (bisect_right(a,R - x,0,i) - bisect_left(a,L - x,0,i))
    print(ans)
posted @ 2024-04-01 22:27  gebeng  阅读(12)  评论(0)    收藏  举报