Problem 511
Number of Zero-Filled Subarrays
Problem
Count the non-empty blocks of consecutive elements that contain only zeros. Blocks at different positions count separately. A run of k zeros in a row contains k × (k + 1) / 2 such blocks, so it is enough to add, at every zero, the length of the run of zeros ending there. The answer can be larger than 2^31, so use a 64-bit integer.
Input
The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].
Output
Print the number of zero-filled subarrays.
Constraints
1 <= N <= 100000; -1000000000 <= A[i] <= 1000000000
Example input
8 1 3 0 0 2 0 0 4
Example output
6
Explanation
In 1 3 0 0 2 0 0 4 there are two runs of two zeros. Each run contains [0], [0] and [0 0], so 3 blocks per run and 6 in total.