spoj#ZQUERY. Zero Query
Zero Query
English | Vietnamese |
Given an array having N elements, each element is either -1 or 1.
You have M queries, each query has two numbers L and R, you have to answer the length of the longest subarray in range L to R (inclusive) that its sum is equal to 0.
Input
The first line contains two numbers N and M (1 <= N, M <= 50000) - the number of elements and the number of queries.
The second line contains N numbers - the elements of the array, each element is either -1 or 1.
In the next M lines, each line contains two numbers L and R (1 <= L <= R <= N).
Output
For each query, print the length of the longest subarray that satisfies the query in one line. If there isn't any such subarray, print 0.
Note
Subarray in an array is like substring in a string, i.e. subarray should contain contiguous elements.
Example
Input: 6 4 1 1 1 -1 -1 -1 1 3 1 4 1 5 1 6
Output: 0 2 4 6