Sums on Segments
Contest: Educational Codeforces Round 173 (Rated for Div. 2)
Difficulty rating: 1600
Tags: binary search, brute force, data structures, dp, greedy, math

You are given an array $$$a$$$ of $$$n$$$ integers, where all elements except for at most one are equal to $$$-1$$$ or $$$1$$$. The remaining element $$$x$$$ satisfies $$$-10^9 \le x \le 10^9$$$.

Find all possible sums of subarrays of $$$a$$$, including the empty subarray, whose sum is defined as $$$0$$$. In other words, find all integers $$$x$$$ such that the array $$$a$$$ has at least one subarray (possibly empty) with sum equal to $$$x$$$. A subarray is a contiguous subsegment of an array.

Output these sums in ascending order. Each sum should be printed only once, even if it is achieved by multiple subarrays.

Input format:
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then, $$$t$$$ test cases follow.

Each test case consists of two lines:

- The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array.
- The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the elements of the array $$$a$$$. In the array $$$a$$$, there is at most one element that is neither $$$1$$$ nor $$$-1$$$.

Additional constraint on the input: the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output format:
For each test case, output two lines:

- In the first line, print a single integer — the number of distinct subarray sums.
- In the second line, print these sums in ascending order.

Each sum should be printed only once, even if it is produced by multiple subarrays.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
Let's define $$$a[i,j]$$$ as the subarray of $$$a$$$ from position $$$i$$$ to position $$$j$$$.

Consider the first test case of the example:

- $$$-1$$$ is produced by $$$a[2,2]$$$;
- $$$0$$$ is produced by the empty subarray;
- $$$1$$$ is produced by $$$a[4,4]$$$;
- $$$2$$$ is produced by $$$a[4,5]$$$;
- $$$9$$$ is produced by $$$a[2,3]$$$;
- $$$10$$$ is produced by $$$a[1,3]$$$;
- $$$11$$$ is produced by $$$a[3,4]$$$;
- $$$12$$$ is produced by $$$a[3,5]$$$.

Example input:
5
5
1 -1 10 1 1
5
-1 -1 -1 -1 -1
2
-1 2
2
7 1
3
1 4 -1

Example output:
8
-1 0 1 2 9 10 11 12 
6
-5 -4 -3 -2 -1 0 
4
-1 0 1 2 
4
0 1 7 8 
6
-1 0 1 3 4 5

Editorial:
What could the answer to the problem be if all elements were equal to $$$1$$$ or $$$-1$$$? Let's consider all segments with a fixed left boundary $$$l$$$. The empty segment $$$[l; l-1]$$$ has a sum of $$$0$$$. As we move the right boundary to the right, the sum will change by $$$\pm1$$$. That is, we can obtain all sums from the minimum sum to the maximum one. To find the sums for the entire array, we need to find the union of the segments. Since all segments include $$$0$$$, their union is also a segment. Therefore, the possible sums are all sums from the minimum sum to the maximum sum in the entire array.
Now let's apply this reasoning to the given problem. The segments that do not include the strange element still form a segment of possible sums that includes $$$0$$$. As for the segments that include the strange element, we can look at them this way. We will remember these segments and remove the strange element. Then the resulting sums will also form a segment that includes $$$0$$$. If we return the element to its place, all sums will increase exactly by this element. Thus, it will remain a segment, however, not necessarily including $$$0$$$ now.
Then the solution could be as follows. We will find the minimum and maximum sum among the segments that do not contain the strange element. We will find the minimum and maximum sum among the segments that do contain it. Then we will output the union of the obtained sum segments.
Next, you need to adapt your favorite algorithm for finding the maximum sum segment for this problem. My favorite is reducing it to prefix sums. The sum of the segment $$$[l; r]$$$ is equal to $$$\mathit{pref}_{r+1} - \mathit{pref}_l$$$. We fix the right boundary of the segment $$$r$$$. Since the first term for all segments is now the same, the maximum sum segment with this right boundary is the one with the minimum possible prefix sum at the left boundary. We will then iterate over $$$r$$$ in increasing order and find the maximum sum among all right boundaries. The minimum prefix sum on the left can be maintained on the fly.
For a fixed right boundary, we have two options: for some prefix of left boundaries, the strange element is inside the segment, and for some suffix, it is outside. This suffix may be empty if the boundary $$$r$$$ is to the left of the strange element. Therefore, we will maintain two values on the fly: the minimum prefix sum before the strange element and after it.
Finally, we need to find the possible sums in the union of the two segments. There are two options here. If the segments intersect, then it includes all sums from the minimum of the left boundaries to the maximum of the right ones. If they do not intersect, then it is simply two segments.
Overall complexity: $$$O(n)$$$ per testcase.