Fish
Time limit1sMemory limit64 MB
Count contiguous subarrays whose sum is at least K.
- Level
Medium7 of 10
- Topics
- Prefix sum, Divide and conquer, Sorting, Binary search
- Solved
- No attempts yet
Problem
Kuching 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 segments from head to tail, numbered 1 to , 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 , so the sum has to be at least . 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 segments, which is the whole fish.
Input
The first line contains two 32-bit signed integers and . is always positive, while may be positive, zero, or negative. ()
The second line contains the satisfaction ratings of the segments in order. The -th integer is the rating of the -th segment. Every rating fits in a 32-bit signed integer and may be positive, zero, or negative.
Output
Print a single integer, the number of ways to cut out one chunk whose satisfaction ratings sum to at least .
Hint
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 . Segments 1, 3 and 5 are not consecutive, so (1, 3, 5) is not a chunk.