For each subarray, find the smallest non-negative integer that no subsequence sums to.
Hard9Segment treeGreedySortingNo attempts yetTime limit2sMemory limit512 MBYou are given a sequence A of length N. A subsequence is what is left after deleting some elements of A, and deleting every element is allowed, so the empty sequence is a subsequence too. The sum of a subsequence is the sum of the integers it keeps, and the sum of the empty subsequence is 0.
For example, if A is [1, 1, 3, 7], the subsequences [], [1], [1, 1], [3], [1, 3], [1, 1, 3] have sums 0, 1, 2, 3, 4, 5 in that order. No subsequence sums to 6, so the smallest non-negative integer that is not a subsequence sum of A is 6.
Then M queries are given. Each query consists of two integers L and R and asks for the smallest non-negative integer that is not the sum of any subsequence of AL,AL+1,…,AR. Write a program that answers every query.
The first line contains the length of the sequence N (1≤N≤100000). The second line contains the sequence A1,A2,…,AN. Every number is a natural number at most 109, and the sum of all numbers in the sequence is also at most 109.
The third line contains the number of queries M (1≤M≤100000). Each of the next M lines contains one query in the form Li Ri (1≤Li≤Ri≤N).
Print the answer to each query on its own line, in the order the queries are given.