Sequence and Queries 0
Time limit2.5sMemory limit512 MB
For each query range [i,j] of a ±1 sequence, report the length of the longest contiguous subarray inside it whose sum is 0, or 0 if none exists.
- Level
Hard9 of 10
- Topics
- Segment tree, Prefix sum, Divide and conquer, Dynamic programming
- Solved
- No attempts yet
Problem
You are given a sequence of length in which every element is or . Write a program that answers the following query.
i j: among the contiguous subsequences contained in , take those whose elements sum to and print the length of the longest one. Print if no contiguous subsequence sums to .
Input
The first line contains the size of the sequence ().
The second line contains separated by spaces. ( is or )
The third line contains the number of queries ().
Each of the next lines holds one query, given as two integers and ().
Output
Print the answer to each query on its own line, in the order the queries are given.