atcoder#ABC305D. [ABC305D] Sleep Log

[ABC305D] Sleep Log

Score : 450450 points

Problem Statement

Takahashi keeps a sleep log. The log is represented as an odd-length sequence A=(A1(=0),A2,,AN)A=(A _ 1(=0), A _ 2,\ldots,A _ N), where odd-numbered elements represent times he got up, and even-numbered elements represent times he went to bed. More formally, he had the following sleep sessions after starting the sleep log.

  • For every integer ii such that 1iN121\leq i\leq\dfrac{N-1}2, he fell asleep exactly A2iA _ {2i} minutes after starting the sleep log and woke up exactly A2i+1A _ {2i+1} minutes after starting the sleep log.
  • He did not fall asleep or wake up at any other time.

Answer the following QQ questions. For the ii-th question, you are given a pair of integers (li,ri)(l _ i,r _ i) such that 0liriAN0\leq l _ i\leq r _ i\leq A _ N.

  • What is the total number of minutes for which Takahashi was asleep during the rilir _ i-l _ i minutes from exactly lil _ i minutes to rir _ i minutes after starting the sleep log?

Constraints

  • 3N<2×1053\leq N\lt2\times10^5
  • NN is odd.
  • 0=A1<A2<<AN1090=A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq10^9
  • 1Q2×1051\leq Q\leq2\times10^5
  • 0liriAN (1iQ)0\leq l _ i\leq r _ i\leq A _ N\ (1\leq i\leq Q)
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN

A1A _ 1 A2A _ 2 \ldots ANA _ N

QQ

l1l _ 1 r1r _ 1

l2l _ 2 r2r _ 2

\vdots

lQl _ Q rQr _ Q

Output

Print the answer in QQ lines. The ii-th line should contain an integer answering to the ii-th question.

7
0 240 720 1320 1440 1800 2160
3
480 1920
720 1200
0 2160
480
0
960

Takahashi slept as shown in the following figure.

The answers to each question are as follows.

  • Between 480480 minutes and 19201920 minutes after starting the sleep log, Takahashi slept from 480480 minutes to 720720 minutes, from 13201320 minutes to 14401440 minutes, and from 18001800 minutes to 19201920 minutes in 33 sleep sessions. The total sleep time is 240+120+120=480240+120+120=480 minutes.
  • Between 720720 minutes and 12001200 minutes after starting the sleep log, Takahashi did not sleep. The total sleep time is 00 minutes.
  • Between 00 minutes and 21602160 minutes after starting the sleep log, Takahashi slept from 240240 minutes to 720720 minutes, from 13201320 minutes to 14401440 minutes, and from 18001800 minutes to 21602160 minutes in 33 sleep sessions. The total sleep time is 480+120+360=960480+120+360=960 minutes.

Therefore, the three lines of the output should contain 480480, 00, and 960960.

21
0 20 62 192 284 310 323 324 352 374 409 452 486 512 523 594 677 814 838 946 1000
10
77 721
255 541
478 970
369 466
343 541
42 165
16 618
222 592
730 983
338 747
296
150
150
49
89
20
279
183
61
177