exp (BOI contest)
Time limit: 1 seconds | Memory limit: 256 MB | Task type: batch
Algorithms: divide and conquer, dynamic programming, data structures, range queries, binary search

# Task: EXP
Exponents

BOI 2025, Day 2. Available memory: 1024 MB.

![](BOI)

2025.04.27

The famous polymath Nicolaus Copernicus was born and grew up in Toruń in the 15th century. Archaeologists have recently discovered his notebook, and learned that he was fond of using powers of two to store large numbers. In particular, even when he added two powers of two:

2a + 2b,

Copernicus evaluated the result and then rounded up the result to the nearest power of two. That is, he would evaluate $2^a + 2^b$ to $2^{\\max(a,b)+1}$. To evaluate a longer expression of the form:

$2^{b_1} + 2^{b_2} + ... + 2^{b_k},$

he first inserted the brackets to make it well-parenthesised*. For example, an expression $2^5 + 2^4 + 2^4 + 2^4 + 2^5$ can be made well-parenthesised to obtain $((2^5 + 2^4) + (2^4 + (2^4 + 2^5)))$. Finally, he evaluated the result of the obtained well-parenthesised expression, operating on powers of two as described above. Notice that the obtained result might vary depending on how he inserts the brackets. For example, here are two possible ways to evaluate $2^5 + 2^4 + 2^4 + 2^4 + 2^5$:

$$\begin{aligned}
(((2^5 + 2^4) + 2^4) + (2^4 + 2^5)) &= ((2^6 + 2^4) + 2^6) = (2^7 + 2^6) = 2^8 \\
((2^5 + (2^4 + 2^4)) + (2^4 + 2^5)) &= ((2^5 + 2^5) + 2^6) = (2^6 + 2^6) = 2^7
\end{aligned}$$

The first page of the Copernicus' notebook contains only a single expression $2^{a_1} + 2^{a_2} + ... + 2^{a_n}$ called the main expression. Later pages of the notebook then reference fragments of the main expression, which are of the form $2^{a_\ell} + 2^{a_{\ell+1}} + ... + 2^{a_r}$, for some $1 \le \ell \le r \le n$.

You are not sure about their meaning, but suspect that you should calculate, for each such fragment, the smallest possible result that can be obtained when evaluating the result as described above for the fragment. Note that each fragment is evaluated independently of the other fragments.

### Input

The first line contains two integers $n$ and $q$ ($1 \le n, q \le 300000$) denoting the length of the main expression from the first page of the notebook and the number of queries, respectively.

The second line contains $n$ integers $a_1, a_2, ..., a_n$ ($0 \le a_i \le 10^6$), where the $i$-th integer $a_i$ denotes the exponent of the $i$-th power of two in the main expression.

The next $q$ lines describe the queries. Each query consists of two integers $\ell$ and $r$ ($1 \le \ell \le r \le n$) representing a fragment of the $\ell$-th power of two and ending at the $r$-th power of two.

### Output

You should output $q$ lines. The $i$-th line should contain the smallest possible result that can be obtained when evaluating the fragment described in the $i$-th query. You should output only the exponent of the corresponding power of two.

### Example

| For the input data: | the correct result is: |
|---------------------|------------------------|
| `8 4`                 | `7`                      |
| `2 4 2 5 4 4 4 5`     |                        |
| `4 8`                 | `7`                      |
| `1 4`                 | `7`                      |
| `2 5`                 | `8`                      |
| `1 7`                 | `8`                      |

*The formal definition of a well-parenthesised expression is as follows: $2^a$ is a well-parenthesised expression for any non-negative integer $a$; if $E_1$ and $E_2$ are well-parenthesised expressions then so is $(E_1 + E_2)$. No other expressions are well-parenthesised.

## Scoring

| Subtask | Constraints                | Points |
|---------|----------------------------|--------|
| 1       | $n \le 8, q \le 10$       | 6      |
| 2       | $n \le 200$               | 8      |
| 3       | $n, q \le 2000$           | 23     |
| 4       | $a_i \le 20$              | 22     |
| 5       | No additional constraints. | 41     |

Starter code:

Grader code:

Setup script:

Evaluation script: