시간 제한메모리 제한제출정답맞힌 사람정답 비율
0.6 초 1024 MB228746.667%

문제

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 $c_1, c_2, \dots , c_N$ consisting of $N$ integers, and $Q$ pairs of endpoints $(L_i , R_i)$, each representing the subarray $c_{L_i} , c_{L_i+1}, \dots , c_{R_i}$, where $1 ≤ i ≤ N$.

For each such pair $(L_i , R_i)$, Roxy is asked what is the maximum number of disjoint sum-0 subarrays one can choose from the queried array $c_{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.

입력

The first line of the input contains a single integer $N$.

The second line contains $N$ space-separated integers $c_1, c_2, \dots , c_N$.

The third line contains the number $Q$ of queries.

The next $Q$ lines contain two numbers $L_i$ and $R_i$ each, representing the $i$th query.

출력

Print $Q$ lines: on the $i$th line you should print the answer to the $i$th query.

제한

  • $1 ≤ N ≤ 400\,000$
  • $1 ≤ Q ≤ 400\,000$
  • $−10^9 ≤ c_i ≤ 10^9$ for all $1 ≤ i ≤ N$
  • $1 ≤ L_i ≤ R_i ≤ N$ for all $1 ≤ i ≤ Q$

서브태스크 1 (22점)

  • $1 ≤ N ≤ 5\,000$
  • $1 ≤ Q ≤ 5\,000$

서브태스크 2 (39점)

  • $1 ≤ N ≤ 100\,000$
  • $1 ≤ Q ≤ 100\,000$

서브태스크 3 (39점)

  • No additional constraints.

예제 입력 1

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

예제 출력 1

4
2
2

채점 및 기타 정보

  • 예제는 채점하지 않는다.