We need to solve: for each test case, given array a of length n, count pairs (i<j) such that l ≤ a_i + a_j ≤ r.
Constraints: total n across tests ≤ 2e5, t up to 1e4. So O(n log n) per test is fine.
Standard two-pointer technique after sorting the array. For each i (0-indexed), find number of j > i such that a_i + a_j ∈ [l, r]. Sort array ascending. Then for each i from left to right, we can use binary search to find the range. But better: two pointers from both ends. However careful: for each i, we need to count j > i, not j>=i? Since pairs are unordered (i<j). So we can fix i, and find j>i. But if we use two-pointer approach that moves both, we need to count all pairs without double counting. A common method: After sorting, for each i, use lower_bound and upper_bound to find number of indices > i with a_j in [l - a_i, r - a_i]. Since array is sorted, we can binary search for left and right boundaries among indices i+1..n-1. Complexity O(n log n). With total n=2e5, that's fine.
Alternatively, we can use two pointers: Start with left=0, right=n-1, but we must count each pair exactly once. There is a known approach: count all pairs (i<j) with sum ≤ r, and subtract those with sum ≤ l-1. But careful: we need both lower and upper bound. So we can compute count of pairs with sum ≤ X (for X = r) and subtract count of pairs with sum ≤ l-1. So the problem reduces to counting pairs with sum ≤ S, where S is some integer. That can be done with two pointers: sort array, then for each i from left to right, find the largest j (with j > i) such that a_i + a_j ≤ S. However need to be careful to avoid double counting? Actually counting pairs with sum ≤ S can be done by two-pointer technique: Initialize left=0, right=n-1, and while left < right: if a[left] + a[right] ≤ S, then all pairs with that left and any right' from left+1 to right satisfy? No, the typical method: we can use two pointers moving from ends to count number of pairs (i<j) with sum ≤ S. Usually for counting pairs with sum ≤ S, we do: after sorting, we have two pointers i=0, j=n-1, count=0. While i<j: if a[i]+a[j] ≤ S, then all pairs (i, k) for k from i+1 to j will have sum ≤ S? Not necessarily because a[i] + a[k] could be larger than S if a[k] is large. Actually the common technique: for each i, we can find the largest j>i such that a[i] + a[j] ≤ S using binary search. That's O(n log n). But we can also use two-pointer scanning from left and right to count all pairs without binary search? There is a method: sort, then for each i, we maintain a pointer j that starts at n-1 and moves left as i increases, such that for each i we find the largest j>i with a_i + a_j ≤ S. Since the array is sorted, as i increases, the complementary value needed (S - a_i) decreases, so j can only move left. This gives O(n) time. So we can compute count_leq(S) in O(n). Then answer = count_leq(r) - count_leq(l-1). That is efficient.
Let's detail: countPairsLessOrEqual(S):
- sort a.
- initialize j = n-1
- ans = 0
- for i from 0 to n-1:
while (j > i) and (a[i] + a[j] > S):
j -= 1
// after while, j is the largest index such that a[i]+a[j] ≤ S, but j may become <= i.
if j > i:
ans += (j - i) // because for this i, all indices from i+1 to j inclusive satisfy sum ≤ S (since array sorted, a[i]+a[k] ≤ a[i]+a[j] ≤ S for k ≤ j? Wait: Since a[k] ≤ a[j] for k ≤ j, but a[i] + a[k] could be less than or equal to a[i] + a[j] ≤ S, yes because a[k] ≤ a[j] implies a[i]+a[k] ≤ a[i]+a[j] ≤ S. So indeed for all k between i+1 and j, sum ≤ S. But careful: this holds only because the array is sorted ascending and we have that for fixed i, as k increases, a[k] increases, so a[i]+a[k] is increasing. So for k ≤ j, a[i]+a[k] ≤ a[i]+a[j] ≤ S. So yes, all pairs (i, k) with i<k≤j are valid. So we can add (j-i). However we must ensure that j>i, and also that j is within bounds. So ans += max(0, j - i).
// then proceed: i increments, and we keep j as is (since for larger i, the required j may be smaller). But we must also ensure that j is never less than i+1? The while condition ensures j is adjusted. At the end of loop, j might be less than i? Actually after i increments, j might be < i? We need to maintain j ≥ i+1? The while loop condition j > i and a[i]+a[j] > S: we decrement j until condition fails or j <= i. But after that, if j <= i, then no pairs for current i. But for next i+1, we may need to possibly increase j? Not necessary because for larger i, the sum a[i]+a[j] with current j might be ≤ S? Actually as i increases, a[i] increases, so S - a[i] decreases, so the maximal j that satisfies a[i] + a[j] ≤ S cannot increase; it can only stay the same or decrease. So j should not need to move rightwards. So it's safe to keep j as is without resetting. However we must be careful that j might become less than i+1 after adjustments, and for next i, we might need to consider that j can be less than i+1; but we still use while loop to reduce further if needed, but we never increase j. That's correct. So algorithm works.
But we have to count pairs with i < j only. The loop adds (j-i) for each i, where j is the largest index > i with sum ≤ S. Summing these over i gives total number of pairs with sum ≤ S.
Double-check: Example: a = [1,2,5], sorted. S = 7. n=3. j start at 2. i=0: while (j>0 and 1+5>7? 6>7? false) so j remains 2. j>i => ans+=2-0=2 (pairs (0,1) and (0,2) both ≤7). i=1: while (j>1 and 2+5>7? 7>7? false because 7 is not >7) no decrement, j=2, j>i => ans+=2-1=1 => (1,2) sum=7 ≤7. total ans=3. That's all pairs, correct because all sums ≤7: (1+2=3,1+5=6,2+5=7). For S=6: i=0: while (j>0 and 1+5>6? 6>6? false) j=2, j>0 ans+=2 => adds (0,1) and (0,2) both ≤6? But (0,2) sum=6 ≤6, okay. i=1: while (j>1 and 2+5>6? 7>6 true) j-- -> j=1. Now j>i? 1>1 false, so ans unchanged. Total=2. Pairs: (0,1)=3, (0,2)=6, (1,2)=7 >6. So count=2 correct.
But wait: in S=6, when i=0, we added (0,2) which sum=6 is okay. However our logic for i=0 considered j=2 and added all k from i+1 to j. But is it guaranteed that a[0]+a[1] ≤ S? Yes, because a[1] ≤ a[2] and a[0]+a[2] ≤ S, so a[0]+a[1] ≤ a[0]+a[2] ≤ S. So fine.
Now for S=4: i=0: while (j>0 and 1+5>4? 6>4 true) j-- -> j=1; then (j>0 and 1+2>4? 3>4 false) stop j=1, j>0 ans+=1-0=1 (pair (0,1) sum=3 ≤4). i=1: while (j>1 and 2+2? Actually j=1 now, condition j>1 false. j>i? 1>1 false. So ans=1. That's correct: only pair (0,1) sum=3 ≤4. Works.
Edge cases: duplicates, zero? But a_i >=1 so fine.
Now for the original problem: count pairs with l ≤ sum ≤ r. So answer = countPairsLessOrEqual(r) - countPairsLessOrEqual(l-1). However careful: countPairsLessOrEqual counts pairs with sum ≤ S. So subtracting l-1 yields pairs with sum >= l. Because pairs with sum ≤ r minus those with sum ≤ l-1 gives pairs with sum in [l, r]. Yes.
But we must be careful about integer overflow: total pairs could be up to n*(n-1)/2 ≈ 2e5 * 2e5/2 = 2e10, which fits in 64-bit (2e10 < 9e18). So we need to use long long (int64).
Algorithm per test case:
- Read n, l, r.
- Read array a.
- Sort a.
- Define function count_leq(S) that returns number of pairs (i<j) with a_i + a_j ≤ S. If S < minimal sum? but it's fine.
Implementation:
long long count = 0;
int j = n-1;
for (int i = 0; i < n; i++) {
while (j > i && a[i] + a[j] > S) j--;
if (j > i) count += (j - i);
}
return count;
But careful: This algorithm assumes that a is sorted ascending. Let's test with sample: n=3, a=[5,1,2] sort -> [1,2,5], l=4, r=7. count_leq(7)=3, count_leq(3)=? pairs ≤3: only (1,2) sum=3 => count=1. So answer=3-1=2. Works.
Second sample: n=5, a=[5,1,2,4,3] sort -> [1,2,3,4,5], l=5, r=8. count_leq(8): compute. Let's simulate: n=5.
- i=0, a[0]=1, j start=4 (a[4]=5), while (j>0 && 1+5>8? 6>8 false) -> j=4, j>i -> count+=4-0=4 (pairs (0,1),(0,2),(0,3),(0,4)). All sums:1+2=3,1+3=4,1+4=5,1+5=6 ≤8.
- i=1, a[1]=2, while (j>1 && 2+5>8? 7>8 false) j=4, j>1 -> count+=4-1=3 (pairs (1,2)=5, (1,3)=6, (1,4)=7) -> all ≤8.
- i=2, a[2]=3, while (j>2 && 3+5>8? 8>8 false) j=4, j>2 -> count+=4-2=2 (pairs (2,3)=7, (2,4)=8)
- i=3, a[3]=4, while (j>3 && 4+5>8?9>8 true) j-- -> j=3; condition now j>3? false. j>i? 3>3 false -> no addition.
- i=4 (skip because i < n but j will be 3, while loop may not run but we check j>i? false)
Total count_leq(8)=4+3+2=9.
count_leq(4): we compute.
i=0: j=4; while (1+5>4? 6>4 true) j=3; while (1+4>4?5>4 true) j=2; while (1+3>4?4>4 false) stop j=2; j>0 => count+=2-0=2 (pairs (0,1)=3, (0,2)=4). Actually careful: (0,2) sum=4 which is ≤4, so counts. But pair (0,2) is index 2, so j=2 means indices i+1..2: (0,1) and (0,2). Good.
i=1: while (j>1 && 2+ a[j] >4). j currently=2; check 2+3>4?5>4 true, so j-- => j=1; now j>1 false; so no addition. So count_leq(4)=2.
Thus answer = 9-2 =7. Which matches sample output.
Third sample: n=4, a=[1,1,1,1], l=100, r=1000. All sums =2, so none. count_leq(1000) = total pairs = 6. count_leq(99) = 6? Actually pairs sum =2 ≤99 so count_leq(99) =6, so answer=0. Yes.
Fourth sample: n=5, a=[2,5,5,1,1] sort -> [1,1,2,5,5]; l=9, r=13. Let's compute manually: pairs:
1+1=2, 1+2=3, 1+5=6, 1+5=6 (two 1s with each 5: total 4 sums of 6? Actually there are two 1s and two 5s, so pairs: (1,1) sum=2, (1,2) indices? Actually better to enumerate distinct indices after sorting: values: v0=1, v1=1, v2=2, v3=5, v4=5. All unordered pairs (i<j):
(0,1):2
(0,2):3
(0,3):6
(0,4):6
(1,2):3
(1,3):6
(1,4):6
(2,3):7
(2,4):7
(3,4):10
So sums within [9,13] only (3,4)=10. So answer=1. Now count_leq(13)=? Count all pairs sum ≤13: all 10 pairs (since max sum=10) so 10. count_leq(8)=? pairs with sum ≤8: exclude (3,4)=10? Actually (3,4)=10 >8, also (2,3)=7 ≤8, (2,4)=7 ≤8, rest ≤6. So pairs ≤8: total pairs except (3,4): 9 pairs. So answer=10-9=1. Good.
Thus algorithm works.
But wait, there might be a subtle bug: In count_leq, we assume that for fixed i, the j found is the largest index such that a[i]+a[j] ≤ S, and then we add all k from i+1 to j. This is correct because array sorted ascending, so a[i]+a[k] ≤ a[i]+a[j] for k≤j. However this relies on the fact that the array is sorted in non-decreasing order. Yes.
We should also ensure that j is correctly maintained across i. The while condition uses a[i] + a[j] > S to decrement j. But what about when a[i] + a[j] <= S? Then we don't decrement; j may be too high? Actually it's the maximum possible j because if a[i] + a[j] <= S, then j could be larger? No, since j is initially n-1 and we only decrement, j can't be larger than the true maximum. But could it be smaller than the true maximum because we overshoot from previous i? Let's think: At i=0, we set j to n-1 and decrement until condition fails, so j becomes the maximum index with a[0]+a[j] ≤ S. For i=1, we start with that j (which could be > maximum for i=1? Actually for i=1, the maximum index j_max1 such that a[1]+a[j] ≤ S may be smaller than j_max0 because a[1] ≥ a[0]. So j_max1 ≤ j_max0. Our current j from previous step is ≤ j_max0. But is it possible that j is less than j_max1? Could j have been decremented too much due to earlier i? Suppose at i=0, j ended up at some value j0. At i=1, the true j_max1 might be larger than j0? Since a[1] ≥ a[0], the sum a[1]+a[j] for a given j is ≥ a[0]+a[j]. Actually if a[1] ≥ a[0], then for a fixed j, a[1]+a[j] ≥ a[0]+a[j]. So if a[0]+a[j] > S, it's possible that a[1]+a[j] > S as well. But could it be that a[0]+a[j] > S (so we decremented j to a smaller value), but for i=1, a[1]+a[j] might be ≤ S for that same j? Since a[1] is larger, a[1]+a[j] >= a[0]+a[j]. So if a[0]+a[j] > S, then a[1]+a[j] > S even more. So if j was too low for i=0, it will also be too low for i=1? Wait "too low" means j is smaller than the maximum possible for i=0? Actually our while loop at i=0 ensures that after decrementing, a[0]+a[j] ≤ S. But it's possible that j is not the maximum; we only decrement while sum > S. So when we exit, we have a[0]+a[j] ≤ S, but we might have decremented one time too many? Let's simulate: start j = n-1. While (j > i && a[i]+a[j] > S) j--. So exit when either j == i or a[i]+a[j] ≤ S. So j is the largest index (starting from n-1 moving down) such that a[i]+a[j] ≤ S? Actually if we decrement until condition fails, we end up at the first index (from high to low) that satisfies ≤ S. However we started from the highest index and decremented each time the sum was > S. So after exiting, a[i]+a[j] ≤ S, and also either j == i or a[i]+a[j+1] > S (if j+1 <= n-1). Because we stopped decrementing when condition false; the previous value (j+1) would have been > S. So indeed j is the maximum index such that a[i]+a[j] ≤ S, provided we started at n-1. But what if we overshoot? No, the loop moves j down step by step, so it finds the highest j (starting from top) that satisfies ≤ S. So j is indeed the maximum.
Now for i=1, we keep j as is. But before entering the while loop for i=1, we have j which is the maximum index for i=0. We need to ensure that for i=1, after possibly decrementing further, we get the maximum index for i=1. Since j_max1 ≤ j_max0, and current j ≤ j_max0. If current j > j_max1, then a[1]+a[j] > S? Actually if j > j_max1, then by definition of j_max1, a[1]+a[j] > S. So while loop condition (j > i && a[i] + a[j] > S) will be true, so we will decrement j until either j == i or a[1]+a[j] ≤ S. This will bring j down to j_max1. If current j is already ≤ j_max1, then a[1]+a[j] ≤ S? Not necessarily: if j < j_max1, then it's possible that a[1]+a[j] ≤ S (since for lower j sums are smaller) but j is not maximum; we might miss some pairs? But can j ever be less than j_max1? The only way is if from previous i we decremented j below j_max1. But could that happen? For i=0 we set j = j_max0. For i=1, we may not decrement if a[1]+a[j] ≤ S. But if j < j_max1, then a[1]+a[j] ≤ S but there exists larger j (up to j_max1) that also satisfy, but we haven't considered them because j is lower. However is it possible that j_max0 > j_max1, and we decreased j during i=0 loop to j_max0. But then for i=1, j is still j_max0. Since j_max0 is >= j_max1? Actually j_max0 is the maximum for i=0; for i=1, the maximum j_max1 is ≤ j_max0. So j_max0 >= j_max1. So after i=0, j = j_max0, which is >= j_max1. Then at i=1, we run the while loop: while (j > i && a[1]+a[j] > S) j--. Since j = j_max0, and j_max0 might be > j_max1, then a[1]+a[j_max0] > S? By definition of j_max1, if j_max0 > j_max1, then j_max0 is not allowed for i=1, so a[1]+a[j_max0] > S. So the while condition is true, so we decrement j until a[1]+a[j] ≤ S. This will bring j down to j_max1. So after the loop, j = j_max1 (or possibly lower if we overshoot? But we decrement until condition false; if we overshoot, we would have stopped when condition becomes false; that means we found the first (highest) index where sum ≤ S, which is exactly j_max1. So we get j_max1). So j ends up exactly at j_max1. Thus it's fine.
But what if j_max0 = j_max1? Then j remains same, and while condition is false because a[1]+a[j] ≤ S. So no decrement, j stays j_max1, correct.
Now, could j ever become less than i+1 before we process i? At start, j=n-1. For i large, j might become ≤ i. Then while loop condition j > i will be false, so no decrement, and then we check j > i? false, so we add 0. But we must ensure that we don't attempt to access a[j] when j == i? Actually while loop condition includes j > i, so when j == i, we don't enter; also we check a[i] + a[j] only if j > i. So safe. However, after i increments, if j < i+1, we still have while loop condition j > i? false. So no addition. That's correct.
Thus the algorithm is correct.
Now we must consider that l could be 1 (minimum). l-1 = 0. But S could be 0. In count_leq(0), because all a_i ≥ 1, no pair sum ≤ 0. So count=0. That's fine.
Now implement.
But careful: The sum of n across test cases ≤ 2e5, so sorting each test case individually is O(n log n) total O(N log N) where N=2e5. That's fine.
We should also handle the case when l > r? It's given l ≤ r always.
Edge cases: n=1 => no pairs, answer=0. count_leq will return 0 because for loop i from 0 to 0, j=n-1=0, while loop condition j>i? 0>0 false, j>i? false, so count=0.
Now code. We'll define function count_leq(vector& a, int S). We'll pass sorted a. Actually we can sort in place.
Pseudo:
long long count_pairs_le(vector& a, int S) {
long long cnt = 0;
int j = (int)a.size() - 1;
for (int i = 0; i < (int)a.size(); i++) {
while (j > i && a[i] + a[j] > S) j--;
if (j > i) cnt += (j - i);
}
return cnt;
}
But careful: The function modifies j that moves leftwards, but we need to reset j for each test case. We'll create a fresh function call each time.
We then compute ans = count_pairs_le(a, r) - count_pairs_le(a, l-1).
But wait: What if l-1 negative? Actually l ≥ 1, so l-1 ≥ 0. So it's okay.
But there is a nuance: When S is less than smallest possible sum (like 0), the while loop might start with j = n-1, and a[i] + a[j] > S (since all sums positive), but the while condition will be true as long as j>i, so j will be decremented until j == i, then loop stops. Then for each i, j will eventually become i, but we might overshoot for subsequent i? Let's test: For S = 0, n=3, sorted a = [1,2,5]. i=0: j=2, while (2>0 && 1+5>0) true -> j=1; while (1>0 && 1+2>0) true -> j=0; while (0>0?) false. Then j=0, j>i? 0>0 false, cnt unchanged. i=1: now j=0, while (j>i? 0>1 false) so no change, j>i false. i=2: j=0, 0>2 false. So cnt=0. Works.
Now time complexity O(n) per call. But we call twice per test case, so O(2n) after sorting. Overall O(n log n).
Now note: The input constraints for t up to 1e4, but total n sum ≤2e5. So many small test cases. Our algorithm per test case uses O(n log n) sorting, plus O(n) for two counts. So total sorting overhead: sum over tests of n_i log n_i. Since sum n_i = 2e5, worst-case when many small test cases: sum n_i log n_i ≤ sum n_i * log(max n_i) maybe but careful: For each test case, n_i could be small, but log n_i is small. So overall fine.
Now we should output answer per test case.
Let's test with some random examples.
Consider n=2, a=[1,2], l=3, r=3: pairs (0,1) sum=3 => count=1. Our algorithm: sorted [1,2], count_leq(3)=? i=0: j=1, while (1>0 && 1+2>3? 3>3 false) j=1, j>i => cnt+=1-0=1; total=1. count_leq(2)=? i=0: while (1>0 && 1+2>2? 3>2 true) j-- => j=0; j>i false; cnt=0. Answer=1-0=1.
n=2, a=[1,2], l=2, r=2: count_leq(2)=0, count_leq(1)=0, answer=0. Actually sum=3, not in [2,2]. So 0 correct.
Now test with array containing duplicates, large numbers, etc.
Now we should consider the possibility that the two-pointer counting method might have off-by-one issues. Let's verify with more examples.
Consider a = [1,1,1,1] (n=4). S=2. Count pairs with sum ≤2: All pairs sum=2, so total pairs =6. Let's simulate:
i=0: j=3; while (j>0 && 1+1>2? 2>2 false) so j=3, j>0 => cnt+=3-0=3 (pairs (0,1),(0,2),(0,3)). All sums=2 ≤2.
i=1: while (j>1 && 1+1>2? 2>2 false) so j=3, j>1 => cnt+=3-1=2 (pairs (1,2),(1,3)).
i=2: while (j>2 && 1+1>2? false) j=3, j>2 => cnt+=3-2=1 (pair (2,3)).
i=3: j=3, while (j>3? false) j>i false. Total=6. Works.
Consider S=1: All sums =2 >1, so count=0. Simulate: i=0: j=3; while (j>0 && 1+1>1? 2>1 true) j-- -> j=2; while (2>0 && 1+1>1? 2>1 true) j=1; while (1>0 && 1+1>1? 2>1 true) j=0; now j=0, j>i false, cnt unchanged. i=1: j=0; while (j>1? false) j>i false. etc. cnt=0.
Now consider a = [1,2,3,4,5] n=5, S=5. Pairs ≤5: (1,2)=3, (1,3)=4, (1,4)=5, (1,5)=6 no, (2,3)=5, (2,4)=6 no, (2,5)=7, (3,4)=7, (3,5)=8, (4,5)=9. So valid: (1,2),(1,3),(1,4),(2,3) => 4 pairs. Let's simulate algorithm:
sorted = [1,2,3,4,5].
i=0, a[0]=1, j=4; while (4>0 && 1+5>5? 6>5 true) j=3; while (3>0 && 1+4>5? 5>5 false) stop j=3. j>0 => cnt+=3-0=3 (pairs (0,1),(0,2),(0,3) i.e., 1+2,1+3,1+4) -> sums 3,4,5 all ≤5. Good.
i=1, a[1]=2, j=3; while (3>1 && 2+4>5? 6>5 true) j=2; while (2>1 && 2+3>5? 5>5 false) stop j=2. j>1 => cnt+=2-1=1 (pair (1,2) =2+3=5). So total 4. Works.
Now consider S=4. Valid pairs: (1,2)=3, (1,3)=4 -> 2 pairs. Simulate: i=0: j=4; while (4>0 && 1+5>4? 6>4 true) j=3; while (3>0 && 1+4>4?5>4 true) j=2; while (2>0 && 1+3>4?4>4 false) stop j=2; j>0 cnt+=2-0=2 (pairs (0,1),(0,2) sums 3 and 4) correct. i=1: j=2; while (2>1 && 2+3>4?5>4 true) j=1; now j>1? false -> cnt unchanged. total=2. Good.
Now consider S=3. Valid: (1,2)=3 only. i=0: j=4; while (1+5>3 true) j=3; while (1+4>3 true) j=2; while (1+3>3?4>3 true) j=1; while (1>0? and 1+2>3?3>3 false?) Actually after j=1, condition: while (j>0 && 1+2>3? 3>3 false) so stop j=1. j>0 => cnt+=1-0=1 (pair (0,1) sum=3) correct. i=1: j=1; while (j>1? false) j>i? 1>1 false. So total=1.
All good.
Now potential pitfalls: The integer S may be up to 1e9, a_i up to 1e9, sum may overflow 32-bit but we use 64-bit for addition (a_i are ints but sum could be up to 2e9, which fits in 32-bit signed? Actually 2e9 fits in 32-bit signed (max 2,147,483,647). So no overflow for int. However for counting we need long long.
Now implementation details: We'll read t. Loop test cases. For each: read n, l, r. Read vector a of size n. Sort a. Compute cnt_r = count_leq(r). Compute cnt_l = (l > 1 ? count_leq(l-1) : 0) but l≥1, l-1>=0, so we can compute count_leq(l-1). Then output cnt_r - cnt_l.
But be careful: count_leq function expects sorted a. Should we sort before calling? Yes.
Now, can we reuse the same function for both calls without sorting again? Yes, we sort once.
Now test on provided examples.
Also consider large values: n=2e5, sorting okay.
Now, is there any alternative method using binary search? Possibly simpler but O(n log n) as well. However our two-pointer approach for count_leq is O(n), which is slightly more efficient.
But note: We need to call count_leq twice. Could we combine into one pass? Possibly not needed.
Now confirm with edge case: l = 1, r = 1e9. count_leq(r) will count all pairs because max sum could be up to 2e9, but r=1e9 may be less than some sums. Actually if r is large enough to include all possible sums, then count_leq(r) = n*(n-1)/2. But we also compute count_leq(l-1) which is 0 because l=1 => l-1=0. So answer = total pairs.
Now, what about r smaller than minimal sum? Then count_leq(r) may be 0. And maybe l-1 smaller than r? answer 0.
Now, ensure we use fast I/O because t up to 1e4 and total input size large. Use ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); and use '\n'.
Now finalize.
But we must be careful: The function count_leq uses a loop for i from 0 to n-1, and while loop decrements j. The condition j > i is important: we should not allow j to become less than i. Also we need to avoid out-of-bounds access when using a[j] in condition. The while condition checks j > i before accessing a[j]. That's safe.
Now, what about if we used a different approach: For each i, we could use binary search to find the upper bound. But the two-pointer is more elegant.
But wait, there is a subtlety: The two-pointer method described is typically used for counting pairs with sum <= S, but careful about inclusion of pairs (i, j) with i < j. The code as given adds (j - i) for each i, which counts exactly all j > i up to j. This counts each pair exactly once (since each pair (i,j) is counted when i is the smaller index, and j is the largest index satisfying condition for that i, but the pair may be counted only if j is >= j? Actually if the pair (i,j) satisfies a_i + a_j ≤ S, then for that i, j will be at least as large as the actual j? Let's examine: For fixed i, we define j_max as the largest index > i such that a_i + a_j_max ≤ S. Then for any j in (i, j_max], a_i + a_j ≤ S. So the code adds all these j. So if a_i + a_j ≤ S, then j ≤ j_max, and it's counted. So correct.
Now verify for a case where there are duplicates and the two-pointer might count incorrectly due to j moving too far left? Already tested.
Thus answer = count_leq(r) - count_leq(l-1).
Now, one more test: n=5, a=[2,3,3,5,6], l=6, r=8. Sorted same. Pairs:
(2,3)=5 (no)
(2,3)=5 (no)
(2,5)=7 (yes)
(2,6)=8 (yes)
(3,3)=6 (yes)
(3,5)=8 (yes)
(3,6)=9 (no)
(3,5)=8 (yes? Actually indices: let's list sorted values: [2,3,3,5,6]
Pairs: (0,1)=5, (0,2)=5, (0,3)=7, (0,4)=8, (1,2)=6, (1,3)=8, (1,4)=9, (2,3)=8, (2,4)=9, (3,4)=11. So within [6,8]: (0,3)=7, (0,4)=8, (1,2)=6, (1,3)=8, (2,3)=8. That's 5 pairs. Let's compute count_leq(8) and count_leq(5).
count_leq(8):
i=0: j=4; while (2+6>8? 8>8 false) j=4; cnt+=4-0=4 (pairs (0,1)=5, (0,2)=5, (0,3)=7, (0,4)=8) all ≤8.
i=1: a[1]=3, j=4; while (j>1 && 3+6>8?9>8 true) j=3; while (j>1 && 3+5>8?8>8 false) stop j=3; cnt+=3-1=2 (pairs (1,2)=6, (1,3)=8) both ≤8.
i=2: a[2]=3, j=3; while (j>2 && 3+5>8?8>8 false) j=3; cnt+=3-2=1 (pair (2,3)=8) ≤8.
i=3: j=3; while (j>3 false) j>i? 3>3 false; cnt unchanged.
total = 4+2+1 =7. So count_leq(8)=7. Which pairs? All 10 pairs? Actually we counted 7 pairs. List all pairs with sum ≤8: from earlier list, pairs with sum ≤8 are: (0,1)=5, (0,2)=5, (0,3)=7, (0,4)=8, (1,2)=6, (1,3)=8, (2,3)=8. That's exactly 7. Good.
count_leq(5):
i=0: j=4; while (2+6>5?8>5 true) j=3; while (2+5>5?7>5 true) j=2; while (2+3>5?5>5 false) stop j=2; cnt+=2-0=2 (pairs (0,1)=5, (0,2)=5) both ≤5.
i=1: a[1]=3, j=2; while (j>1 && 3+3>5?6>5 true) j=1; now j>1? false; cnt unchanged.
i=2: j=1; while (j>2? false); j>i?1>2 false. total=2. So count_leq(5)=2. Then answer=7-2=5, matches.
Thus algorithm correct.
Now, one more corner: The array can have values up to 1e9, l and r up to 1e9, but sums up to 2e9. Our addition in while condition is within 32-bit signed int? 2e9 < 2^31-1 (2,147,483,647). So it's safe for int if using 32-bit. However some programming languages like Java may need long for sum to avoid overflow? Actually 1e9+1e9=2e9, which fits in 32-bit signed int. But to be safe, we can use long long for sum, but not necessary. I'll use long long to avoid any chance of overflow if a_i can be 10^9 and sum 2*10^9 fits in signed 32-bit? Actually signed 32-bit max is 2,147,483,647 > 2,000,000,000, so okay. But some languages like C++ int is at least 32 bits, typical 32-bit with max 2,147,483,647. So fine.
But total count of pairs may be up to ~2e10, need long long for answer.
Now complexity: Sorting O(n log n). Two-pointer each O(n). So fine.
Now one more consideration: Could there be any off-by-one in count_leq for S that is less than a[i] + a[i+1] for all i? Already tested.
Now we must implement count_leq to work for empty array? n could be 1, we handle.
Now I'll write solution in C++.
Potential pitfalls: The two-pointer algorithm must have j starting at n-1 each time we call count_leq, because j gets modified. So inside count_leq, we set j = n-1 at start.
Now ensure we pass vector by reference to avoid copy. But not necessary for performance? but fine.
Now confirm with sample input:
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
Our algorithm gives:
Test1: 2
Test2: 7
Test3: 0
Test4: 1
Matches.
Now finalize.
But wait, are there any constraints that might cause the two-pointer count_leq algorithm to be wrong if we use while (j > i && a[i] + a[j] > S) j--;? Let's test with array that has negative numbers? a_i are positive (1 ≤ a_i). So it's fine.
Now let's think about alternative approach: Binary search for each i. That is simpler to implement and less error-prone, and O(n log n) is acceptable. But two-pointer is O(n) after sorting, which is slightly faster. However with total n=2e5, n log n is fine. But implementing two-pointer might be simpler? Actually both are simple. But I'll stick with two-pointer.
However, one nuance: In count_leq, we use condition while (j > i && a[i] + a[j] > S). This works as long as a[i] + a[j] is monotonic with j. But what if there are duplicates? It's still monotonic. So fine.
But we must ensure that for each i, after decrementing, j is >= i+1? Not necessarily, but that's handled.
Now let's test with a random large array in mind.
But wait: The two-pointer approach works because we are counting pairs with sum ≤ S. However, there is a known technique: For each i, we can use upper_bound to find the largest j such that a[i]+a[j] ≤ S, among indices > i. That yields same result. The two-pointer is essentially the same but with a moving pointer.
Now I'll code.
Implementation plan:
- include bits/stdc++.h
- using namespace std;
- typedef long long ll;
ll count_leq(const vector& a, int S) {
int n = a.size();
int j = n-1;
ll cnt = 0;
for (int i = 0; i < n; i++) {
while (j > i && a[i] + a[j] > S) {
j--;
}
if (j > i) {
cnt += (j - i);
}
}
return cnt;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n, l, r;
cin >> n >> l >> r;
vector a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
ll ans = count_leq(a, r) - count_leq(a, l-1);
cout << ans << '\n';
}
return 0;
}
But wait: When we call count_leq(a, l-1) we pass l-1. If l == 0? But l >=1, so fine.
Now test with sample.
Now think about performance: For each test case, we sort a. But we call count_leq twice, each resets j to n-1. In second call, we need to reset j to n-1 again. That's fine.
Now some might argue that we could compute both counts in one pass but not needed.
Now, potential bug: If l is 1, then l-1 = 0, count_leq(0) works as expected (all sums >0 so 0). So answer = count_leq(r). If r is also 0 (not possible because l ≤ r and l≥1 so r>=1), but if l=0? not allowed.
Now confirm that the two-pointer algorithm might produce a wrong result if there are many elements and j decrements too much and never resets. But as argued, it's correct.
Now let's test with another case: a = [10,20,30,40,50], l=60, r=80. Sorted. pairs with sum in [60,80]: (10,50)=60, (10,40=50 no), (10,30=40 no), (10,20=30 no), (20,50=70), (20,40=60), (20,30=50 no), (30,50=80), (30,40=70), (40,50=90). So valid: (10,50)=60, (20,50)=70, (20,40)=60, (30,50)=80, (30,40)=70 => 5 pairs. Count_leq(80) - count_leq(59).
count_leq(80): i=0: j=4; while(10+50>80? 60>80 false) j=4, cnt+=4-0=4 (pairs (0,1)=30, (0,2)=40, (0,3)=50, (0,4)=60) but (0,4) sum=60 ≤80, all ≤80. Actually (0,1)=30, (0,2)=40, (0,3)=50, (0,4)=60 all ≤80. So adds 4.
i=1: a[1]=20, j=4; while(20+50>80?70>80 false) j=4, cnt+=4-1=3 (pairs (1,2)=50, (1,3)=60, (1,4)=70) all ≤80. (1,4)=70 ≤80, yes.
i=2: a[2]=30, j=4; while(30+50>80?80>80 false) j=4, cnt+=4-2=2 (pairs (2,3)=70, (2,4)=80) both ≤80.
i=3: a[3]=40, j=4; while(40+50>80?90>80 true) j=3; j>3? false, no addition.
i=4: no.
Total=4+3+2=9. But total pairs with sum ≤80? Actually all pairs? Let's list all pairs (i<j):
(10,20)=30
(10,30)=40
(10,40)=50
(10,50)=60
(20,30)=50
(20,40)=60
(20,50)=70
(30,40)=70
(30,50)=80
(40,50)=90
Those ≤80: all except (40,50)=90. So 9 pairs. So count_leq(80)=9.
Now count_leq(59):
i=0: j=4; while(10+50>59?60>59 true) j=3; while(10+40>59?50>59 false) stop j=3; cnt+=3-0=3 (pairs (0,1)=30, (0,2)=40, (0,3)=50) all ≤59? (0,3)=50 ≤59, yes. So cnt=3.
i=1: a[1]=20, j=3; while(j>1 && 20+40>59?60>59 true) j=2; while(20+30>59?50>59 false) stop j=2; cnt+=2-1=1 (pair (1,2)=50) ≤59.
i=2: a[2]=30, j=2; while(j>2 false) j>i? 2>2 false. So cnt=4.
i=3: j=2, j>i false, etc. So count_leq(59)=4. Wait earlier we computed count_leq(59) as 4? Let's list pairs ≤59: (10,20)=30, (10,30)=40, (10,40)=50, (20,30)=50. That's 4. So correct. Then ans=9-4=5, matches expected.
So algorithm works.
Now consider a test case where l and r are such that l is very small, r is very large: count_leq(r) might be total pairs; count_leq(l-1) might be zero; correct.
Now consider a test where l and r are the same and there are multiple pairs equal to that sum. Works.
Now confirm with extremes: n=200000, all values = 10^9. Then sum = 2e9. If l=1, r=2e9, count_leq(r) = n*(n-1)/2 (since all sums = 2e9 ≤ 2e9). That is 19999900000? Actually approx 2e5^2/2 = 2e10. Fits in 64-bit. count_leq(0)=0. So answer = n*(n-1)/2.
Our count_leq for S=2e9: i=0: j=n-1; while (j>i && 1e9+1e9 > 2e9? 2e9 > 2e9? false) because 2e9 is not greater than 2e9. So j remains n-1. cnt += (n-1 - 0) = n-1. i=1: j still n-1; while (j>1 && 1e9+1e9 > 2e9? false) so cnt += (n-1 - 1) = n-2. ... So total sum = (n-1)+(n-2)+...+0 = n*(n-1)/2. Works.
Now if S is slightly less than 2e9, say 1999999999, then while condition may be true because 2e9 > 1999999999. Then j will decrement until a[i]+a[j] <= S. Since all a[i]=1e9, we need a[i]+a[j] <= S, that means j must be such that 1e9+1e9 <= S? Actually since all values same, for any j, sum=2e9 > S, so while loop will keep decrementing j until j == i? Actually for i=0, we start j=n-1, condition 2e9 > S true, so j-- repeatedly until j==0? But wait, the condition checks j > i and a[i]+a[j] > S. Since a[i]+a[j] = 2e9 > S for all j>i. So the loop will run until j becomes i. So j ends at i. Then cnt adds 0. For i=1, j currently = i from previous step? Actually after i=0, j becomes 0. For i=1, we have j=0, and while (j>i? 0>1 false) so no change, cnt adds 0. So total cnt=0. That's correct because no pair has sum ≤ S (since all sums =2e9 > S). Good.
Now what about potential integer overflow in a[i]+a[j] if using int? 1e9+1e9=2e9 fits. So fine.
Thus solution is ready.
Now I think about an alternative: use binary search. The two-pointer method may be tricky for some because it requires careful pointer movement. However, I'm comfortable with it.
But I recall that some Codeforces problems (like "Pairs" or "Count the Pairs") use exactly this approach. In particular, the problem "Pairs" (CF 1538C?) is similar. So it's standard.
Now double-check the function signature: count_leq uses const vector& a. Should we pass the vector as reference? Yes.
Now what about the case where n=0? Not possible (n≥1).
Now we should test with random small arrays by brute force to verify. I can do mental but can be omitted.
Now possible nuance: The input says "Find the number of pairs (i, j) (1 ≤ i < j ≤ n)" but array indexing is 1-based in problem statement. However we treat indices 0-based; counting pairs by values only.
Now finalize answer.
But wait, there is a potential optimization: Since we have to call count_leq twice, we could compute both using one two-pointer scan? Possibly compute prefix sums? But not needed.
Now we need to make sure we use long long for count in function and output. Implementation in C++.
Now there is also an alternative: using binary search for each i: for i, lower_bound for left = l - a[i] and right = r - a[i], and then count number of elements in [left, right] among indices > i. But that also works and might be simpler conceptually, but O(n log n) is fine. However the two-pointer method reduces constant factor. But we must ensure that count_leq is called with S, and that we subtract correctly.
Now I'll produce the final solution.
But before finalizing, I'll think about any possible pitfalls with the two-pointer for count_leq when S is less than a[i] + a[i+1] for all i, the while loop might set j = i for each i? Actually if S < a[i] + a[i+1] for all i, then for i=0, j will be decremented until j == i (since all sums > S), so j ends at 0. Then for i=1, j=0 (which is < i+1). Condition while (j > i) false, so j stays 0, and then cnt adds 0. So total 0. Correct.
Now when S is very large, j remains n-1 for all i, so cnt adds (n-1 - i) for each i, sum = n*(n-1)/2.
Now, it's important to reset j = n-1 for each call. In our function, we set j = n-1 at start, so fine.
Now think about memory: vector of size n.
Now one more check: The problem says sum of n over all test cases ≤ 2e5. So we can declare vectors each test case and they will be freed.
Now, are there any tricky cases where l and r are such that l > r? Not allowed.
Now, implement in code.
Now I'll write final answer.
But wait, there is a nuance: The two-pointer count_leq function counts pairs (i<j) with sum ≤ S. But what if S is negative? l-1 could be 0, not negative. So fine.
Now, it's important to note that we are using int for a_i and for indices. The sum a[i] + a[j] may be up to 2e9, which fits in int (signed 32-bit). But if we use unsigned int, also fine. But some compilers might treat int as 16-bit? No, typical 32-bit. So safe.
Now, I'd like to confirm with another random test via brute force (simulate mentally small array):
n=4, a=[3,1,4,2] l=3 r=6. Sorted: [1,2,3,4]. Pairs:
(1,2)=3; (1,3)=4; (1,4)=5; (2,3)=5; (2,4)=6; (3,4)=7. Those within [3,6]: 3,4,5,5,6 => 5 pairs? Actually list: (1,2)=3 yes; (1,3)=4 yes; (1,4)=5 yes; (2,3)=5 yes; (2,4)=6 yes; (3,4)=7 no. So 5 pairs.
count_leq(6): i=0: j=3; while (1+4>6?5>6 false) j=3 cnt+=3-0=3 (pairs (0,1)=3, (0,2)=4, (0,3)=5) all ≤6. i=1: a[1]=2, j=3; while (2+4>6?6>6 false) j=3 cnt+=3-1=2 (pairs (1,2)=5, (1,3)=6) ≤6. i=2: a[2]=3, j=3; while (3+4>6?7>6 true) j=2; j>i? 2>2 false. total=5. count_leq(2): i=0: j=3; while (1+4>2?5>2 true) j=2; while (1+3>2?4>2 true) j=1; while (1+2>2?3>2 true) j=0; cnt+=0? no. i=1: j=0; while false; cnt0. total=0. ans=5-0=5. But l=3, so we need to subtract count_leq(2) which is correct, but are we sure that count_leq(2) excludes sums exactly 3? Yes. So answer 5 correct.
Now, test with l=4, r=5. Then answer should be pairs with sum 4 or 5: (1,3)=4, (1,4)=5, (2,3)=5 => 3 pairs. Compute count_leq(5)=? i=0: j=3; while (1+4>5?5>5 false) j=3 cnt+=3-0=3 (pairs: 1+2=3, 1+3=4, 1+4=5) all ≤5 -> includes sum=3 which is less than 4. i=1: a[1]=2, j=3; while (2+4>5?6>5 true) j=2; while (2+3>5?5>5 false) stop j=2 cnt+=2-1=1 (pair (1,2)=5) ≤5. i=2: a[2]=3, j=2; while false; j>i? false. total=4. count_leq(3): i=0: j=3; while (1+4>3?5>3 true) j=2; while (1+3>3?4>3 true) j=1; while (1+2>3?3>3 false) stop j=1 cnt+=1-0=1 (pair (0,1)=3). i=1: j=1; while false; j>i? 1>1 false. total=1. ans=4-1=3, correct.
So algorithm holds.
Now, note: For each test case, we sort the array. However, the two-pointer method in count_leq works for sorted arrays. But we must be careful: In count_leq, we use the condition while (j > i && a[i] + a[j] > S). Since the array is sorted in non-decreasing order, this works. If we sort in non-decreasing order, then for each i, as j decreases, sum decreases. Good.
Now, we must be cautious about using int for S; S can be up to 1e9, but a[i]+a[j] can be up to 2e9. So we need to ensure the addition does not overflow signed int? 2e9 < 2^31-1, so safe. However if we used short int, not.
Now, also consider that l-1 could be 0, that's fine.
Now finalize.
Now, let's consider if there is any more efficient way? Not needed.
Now, I'm thinking about potential pitfalls with large t and many small arrays. Sorting each small array is fine.
Now, I'm ready to produce final answer.
But before final, let's double-check that we are using the correct type for indices: int for i, j is fine as n ≤ 2e5.
Now, we need to output each answer on separate line.
Now, write code.
One more thing: In the count_leq function, we could also implement as:
ll cnt = 0;
int j = n-1;
for (int i = 0; i < n; i++) {
while (j > i && a[i] + a[j] > S) j--;
if (j > i) cnt += j - i;
}
return cnt;
But note: This increments j decreasing only. For each i, we start with j from previous iteration. That's correct.
Now, consider the case where n is large, and S is very small. Then for i=0, j will decrement many steps maybe all the way to 0. That's O(n) steps total across all i, because j never increases. So total while loop iterations across all i is at most n (since j decreases from n-1 to possibly 0 at most). So overall O(n). Good.
Now, in count_leq for large S, j never decrements, so while loop doesn't run often. So O(n).
Now, final code.
But there is a subtle detail: When we call count_leq(l-1), we might have l = 1, l-1 = 0. That's fine. But what if l is 0? Not allowed. So fine.
Now, is there any case where l-1 is negative? No, because l ≥ 1.
Thus ready.
Now, I'll produce final answer.
Now, but wait, there might be an alternative approach using binary search that some might find simpler to avoid pointer bugs. But I'll stick with two-pointer.
Now, let's also think about potential issue with integer type: In the while condition, we compare a[i] + a[j] > S. This addition is done in int. Since both are int, it's safe. But if we wanted to be extra safe, we could cast to long long. Not necessary.
Now, I'll write final solution.
But before concluding, let's test the sample with code in mind:
Input:
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
Processing:
Test1: n=3, a=[5,1,2] sort=[1,2,5], count_leq(7)=? as earlier 3, count_leq(3)=1 => ans=2.
Test2: n=5, a=[5,1,2,4,3] sort=[1,2,3,4,5], count_leq(8)=9, count_leq(4)=2 => ans=7.
Test3: n=4, a=[1,1,1,1] sort same, count_leq(1000)=6, count_leq(99)=6 => ans=0.
Test4: n=5, a=[2,5,5,1,1] sort=[1,1,2,5,5], count_leq(13)=10, count_leq(8)=9 => ans=1.
Matches.
Now, all good.
Now, I'll produce final answer. ```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// count pairs (i<j) with a[i] + a[j] <= S
ll count_leq(const vector& a, int S) {
int n = a.size();
int j = n - 1;
ll cnt = 0;
for (int i = 0; i < n; ++i) {
while (j > i && a[i] + a[j] > S) {
j--;
}
if (j > i) {
cnt += (j - i);
}
}
return cnt;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n, l, r;
cin >> n >> l >> r;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
sort(a.begin(), a.end());
ll ans = count_leq(a, r) - count_leq(a, l - 1);
cout << ans << '\n';
}
return 0;
}