Uiro

시간 제한5초메모리 제한2048 MB

요약
각 질의 구간에서 0부터 시작해 카드를 순서대로 더하거나 빼되 중간값이 음수가 되지 않게 하며 뺄셈 횟수의 최댓값을 구한다.
난이도

보통10점 중 7점

유형
그리디, 누적 합, 동적 계획법
정답자
아직 제출이 없습니다

문제

Aoi has NN cards numbered from 11 to NN. Each card has a positive integer written on it. The integer written on the card ii (1≤i≤N1 ≤ i ≤ N) is A_iA\_i.

Aoi is going to play a game QQ times using the cards and a blackboard. The jj-th game (1≤j≤Q1 ≤ j ≤ Q) she plays consists of the following steps.

  1. Write 00 on the blackboard.

  2. Arrange the cards L_j,L_j+1,…,R_jL\_j , L\_j + 1, \dots , R\_j on the desk from left to right in this order.

  3. Perform the following operation for R_j−L_j+1R\_j − L\_j + 1 times. The kk-th operation (1≤k≤R_j−L_j+11 ≤ k ≤ R\_j − L\_j + 1) is as follows.

    • Let xx be the current integer written on the blackboard, and let yy be the integer written on the kk-th card from the left on the desk. Erase xx from the blackboard, and write either x+yx + y or x−yx − y instead. If x−yx − y is chosen, Aoi eats one piece of uiro, a traditional Japanese sweet.
    • However, writing an integer strictly less than 00 is not allowed.

For each game, you want to know the maximum number of uiro pieces Aoi can eat.

Given the information about cards and games, write a program that, for each game, calculates the maximum number of uiro pieces Aoi can eat.

입력

Read the following data from the standard input.

NN

A_1A\_1 A_2A\_2 ⋯\cdots A_NA\_N

QQ

L_1L\_1 R_1R\_1

L_2L\_2 R_2R\_2

⋮\vdots

L_QL\_Q R_QR\_Q

출력

Write QQ lines to the standard output. In the jj-th line (1≤j≤Q1 ≤ j ≤ Q), output the maximum number of uiro pieces Aoi can eat in the jj-th game.

제한

  • 1≤N≤200,0001 ≤ N ≤ 200\\, 000.
  • 1≤A_i≤1001 ≤ A\_i ≤ 100 (1≤i≤N1 ≤ i ≤ N).
  • 1≤Q≤200,0001 ≤ Q ≤ 200\\, 000.
  • 1≤L_j≤R_j≤N1 ≤ L\_j ≤ R\_j ≤ N (1≤j≤Q1 ≤ j ≤ Q).
  • Given values are all integers.

예제3

  1. 예제 1

    입력
    5
    3 4 7 2 8
    2
    1 3
    4 4
    
    예상 출력
    1
    0
    
  2. 예제 2

    입력
    14
    1 2 2 1 2 1 1 2 1 2 2 1 1 1
    5
    1 2
    1 14
    5 11
    3 12
    4 7
    
    예상 출력
    0
    8
    4
    6
    2
    
  3. 예제 3

    입력
    8
    16 23 45 76 43 97 12 43
    7
    1 8
    3 7
    2 7
    4 5
    5 8
    2 6
    3 5
    
    예상 출력
    3
    2
    2
    1
    2
    2
    1