Count contiguous subarrays whose sum is at least K.
Medium7Prefix sumDivide and conquerSortingBinary searchNo attempts yetTime limit1sMemory limit64 MBKuching the cat likes eating fish. He accidentally bought a fish that is far too large and does not want to eat all of it. He cut the fish into N segments from head to tail, numbered 1 to N, and gave every segment a satisfaction rating. The higher the rating, the more Kuching enjoys that segment. Fish only tastes good when it is eaten as a chunk. A chunk is one or more segments with consecutive numbers.
Kuching is picky and does not enjoy a chunk whose satisfaction ratings sum to less than K, so the sum has to be at least K. Count the ways to cut a single chunk out of the fish that Kuching would enjoy eating. Segments outside the chunk are thrown away.
A chunk holds at least 1 segment and at most N segments, which is the whole fish.
The first line contains two 32-bit signed integers N and K. N is always positive, while K may be positive, zero, or negative. (1≤N≤200,000)
The second line contains the satisfaction ratings of the N segments in order. The i-th integer is the rating of the i-th segment. Every rating fits in a 32-bit signed integer and may be positive, zero, or negative.
Print a single integer, the number of ways to cut out one chunk whose satisfaction ratings sum to at least K.
In the first example Kuching cut the fish into 5 segments with ratings 1, -2, 3, -4, 5, and he only enjoys chunks whose sum is at least 2. The six chunks he enjoys are (1, -2, 3), (1, -2, 3, -4, 5), (-2, 3, -4, 5), (3), (3, -4, 5) and (5). Every other chunk sums to less than K. Segments 1, 3 and 5 are not consecutive, so (1, 3, 5) is not a chunk.