QOJ.ac

QOJ

Time Limit: 1.0 s Memory Limit: 256 MB Total points: 100 Hackable ✓

#14436. Robot Construction

Statistics

You have an ability to create robots of height lying in $[0, d]$. To test them, you constructed a line of length $n$ containing some obstacles. The description of obstacles is given to you as an array $a$. If $a_i = 0$, then there is no obstacle at the $i$-th position, and otherwise, there is an obstacle of height $a_i > 0$.

To test your robot, you select a segment $[l, r]$ and run it through all obstacles contained in the segment. When the robot encounters an obstacle with value $a_i > 0$, one of the two possibilities happens:

  • If the current height of the robot $h$ is smaller than $a_i$, then nothing happens since the robot is too short to reach the obstacle.
  • If the current height of the robot $h$ is at least $a_i$, then the new height changes to $h' = h - a_i$.

Now you need to answer $q$ queries. For each of the $q$ segments $[l_i, r_i]$, you need to tell the maximum possible height with which the robot can end up after running through the obstacles in the segment if the initial height was picked from the interval $[0, d]$.

Input

First line contains a pair of integers $n, q, d$ ($1 \le n, q \le 3 \cdot 10^5, 1 \le d \le 10^9$) — length of the array and number of queries.

Second line contains $n$ integers $a_i$ ($0 \le a_i \le 10^9$) — description of the array.

Next $q$ lines contain a pair of integers $[l_i, r_i]$ ($1 \le l_i \le r_i \le n$) — description of the queries.

Output

Output $q$ integers — answers to the queries.

Examples

Input 1

5 3 5
0 2 6 1 3
5 5
1 5
1 3

Output 1

2
2
3

Input 2

7 5 10
7 6 2 5 0 1 4
1 3
1 7
4 7
2 5
4 6

Output 2

3
2
3
3
4

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#374EditorialOpen题解jianglyView

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.