The legend of the foundation of Vectorland talks of two integers x and y. Centuries ago, the array king placed two markers at points |x| and |y| on the number line and conquered all the land in between (including the endpoints), which he declared to be Arrayland. Many years later, the vector king placed markers at points |x - y| and |x + y| and conquered all the land in between (including the endpoints), which he declared to be Vectorland. He did so in such a way that the land of Arrayland was completely inside (including the endpoints) the land of Vectorland.
Here |z| denotes the absolute value of z.
Now, Jose is stuck on a question of his history exam: "What are the values of x and y?" Jose doesn't know the answer, but he believes he has narrowed the possible answers down to n integers a_1, a_2, ..., a_n. Now, he wants to know the number of unordered pairs formed by two different elements from these n integers such that the legend could be true if x and y were equal to these two values. Note that it is possible that Jose is wrong, and that no pairs could possibly make the legend true.
Input
The first line contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of choices.
The second line contains n pairwise distinct integers a_1, a_2, ..., a_n (-10^9 ≤ a_i ≤ 10^9) — the choices Jose is considering.
Output
Print a single integer number — the number of unordered pairs \{x, y} formed by different numbers from Jose's choices that could make the legend true.
Examples
Input
3
2 5 -3
Output
2
Input
2
3 6
Output
1
Note
Consider the first sample. For the pair {2, 5}, the situation looks as follows, with the Arrayland markers at |2| = 2 and |5| = 5, while the Vectorland markers are located at |2 - 5| = 3 and |2 + 5| = 7:
The legend is not true in this case, because the interval [2, 3] is not conquered by Vectorland. For the pair {5, -3} the situation looks as follows, with Arrayland consisting of the interval [3, 5] and Vectorland consisting of the interval [2, 8]:
As Vectorland completely contains Arrayland, the legend is true. It can also be shown that the legend is true for the pair {2, -3}, for a total of two pairs.
In the second sample, the only pair is {3, 6}, and the situation looks as follows:
Note that even though Arrayland and Vectorland share 3 as endpoint, we still consider Arrayland to be completely inside of Vectorland.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to count unordered pairs (x, y) with x != y from given array a of n distinct integers such that the condition holds: The interval formed by [min(|x|, |y|), max(|x|, |y|)] (Arrayland) is a subset (inclusive endpoints) of the interval formed by [min(|x-y|, |x+y|), max(|x-y|, |x+y|)] (Vectorland). So condition: Arrayland ⊆ Vectorland.
We need to count number of unordered pairs where this holds.
Given x and y (real integers). Let's denote A = [L_A, R_A] with L_A = min(|x|, |y|), R_A = max(|x|, |y|). Vectorland endpoints: p = |x - y|, q = |x + y|. Since both are nonnegative. Let L_V = min(p, q), R_V = max(p, q). Condition: L_V ≤ L_A and R_A ≤ R_V.
So we need: min(|x-y|, |x+y|) ≤ min(|x|, |y|) AND max(|x|, |y|) ≤ max(|x-y|, |x+y|). Let's analyze this condition.
Let’s consider x and y can be any integers (including negative). The absolute values and sums/differences yield nonnegative values.
Observation: For any real numbers x, y, we have triangle inequalities: |x| - |y| ≤ |x±y| ≤ |x| + |y|. More precisely, we have:
|x+y| ≤ |x| + |y|, and |x-y| ≤ |x| + |y|.
| |x| - |y| | ≤ |x+y| and also ≤ |x-y|? Actually, we have: | |x| - |y| | ≤ |x - y| (reverse triangle inequality). Also, | |x| - |y| | ≤ |x + y|? Consider x=1, y=-1: | |1| - |-1| | = 0, and |1+(-1)|=0 so okay. But if x=2, y=1, then |2-1|=1, | |2|-|1| |=1, so equality. For x=2, y=-1, |2+(-1)|=1, | |2|-|1| |=1, so also holds. Indeed, we have: | |x| - |y| | ≤ |x ± y| for either plus or minus? Let's check: For any x,y, we have both: | |x| - |y| | ≤ |x - y|, and also ≤ |x + y|? Actually, consider x=1, y=1: |1+1|=2, difference=0 so 0≤2 ok. For x=1, y=-2: |1+(-2)|=1, |1-2|=1? Wait absolute difference of absolute values: | |1| - | -2| | = |1-2|=1, which equals |1+(-2)|=1. So yes, because |x+y| = |x - (-y)| and applying reverse triangle to x and -y gives: | |x| - | -y| | ≤ |x - (-y)| = |x + y|. So indeed | |x| - |y| | ≤ |x+y| and also ≤ |x-y|. So both hold.
Thus we have: L_V = min(|x-y|, |x+y|) ≥ | |x| - |y| |? Actually, we know that min of two numbers is at least? Not necessarily; it could be less than the absolute difference. Example: x=5, y=3: |x-y|=2, |x+y|=8, min=2 which is exactly |5-3|=2. So min = | |x| - |y| |? Let's compute: |5|-|3|=2, yes. For x=5, y=-3: |x-y|=|8|=8, |x+y|=|2|=2, min=2, and | |5|-| -3| | = |5-3|=2, so min = | |x| - |y| |. For x=2, y=1: min(|2-1|,|2+1|)=min(1,3)=1 = |2-1|=1. For x=2, y=-5: |x-y|=|7|=7, |x+y|=|-3|=3, min=3, | |2|-|5| |=3, yes. It seems that min(|x-y|, |x+y|) = | |x| - |y| |? Let's test a case where signs might affect? Consider x=2, y=0: |x-y|=2, |x+y|=2, min=2; | |2|-|0| |=2, ok. Consider x=0, y=0 trivial. Consider x=-2, y=3: |x-y|=|-5|=5, |x+y|=|1|=1, min=1, | | -2|-|3| |=|2-3|=1, ok. So indeed min(|x-y|,|x+y|) = ||x| - |y||. Why? Because the expressions |x-y| and |x+y| are distances on number line between x and y or between x and -y? More mathematically: For any real numbers x, y, we have: |x-y| and |x+y| are the two distances from x to y and from x to -y. But the absolute values of x and y are their distances from 0. There's a known identity: min(|x-y|,|x+y|) = ||x| - |y||. I think this is true if we consider that the numbers x and y lie on the real line; the distance between x and y is |x-y|, and the distance between x and -y is |x+y|. The distances from x to y and to -y are the two possibilities for the side lengths? Actually, we can derive: Consider squaring? Or use the formula: |x-y|^2 = x^2 + y^2 -2xy, |x+y|^2 = x^2 + y^2+2xy. Their minimum depends on sign of xy. If xy >= 0, then |x+y| = |x|+|y| and |x-y| = ||x|-|y||? Wait careful: If x and y have the same sign (both nonnegative or both nonpositive), then |x+y| = |x|+|y|, and |x-y| = ||x|-|y||. Because if they're same sign, subtracting gives difference of absolute values. If x and y have opposite signs, then |x-y| = |x|+|y| (since x - y = x - (negative) => x + |y|?), Actually: Suppose x >= 0, y < 0. Then |x-y| = |x - (negative)| = |x + |y|| = x + |y| = |x|+|y|. And |x+y| = |x + (negative)| = |x - |y|| = ||x| - |y||. So indeed, depending on sign, one of the two equals |x|+|y|, the other equals ||x|-|y||. So min(|x-y|,|x+y|) is always the smaller one, which is ||x|-|y|| (since that's always ≤ |x|+|y|). And max is |x|+|y|. Because the other one is |x|+|y|. Check: if same sign: |x-y| = ||x|-|y||, |x+y| = |x|+|y|, so min = ||x|-|y||, max = |x|+|y|. If opposite sign: |x-y| = |x|+|y|, |x+y| = ||x|-|y||, so min = ||x|-|y||, max = |x|+|y|. Yes! So indeed, for any real numbers x, y:
L_V = min(|x-y|,|x+y|) = ||x| - |y||.
R_V = max(|x-y|,|x+y|) = |x| + |y|.
That's a crucial simplification. So Vectorland interval is [||x|-|y||, |x|+|y|]. Arrayland is [min(|x|,|y|), max(|x|,|y|)] = [|x|, |y|] after sorting? Actually if we denote u = |x|, v = |y| (both nonnegative), then Arrayland is [min(u,v), max(u,v)] = [L_A, R_A] where L_A = min(u,v), R_A = max(u,v). Vectorland is [|u - v|, u + v] because ||x|-|y|| = |u - v|. So condition becomes:
|u - v| ≤ min(u, v) (since L_V ≤ L_A)
max(u, v) ≤ u + v (which is always true because max(u,v) ≤ u+v for nonnegative u,v; equality only when the other is zero). So the second condition is automatically satisfied (except maybe edge case? Let's verify: For u,v >=0, max(u,v) <= u+v always holds because if u≥v, u ≤ u+v trivially. So indeed it's always true. So the only nontrivial condition is the first: |u - v| ≤ min(u, v).
Given u,v >=0, what does |u - v| ≤ min(u, v) mean? Let's denote without loss assume u ≥ v? Then condition becomes (u - v) ≤ v => u ≤ 2v. Since u≥v, the condition is v ≤ u ≤ 2v. If we had v ≥ u, then condition (v-u) ≤ u => v ≤ 2u. So in general, for two nonnegative numbers, the condition is that the larger one is at most twice the smaller one. That is: max(u,v) ≤ 2 * min(u,v). Or equivalently, ratio between them is at most 2.
But careful: We must also consider the inclusive endpoints condition: L_V ≤ L_A. If u = v, then |u-v|=0, min(u,v)=u, condition holds (0≤u). So ratio 1 ≤2 okay. If u=0? Then min(0,v)=0, |0-v|=v, condition v ≤ 0? Only if v=0. But u and v are absolute values, can be zero if x=0 or y=0. But if one is zero and the other non-zero, then condition fails: e.g., x=0, y=5: u=0, v=5, |u-v|=5, min=0, 5≤0 false. So such pair fails. So condition requires both nonzero? Actually if u=0 and v=0 (both zero) then 0≤0 holds, but x and y must be distinct numbers? The input integers are distinct. Could both be zero? Only if zero appears once, but we need two different numbers, so cannot have both zero unless we have two zeros? But numbers are pairwise distinct, so at most one zero. So zero with nonzero fails. So effectively both absolute values must be positive, or possibly one zero only if both zero but that's not allowed.
Thus the necessary and sufficient condition for the legend to be true is: For the pair (x,y) with x ≠ y, let u = |x|, v = |y|, then we need max(u,v) ≤ 2 * min(u,v). And also note that we derived from L_V ≤ L_A; is there any additional condition from the inclusive endpoints? We also need that L_V ≤ L_A and R_A ≤ R_V. R_A ≤ R_V is always true for nonnegative u,v because max(u,v) ≤ u+v. Even if one is zero? Then R_A = v (if u=0) = v, R_V = 0+v = v, so equality holds? Actually if u=0, v>0: R_A = max(0,v)=v, R_V = 0+v=v, so R_A = v ≤ v holds. So the second condition holds even with zero. The only fail for zero is the first condition: |u-v| = v > 0, L_A=0, so fails. So indeed condition reduces to: |u - v| ≤ min(u,v) <=> max(u,v) ≤ 2 min(u,v). And we also need to consider the case where maybe something else? Let's test with some examples given: (x=2, y=5): u=2, v=5, max=5, min=2, 5 ≤ 4? No, fails, so legend false as sample says. (5, -3): u=5, v=3, max=5, min=3, 5 ≤ 6? Yes, so true. (2, -3): u=2, v=3, max=3, min=2, 3 ≤ 4? Yes, true. (3,6): u=3, v=6, max=6, min=3, 6 ≤ 6? Yes (equal), true. Also sample says (2,5) false; others true.
What about (x=1, y=3): u=1, v=3, 3 ≤2? false; original: Arrayland [1,3]; |x-y|=2, |x+y|=4 => Vectorland [2,4]; doesn't cover [1,2) so fails.
What about (x=-4, y=2): u=4, v=2, 4 ≤4? true. Original: |x|=4,|y|=2 => Arrayland [2,4]; |x-y|=|-4-2|=6, |x+y|=|-4+2|=2 => Vectorland [2,6]; includes [2,4] fine.
Now, what about negative or positive? Since we only use absolute values, the sign does not matter. So for any pair of distinct integers (x,y), the condition depends only on their absolute values. So we can simplify: Count unordered pairs {x,y} from the multiset? Actually array contains distinct integers, but after taking absolute values, they may have duplicates? But the original numbers are distinct, but absolute values could coincide (e.g., 3 and -3 both absolute 3). That's allowed; we need to treat each integer as separate. So we must consider the actual original numbers, but condition only depends on absolute values, and the two numbers must be distinct. So essentially we need to count pairs (i,j) such that i != j and max(|a_i|, |a_j|) ≤ 2 * min(|a_i|, |a_j|). And also note: is there any additional condition about ordering of x and y affecting the interval? No, because we defined A as min(|x|,|y|) to max, and V as min(|x-y|,|x+y|) to max, which we simplified. So condition is exactly that.
But wait: Are we missing something about the endpoints? The condition required "Arrayland was completely inside (including the endpoints) the land of Vectorland." Our derivation ensures L_V ≤ L_A and R_A ≤ R_V. However, we must also ensure that the intervals are defined as the land between the markers, including endpoints. So if L_V = L_A, it's okay. Similarly if R_V = R_A. So inequality inclusive. We used ≤, so inclusive. Good.
Thus problem reduces to: Given n distinct integers a_i, count unordered pairs (i<j) such that max(|a_i|, |a_j|) ≤ 2 * min(|a_i|, |a_j|). Since absolute values are nonnegative.
But wait: There is a nuance: The legend says the vector king placed markers at points |x-y| and |x+y| and conquered all land in between. However, note that if |x-y| = |x+y|, then Vectorland would be a single point? But that's fine. When would |x-y| = |x+y|? This happens when one of them is zero? Actually |x-y| = |x+y| implies squaring: (x-y)^2 = (x+y)^2 => -2xy = 2xy => 4xy=0 => xy=0. So if either x=0 or y=0. In that case, both are equal? For x=0, |x-y|=|y|, |x+y|=|y|, so both equal |y|. So Vectorland is just point |y|. And Arrayland would be [0, |y|] if the other is 0? Actually if x=0, y nonzero, Arrayland is [0, |y|] (since min(|0|,|y|)=0, max= |y|). Vectorland is [|y|, |y|] i.e., just the point |y|. Then is Arrayland inside Vectorland? Arrayland is interval [0,|y|], Vectorland is a point at |y|. The arrayland is not contained in Vectorland because it contains points less than |y|. Our condition correctly fails because max(|x|,|y|)=|y| <= 2min=0? Actually condition: max= |y|, min=0, then max ≤ 2min? That's |y| ≤ 0 false unless y=0. So correct.
Now, what about case when x and y are such that u and v satisfy u ≤ 2v and v ≤ 2u? That's the same condition. So we just need to count pairs where the absolute values satisfy that ratio.
But also note: The original legend says the vector king placed markers at |x-y| and |x+y|. That yields two numbers, which we take as endpoints of interval. However, is it always true that the interval between them includes the arrayland? Our condition derived seems necessary and sufficient. But double-check edge case when u or v = 0? Already covered.
So algorithm: compute absolute values b_i = |a_i|. Count unordered pairs (i<j) such that max(b_i, b_j) ≤ 2 * min(b_i, b_j). Equivalent to: For each pair, if we sort b_i and b_j, then the larger is at most twice the smaller.
Alternatively, for each number b_i, we want to count the number of other indices j such that b_j ∈ [ceil(b_i/2), 2b_i]? Actually careful: For a fixed b_i, the condition with b_j is: If b_j ≤ b_i, then we need b_i ≤ 2b_j, i.e., b_j ≥ ceil(b_i/2). If b_j ≥ b_i, then we need b_j ≤ 2b_i. So both sides: b_j must be within [b_i/2, 2b_i] (inclusive). But also b_i itself? But we need i≠j and distinct indices. However, note that if there are duplicate absolute values, they can satisfy condition because if b_i = b_j, then condition: b_i ≤ 2b_i holds trivially, so yes. So pairs with equal absolute values (e.g., 3 and -3) count. But careful: Original numbers must be distinct; they are because a_i are distinct. So if we have 3 and -3, they are distinct numbers, and condition holds because |3|=3, |-3|=3, and 3 ≤ 23 holds. So count them.
Thus the problem reduces to counting number of unordered pairs (i<j) such that b_j lies in the interval [ ceil(b_i/2), 2b_i ]? But if b_j is less than b_i, we need b_i ≤ 2b_j, which is equivalent to b_j ≥ ceil(b_i/2). So indeed, for each i, count j where b_j ∈ [ ceil(b_i/2), 2b_i ], but also ensure we don't double count? We can do two-pointer after sorting b array (list of absolute values). However, note that b_i may be repeated; we need to count pairs across indices. Since n up to 2e5, we can sort the b array and then for each index i, count number of indices j > i that satisfy condition? But careful: condition is symmetric and we want all unordered pairs. So we could for each i, count all j>i such that b_j ≤ 2b_i and b_i ≤ 2b_j. But note: For j > i (assuming sorted ascending), we have b_j ≥ b_i. Then condition reduces to b_j ≤ 2b_i. Since b_j ≥ b_i, automatically b_i ≤ 2b_j? Not necessarily? Let's check: For b_i and b_j with b_j ≥ b_i, we need both conditions: b_j ≤ 2b_i (since larger ≤ 2* smaller). And also the condition that smaller ≤ 2larger? That's b_i ≤ 2b_j, which is always true because b_i ≤ b_j ≤ 2b_j (since b_j ≥0). So for sorted order, when b_j ≥ b_i, condition reduces to b_j ≤ 2b_i. So it's a one-sided condition. But careful: That's only if we consider sorted ascending and we pick i as the smaller index (with smaller b). If we want to count all pairs, we can sort the b array and for each i (as the smaller element), count number of j > i such that b_j ≤ 2b_i. Then sum those counts. This includes pairs where b_i = b_j? Yes, because if b_i = b_j, then b_j = b_i ≤ 2b_i holds. So for equal values, we need to count all pairs among them. But note that in sorted array, for each i, counting j > i with b_j ≤ 2*b_i will count each pair exactly once because the smaller index is the one with smaller b, but when b_i = b_j, which is the smaller? Both indices satisfy i < j, so the pair is counted with i being the earlier index (since indices distinct). So all unordered pairs are counted exactly once.
Thus algorithm: Compute absolute values array B of length n. Sort B ascending. Then for each i from 0 to n-1, find the largest index r such that B[r] ≤ 2 * B[i]. Then the number of valid pairs with i as the smaller element is (r - i) (since for j from i+1 to r inclusive, condition holds). Sum over i. This counts all pairs satisfying B[j] ≤ 2*B[i] for j > i. But we must ensure that when B[i]=0? If B[i]=0, then condition B[j] ≤ 0 => B[j] =0 only. But since numbers distinct, only one zero maybe? Actually if zero appears, B[i]=0. For i with B[i]=0, we need to count j>i such that B[j] ≤ 0, i.e., only if B[j]=0. But there won't be another zero because original numbers distinct and absolute of zero only from zero. So (r-i) would be 0. So fine.
Now, is there any other condition? Wait, we assumed that for sorted order, the condition reduces to b_j ≤ 2b_i. But is that sufficient? Let's test a case where b_i is not the smallest? Suppose we have b_i=5, b_j=2 (i index earlier? Actually if sorted, b_i <= b_j. So we wouldn't have i with b_i=5 and j with b_j=2 because then b_i > b_j. So in the sorted method, we only consider i as smaller value. So condition becomes b_j <= 2b_i. However, we must also ensure that b_i itself satisfies something about b_j? The original condition for any pair (u,v) is max(u,v) ≤ 2min(u,v). In sorted terms, with u = min, v = max, this is exactly v ≤ 2u. So yes, that's correct. So counting pairs where v ≤ 2*u, where u is the smaller absolute value, v the larger.
Thus the problem reduces to counting pairs with ratio ≤2.
But we must be careful about possible nuance: Are we missing the case where one of |x-y| or |x+y| is the min and the other max? We already used that identity, which is mathematically correct for all real numbers. So it's fine.
Thus the solution: Compute absolute values, sort, then two-pointer to count pairs.
But before coding, let's test with sample:
Sample1: numbers: 2,5,-3 => abs: 2,5,3. Sorted: [2,3,5]. For i=0 (2): find r such that B[r] ≤ 4. r= index1? B[1]=3 ≤4, B[2]=5 >4, so r=1. Count = 1-0=1 (pair (2,3) i.e., abs values 2 and 3 correspond to numbers 2 and -3? But also maybe 2 and 3 corresponds to original numbers (2, -3) and (2, 5? Wait 3 is abs of -3, but also 5? Actually 5 not ≤4, so only (2, -3) counted. i=1 (3): 2*3=6, r: B[2]=5≤6 => r=2, count = 2-1=1 (pair (3,5) i.e., -3 and 5). i=2 (5): no j>2 => 0. Sum=2. matches sample.
Now consider a case with duplicate absolute values: e.g., numbers: -2, 2, 3. abs:2,2,3 sorted [2,2,3]. i=0: r such that ≤4: B[1]=2≤4, B[2]=3≤4? 3≤4 yes, so r=2, count =2-0=2 (pairs: (0,1): -2,2; (0,2): -2,3? Wait (0,2) is abs 2 and 3, condition: 3 ≤ 22=4, yes valid. So count includes ( -2,3) and (2,3) later? Actually both pairs from the two 2's and 3? Let's see: pair (-2,2) both absolute 2: 2 ≤ 22=4 true. pair (-2,3) and (2,3) both have 3 ≤ 4 true. So total should be 3 pairs. But our counting: i=0 gave 2 pairs (with j=1 and j=2). i=1 (second 2): find r such that ≤4: B[2]=3≤4 => r=2, count = 2-1=1 (pair (1,2) which is (2,3)). Sum = 3. So correct. So algorithm works even with duplicates.
Edge Cases: Negative numbers, zero. If zero present, abs=0. In sorted, zeros come first. For i with 0: 20=0, so r will be the last index where B[r] ≤0 => only if there are multiple zeros, but zeros correspond to distinct numbers? There can't be multiple zeros because numbers are distinct, so at most one zero. So i=0 (if zero exists) count 0. For later i >0, condition: for a nonzero b_i, we count j>i with b_j ≤ 2b_i. That includes possible pairs with zero? But note: when b_i is nonzero, if we consider i with b_i >0, then any j with b_j=0 would be before i in sorted order (since 0 < b_i). So those pairs would have been counted when i was the index of zero (as smaller) but we didn't count because condition fails (since 0, b_i fails max ≤ 2*min). So they are not counted. So that's correct.
Now complexity: sorting O(n log n), then two-pointer: For each i, we need to find r which is monotonic as i increases. Since array sorted, we can keep a pointer 'right' that moves forward as i increases, because when i increases, the threshold 2B[i] may shrink? Actually B[i] increases, so 2B[i] increases. So the rightmost index satisfying B[r] ≤ 2B[i] will be non-decreasing. So we can use two-pointer technique: Initialize right = 0 (or -1). For each i from 0 to n-1, while right+1 < n and B[right+1] ≤ 2B[i], increment right. Then count = right - i (since j>i up to right). But careful: right must be at least i, but if right < i, then count negative? Should start right at i-1, then expand. Actually typical: Initialize right = 0; For i in range(n): if right < i: right = i; while right+1 < n and B[right+1] ≤ 2*B[i]: right+=1; then add (right - i). This works. Complexity O(n).
Alternatively, we can use binary search for each i to find upper bound of 2*B[i] and count. That would be O(n log n) also fine, given n=2e5. But two-pointer is O(n).
Now, verify that the condition we derived is indeed correct and complete. But wait, there is nuance: The story says the array king placed markers at points |x| and |y| on the number line and conquered all the land in between (including the endpoints). That interval is [min(|x|,|y|), max(|x|,|y|)]. However, what if |x| and |y| are the same? Then Arrayland is a single point. That's fine. Our condition handles that.
Now, we derived that Vectorland interval is [||x|-|y||, |x|+|y|] based on identity min(|x-y|,|x+y|)=||x|-|y|| and max = |x|+|y|. But is it always true that the Vectorland interval is between those two values? The markers are placed at points |x-y| and |x+y|. The interval they cover is from the smaller to the larger. So yes.
Thus condition for Arrayland ⊆ Vectorland is:
min(|x|,|y|) >= ||x|-|y||? Actually we wrote L_V ≤ L_A: ||x|-|y|| ≤ min(|x|,|y|). And R_A ≤ R_V: max(|x|,|y|) ≤ |x|+|y|. So it's equivalent.
Now, is it possible that Vectorland could be "inside out"? No.
Thus solution appears trivial.
But before finalizing, let's double-check with some edge cases:
Case: x = -5, y = 2. u=5,v=2 => 5 ≤ 4? false. Original: Arrayland [2,5]; Vectorland: |(-5)-2|=7, |(-5)+2|=3 => [3,7]. Arrayland [2,5] not subset because 2 not covered. So fails.
Case: x = 0, y = 0 not allowed.
Case: x = 0, y = 1: fails as expected.
Seems consistent.
Now, is there any scenario where the condition max(u,v) ≤ 2 min(u,v) is necessary but not sufficient given the original constraints? Let's test scenario where u and v satisfy that but maybe Vectorland doesn't cover Arrayland due to something about sign? But we already proved identity independent of sign. So it's sufficient.
Thus the answer is count of unordered pairs with absolute values ratio ≤2.
But wait, could there be a scenario where Arrayland interval is not exactly [min(|x|,|y|), max(|x|,|y|)] because the markers are placed at |x| and |y|, but if |x| and |y| are placed, the interval between them is exactly that. So yes.
Thus solution seems straightforward.
Now, verify constraints: n up to 200k, a_i up to 1e9 in absolute value, so absolute values up to 1e9. Sorting is fine.
Implementation details:
Read n.
Read list a.
Compute list b = [abs(x) for x in a].
Sort b.
Initialize ans = 0, right = 0.
For i in range(n):
if right < i: right = i # maybe ensure right is at least i.
while right+1 < n and b[right+1] <= 2 * b[i]:
right += 1
ans += (right - i)
Print ans.
But note: Using two-pointer, as i increases, b[i] increases, so right will not decrease. So we can keep right pointer globally. However, need to be careful that b[right+1] may be less than 2b[i] but right may already be at end. Also need to ensure integer overflow? 2b[i] could be up to 2e9, fits in Python int.
Test with sample: as above.
Edge test: n=2, values 0 and 1: b=[0,1] sorted; i=0: while right+1<2 and b[1]<=0? 1 <=0 false, so right stays at 0? We need to initialize right = 0? But if i=0 and we haven't set right to i? Typically we set right = i (0). Then while condition: right+1 < n and b[1] <= 2*b[0]=0 -> false, so right remains 0. Then ans += (0-0)=0. i=1: right < i so set right=1. while right+1<2 false. ans+= (1-1)=0. total 0. Correct.
Another test: n=2, values -1 and 1: b=[1,1] sorted. i=0: right initially? We'll set right = 0? Actually we can start with right = 0. But better to start with right = -1? I'll implement: right = -1; for i in range(n): if right < i: right = i; while right+1 < n and b[right+1] <= 2b[i]: right +=1; ans += right - i. Let's test: b=[1,1]. i=0: right <0 => set right=0. while: right+1=1 <2 and b[1]=1 <=21=2 => true, so right=1. Now while again: right+1=2 not <2, stop. ans+= 1-0=1. i=1: right currently=1, so right>=i. while condition: right+1=2 not <2, stop. ans+= 1-1=0. total=1. correct.
Thus algorithm works.
But is there any tricky scenario where condition derived might be false? Let's re-derive identity:
We claim: min(|x-y|, |x+y|) = ||x|-|y|| and max = |x|+|y|.
Proof: For any real numbers, we have |x-y|^2 = x^2 + y^2 - 2xy, |x+y|^2 = x^2 + y^2 + 2xy. So if xy >= 0, then |x+y| >= |x-y|? Actually if xy >= 0, then 2xy >=0, so |x+y|^2 >= x^2+y^2 >= |x-y|^2? Actually |x+y|^2 = x^2+y^2+2xy, and |x-y|^2 = x^2+y^2-2xy. So if xy >= 0, then |x+y|^2 >= x^2+y^2 >= |x-y|^2? Not necessarily x^2+y^2 >= |x-y|^2? Actually |x-y|^2 = x^2+y^2-2xy ≤ x^2+y^2, and |x+y|^2 = x^2+y^2+2xy ≥ x^2+y^2. So the larger is |x+y|, the smaller is |x-y|. And we can compute: |x+y| = |x|+|y|? If x and y have same sign, then indeed |x+y| = |x|+|y| because sum of nonnegatives is sum, sum of negatives absolute is sum of absolute values. And |x-y| = ||x|-|y|| because subtracting numbers of same sign yields absolute difference of magnitudes. So min = ||x|-|y||, max = |x|+|y|. If xy <= 0 (different signs), then x+y is like difference of magnitudes, and x-y is sum of magnitudes. Specifically, if x ≥0, y≤0, then |x+y| = | |x| - |y| |, and |x-y| = |x|+|y|. So again min = ||x|-|y||, max = |x|+|y|. So identity holds.
Thus condition is correct.
Thus answer is count of pairs with ratio <=2.
Now, is there any case where x and y are both zero? Not allowed due to distinctness. If both zero but distinct? impossible.
Now, final solution is as above.
But wait, there might be an alternative interpretation: The legend says "the array king placed two markers at points |x| and |y| on the number line and conquered all the land in between (including the endpoints), which he declared to be Arrayland." This implies that the markers are placed at the absolute values of x and y. But note: If x is negative, the marker is placed at |x|, which is positive. So indeed markers are always nonnegative. So Arrayland is always a segment on the nonnegative side of the number line? Possibly yes, because |x| is nonnegative. So Arrayland lies in nonnegative numbers. Similarly, Vectorland markers are at |x-y| and |x+y|, which are also nonnegative, so Vectorland is also on the nonnegative side? Actually both are nonnegative, so interval is in [0, +∞). So it's fine.
Now, the condition we derived is necessary and sufficient. So problem solved.
But we must consider: The input values a_i can be negative, but we only care about absolute values.
Now, are there any hidden traps? Possibly the condition we derived of ratio ≤ 2 might not be sufficient if the intervals are not nested because of something like? Let's double-check with a more intricate example: x = -1, y = 2? u=1, v=2, ratio 2 ≤ 2? Actually max=2, min=1, 2≤2, holds. Arrayland [1,2]. Vectorland: |x-y| = |-1-2|=3, |x+y|=|1|=1 => interval [1,3]. Contains [1,2]. Good. x= -1, y = 3? u=1, v=3, 3≤2? fails, so not counted.
Now, let's test a case with one being zero? Already fails.
Now, what about x and y both negative, say x=-3, y=-6: u=3, v=6, ratio 6≤6 holds. Arrayland [3,6]; Vectorland: |(-3)-(-6)|=|3|=3, |(-3)+(-6)|=|-9|=9 => [3,9] contains [3,6]? Yes includes 3 to 6. Good.
Now, consider x and y such that the smaller absolute value is 0? Already fails.
Thus solution stands.
Thus complexity O(n log n) due to sorting. Could be O(n) if we use counting but not needed.
Now, think about integer overflow? Not in Python.
Now, check constraints: n up to 200k, a_i up to 1e9, so 2*a_i up to 2e9, fits in 32-bit signed? Actually 2e9 fits in 32-bit signed (max 2,147,483,647) but safe anyway.
Now, implement.
But wait: There is an alternative approach: Use binary search for each i. But we'll do two-pointer.
Implementation steps:
def solve():
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = [abs(x) for x in a]
b.sort()
ans = 0
right = 0
for i in range(n):
if right < i:
right = i
while right + 1 < n and b[right+1] <= 2 * b[i]:
right += 1
ans += right - i
print(ans)
Now, consider negative numbers only: a=[-5,-2,-1] -> b=[5,2,1] sorted [1,2,5]; i=0: 21=2, find r: indices 1? b[1]=2 ≤2 => r=1; b[2]=5 >2; ans+=1-0=1 (pair (1,2) i.e., 1 and 2). i=1: 22=4, r: b[2]=5 >4 => r stays at 1? Actually right currently=1, while: right+1=2, b[2]=5 ≤4? false; ans+=1-1=0; i=2: ans+=2-2=0; total=1. Which corresponds to pair with abs 1 and 2 (i.e., -1 and -2). Pair (2,5) fails because 5>4. Pair (1,5) fails because 5>2. So correct.
Now, test more random: x=10, y=6 -> ratio 10 ≤12? Actually 10≤12 true, so valid. Our condition says max ≤2*min. 10 and 6: max=10, min=6, 10 ≤12 true. So counts.
Now, time complexity O(n log n). Memory O(n).
Thus answer.
But wait, there is a nuance: The legend says "the vector king placed markers at points |x - y| and |x + y| and conquered all the land in between (including the endpoints), which he declared to be Vectorland. He did so in such a way that the land of Arrayland was completely inside (including the endpoints) the land of Vectorland." It says "He did so in such a way that ..." meaning he chose markers accordingly? Actually reading: "Many years later, the vector king placed markers at points |x - y| and |x + y| and conquered all the land in between (including the endpoints), which he declared to be Vectorland. He did so in such a way that the land of Arrayland was completely inside (including the endpoints) the land of Vectorland." So it's not that he chose x and y; rather, the legend states that the vector king placed markers such that Arrayland (which was already defined by x and y) ends up being inside Vectorland. So it's a property that must hold for the given x and y. So condition we derived is correct.
Now, we must ensure that we count unordered pairs {x,y} (order doesn't matter). That's what we are counting.
Thus the problem is solved.
But wait, we need to consider if there is any possible misinterpretation: Could the array king have placed markers at points x and y (without absolute)? The statement says "points |x| and |y|". So it's absolute values.
Thus correct.
Now, check if there is any scenario where the vector king's interval could be reversed (i.e., the larger marker is actually smaller)? No, we take min and max.
Thus final solution.
Now, test with a bigger random to ensure two-pointer logic works.
We'll also consider an alternative using Fenwick tree if values large but we sorted.
Now, we must produce answer as single integer.
Now, check constraints: n up to 2e5, so ans can be up to n*(n-1)/2 ~ 2e10, which fits in 64-bit (less than 2^63-1 ~9e18). Python int fine.
Now, let's also reason about potential pitfalls: What if b[i] = 0 and there are other zeros? But distinct numbers can't have two zeros because absolute of zero is zero, and numbers are distinct. However, could we have two distinct numbers both with absolute zero? Only zero has absolute zero. So at most one. So fine.
Now, ensure that our two-pointer does not overcount due to when b[i] = 0. For i with b[i]=0, while condition: b[right+1] <= 0, but all other b >0 so false. So right stays at i? Actually we set right = i (since right < i? right might be less than i if previous i? For first i=0, right=0 after set. While condition false. ans += 0-0=0. So good.
Now, we must also handle the case where n=200k and input large, use fast I/O.
Now, let's write final solution.
But wait, I recall that in Codeforces problem "Vectorland" maybe there is a twist? I think this is from Codeforces Round #? Actually I recall a problem about "Vectorland" or something. But anyway.
Double-check the problem statement: "The legend of the foundation of Vectorland talks of two integers x and y. Centuries ago, the array king placed two markers at points |x| and |y| on the number line and conquered all the land in between (including the endpoints), which he declared to be Arrayland. Many years later, the vector king placed markers at points |x - y| and |x + y| and conquered all the land in between (including the endpoints), which he declared to be Vectorland. He did so in such a way that the land of Arrayland was completely inside (including the endpoints) the land of Vectorland."
Our derivation matches.
Now, I want to test with more examples:
Example: x = -1000000000, y = 2000000000? u=1e9, v=2e9, ratio 2e9 ≤ 21e9=2e9 true. Should be valid. Our algorithm: b sorted [1e9, 2e9], i=0: 21e9=2e9, while: right+1=1, b[1]=2e9 <=2e9 true, right=1, ans+=1-0=1. So count. Does original condition hold? Arrayland [1e9,2e9]; Vectorland: |x-y| = | -1e9 - 2e9|? Actually -1e9 - 2e9 = -3e9 absolute 3e9, |x+y| = |-1e9+2e9|=|1e9|=1e9. So Vectorland [1e9,3e9], contains Arrayland. Good.
Now, consider x = 1, y = -1000000000: u=1, v=1e9, ratio 1e9 ≤2? fails. Not counted.
Now, maybe there is another condition: The vector king placed markers at points |x-y| and |x+y|, but does he conquer the land "in between" meaning the interval between those two points? That is indeed [min, max]. So we covered.
Thus final.
But let's be thorough: Could there be scenario where Arrayland is not inside Vectorland because the intervals might not be aligned? Actually both intervals are subsets of the nonnegative real line. So inclusion is just numeric.
Now, edge: n=200000 all same absolute? But numbers distinct? Could have both 3 and -3, but we could have many numbers with same absolute? For example, numbers: 1, -1, 2, -2, 3, -3, ... up to 100000 and -100000. So absolute values have duplicates. Count of pairs should be all pairs with ratio ≤2. Since all absolute values are from 1 to 100000, and ratio condition for two numbers a and b is max(a,b) ≤ 2min(a,b). For numbers from 1 to 100000, many pairs satisfy. Could be large. Our two-pointer algorithm should handle duplicates correctly. Let's test small: b = [1,1,2,2] sorted. i0: right up to index? 21=2 => all numbers ≤2: indices 0,1,2,3? Actually b[0]=1, b[1]=1, b[2]=2, b[3]=2 all ≤2, so right=3, count=3-0=3 (pairs: (0,1),(0,2),(0,3)). i1: now right already at 3, 21=2 still, so right remains 3, count=3-1=2 (pairs (1,2),(1,3)). i2: 22=4, right+1=4 out, count=3-2=1 (pair (2,3)). i3:0. Total=6. All 6 unordered pairs among 4 items? Actually total possible pairs = 4C2=6, all satisfy? Check pair between 1 and 2: 2 ≤2*1=2 holds; pair between 2 and 2 holds; pair between 1 and 1 holds. So yes all 6 pairs. So algorithm counts them. Good.
Now, if we have many duplicates, two-pointer still works as right is extended correctly.
Now, potential overflow in Python not issue.
Now, final code.
But we should consider if there is any other condition like "two different elements" meaning x and y must be different numbers. Our count only includes pairs with i<j, so they are different indices and numbers are distinct, so automatically different. So fine.
Thus answer.
Now, to be safe, test random small n brute force to verify condition vs ratio. Write a small test in head: for a few random numbers, compute manually. But seems fine.
Now, I'll produce final solution.
But I recall that there is a Codeforces problem "Vectorland" and the solution is exactly count pairs with ratio ≤2. So it's consistent.
Now, implement.
Potential pitfalls in two-pointer: If we initialize right = 0 and then for i=0, while condition uses b[right+1] which might go out-of-bounds if n=1, but n≥2. So okay.
Better to initialize right = -1, then for each i, if right < i: right = i. Then while.
Or we can do:
right = 0
for i in range(n):
while right < n and b[right] <= 2*b[i]:
right += 1
# but then we need to ensure right-1 is the last index satisfying condition. And we need to count from i+1 to right-1 inclusive. However, right will be one beyond. But careful: if we start right = i+1? easier as earlier.
Implementation:
ans = 0
right = 0
for i in range(n):
if right < i+1:
right = i+1
while right < n and b[right] <= 2 * b[i]:
right += 1
ans += (right - i - 1)
But we have to ensure right is non-decreasing. Let's test with [2,3,5], i=0: set right = max(right,i+1)=1? Actually right initially 0, so set to 1. while: b[1]=3 ≤4, right=2; while: b[2]=5 ≤4? false; ans+=2-0-1=1. i=1: right=2, i+1=2 so ok; while: b[2]=5 ≤6, right=3; ans+=3-1-1=1. i=2: right=3, i+1=3, while none; ans+=3-2-1=0. Works.
But careful for duplicates: [1,1,2,2], i=0: right set to 1; while: b[1]=1≤2, right=2; while: b[2]=2≤2, right=3; while: b[3]=2≤2, right=4; ans+=4-0-1=3. i=1: right currently 4, i+1=2 => right>=2; while condition: right<4? right=4, n=4, so no; ans+=4-1-1=2. i=2: right=4, i+1=3, ans+=4-2-1=1. i=3: ans+=4-3-1=0. total=6. Works.
Thus either implementation is fine.
I'll use the simpler: right = i; then while right+1 < n and b[right+1] <= 2*b[i]: right += 1; ans += right - i.
Now, we must ensure that if b[i] is large, multiplication may overflow? Not in Python.
Now, final code.
But before finalizing, check if there is any scenario where the condition is more complex because the vector king could have placed markers at |x-y| and |x+y|, but what if one of these is less than 0? They are absolute values, so nonnegative. So fine.
Now, any hidden detail: "the array king placed two markers at points |x| and |y| on the number line and conquered all the land in between (including the endpoints), which he declared to be Arrayland." That's clear.
Now, the legend says "He did so in such a way that the land of Arrayland was completely inside (including the endpoints) the land of Vectorland." This is a property that must hold for the specific x and y. So our condition is necessary and sufficient.
Thus answer is count of pairs.
Now, consider the possibility of x and y being such that one marker of Vectorland might be inside Arrayland? Doesn't matter as long as containment holds.
All good.
Thus final solution.
Now, I'll produce answer.
But there is an alternative perspective: The condition is that |x| and |y| lie between ||x|-|y|| and |x|+|y|. That's exactly the condition we derived. So indeed.
Now, check the provided note: In first sample, pair (2,5) fails, (5,-3) and (2,-3) succeed. That matches ratio condition: (2,5) -> 5>4 fail, (5,3) -> 5≤6 success, (2,3) -> 3≤4 success.
Thus solution is correct.
Now, we should output the integer.
Now, final code.
But wait, we need to be careful about the range of values: a_i can be negative up to -10^9, so absolute up to 10^9, sorting is fine.
Now, implement in Python.
We'll read input with sys.stdin, output with print.
Now, final.
But before finalizing, consider a scenario: Could there be any off-by-one due to integer division? We used direct inequality <=, which matches inclusive endpoints. So fine.
Now, let's think about potential alternative condition: The derived condition max(u,v) ≤ 2min(u,v) is equivalent to v ≤ 2u if v>=u. So we counted based on that.
Now, check if there is any scenario where u=0, v=0? Not possible distinct. So ignore.
But before final answer, check if there is any possibility that the condition could be satisfied when one of the absolute values is zero and the other is zero? Not applicable. So fine.
Now, final.
One more nuance: The problem says "two integers x and y". Jose has narrowed possible answers down to n integers a_i. He wants unordered pairs {x,y} with x and y from these n integers (different) such that the legend could be true. So we need to count unordered pairs of distinct elements from the given set that satisfy condition. So we treat each number as a candidate for x or y. So we need to consider all unordered pairs.
Our method does exactly that.
Now, answer.
I'll provide solution explanation: reduce to counting pairs with abs ratio ≤ 2.
Now, produce final code.
Given the problem statement, we must output a single integer.
We'll implement as described.
Potential pitfall: In two-pointer, if we use while right+1 < n and b[right+1] <= 2 * b[i], we need to make sure that right is initialized to i-1? Or we can set right = i and then while. That's fine.
Implementation in Python:
def solve():
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = [abs(v) for v in a]
b.sort()
ans = 0
right = 0
for i in range(n):
if right < i:
right = i
while right + 1 < n and b[right + 1] <= 2 * b[i]:
right += 1
ans += right - i
print(ans)
if name == "main":
solve()
Test with sample.
Now, final answer.
But wait: There's a nuance: The legend talks about two integers x and y. In the story, the array king placed markers at |x| and |y|. That suggests that x and y themselves could be negative, but the markers are placed at the absolute values. So the absolute values matter.
Thus our approach stands.
Now, I'm confident.
Now, produce final answer. ```python
import sys
def solve() -> None:
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
# absolute values
b = [abs(x) for x in a]
b.sort()
code
ans = 0
right = 0 # rightmost index satisfying b[right] <= 2*b[i]
for i in range(n):
if right < i:
right = i
# extend the pointer while the condition holds
while right + 1 < n and b[right + 1] <= 2 * b[i]:
right += 1
# all j in (i, right] are valid
ans += right - i
print(ans)