Sum Zero

Time limit: 0.9s Memory limit: 21MB Input: Output:

Roxy, the space traveler, is facing a very abstract problem. Since she's clueless as to how to solve it, you, as her best friend, have no choice but to help her out:

She is given an array c1,c2,,cNc_1, c_2, \dots , c_N consisting of NN integers, and QQ pairs of endpoints (Li,Ri)(L_i, R_i), each representing the subarray cLi,cLi+1,,cRic_{L_i} , c_{L_{i+1}}, \dots, c_{R_i} , where 1iN1 \leq i \leq N.

Task

For each such pair (Li,Ri)(L_i, R_i), Roxy is asked what is the maximum number of disjoint sum-00 subarrays one can choose from the queried array cLi,cLi+1,,cRic_{L_i} , c_{L_{i+1}}, \dots , c_{R_i}. Two subarrays are considered disjoint if they have no entries in common; however, they can still have neighboring endpoints. Note that, there might be entries from the queried array that are not part of any of the chosen subarrays.

Input

The first line of the input contains a single integer NN.

The second line contains NN space-separated integers c1,c2,,cNc_1, c_2, \dots , c_N.

The third line contains the number QQ of queries.

The next QQ lines contain two numbers LiL_i and RiR_i each, representing the ithi^{th} query.

Output

Print QQ lines: on the ithi^{th} line you should print the answer to the ithi^{th} query.

Constraints

  • 1N400 0001 \leq N \leq 400 \ 000
  • 1Q400 0001 \leq Q \leq 400 \ 000
  • 109ci109-10^9 \leq c_i \leq 10^9 for all 1iN1 \leq i \leq N
  • 1LiRiN1 \leq L_i \leq R_i \leq N for all 1iQ1 \leq i \leq Q
# Points Constrains
1 22 1N,Q5 0001 \leq N, Q \leq 5 \ 000
2 39 1N,Q100 0001 \leq N, Q \leq 100 \ 000
3 39 No further constraints.

Examples

stdin

10
1 2 -3 0 1 -4 3 2 -1 1
3
1 10
1 5
2 9

stdout

4
2
2

Log in or sign up to be able to send submissions!