#P16563. [ICPC 2026 APC] Parallel Sums

[ICPC 2026 APC] Parallel Sums

Problem Description

You are given two integers n n and m m . For a sequence of n n integers A=(a1,a2,…,an) A=(a_1, a_2, \ldots, a_n) , the parallel sums of A A are the n−m+1 n-m+1 integers s1,s2,…,sn−m+1 s_1, s_2, \ldots, s_{n-m+1} defined by si=ai+ai+1+…+ai+m−1 s_i = a_i + a_{i+1} + \ldots + a_{i+m-1} for each i i ( 1≤i≤n−m+1 1 \leq i \leq n-m+1 ).

You are given the values of s1,s2,…,sn−m+1 s_1, s_2, \ldots, s_{n-m+1} . Your task is to answer q q queries, described as follows. In the j j -th query, you are given two integers lj l_j and rj r_j , and you are asked to find the smallest possible value of max⁡(alj,alj+1,…,arj) \max(a_{l_j}, a_{l_j+1}, \ldots, a_{r_j}) among all sequences A=(a1,a2,…,an) A=(a_1, a_2, \ldots, a_n) of n n integers (possibly negative) such that s1,s2,…,sn−m+1 s_1, s_2, \ldots, s_{n-m+1} are the parallel sums of A A . Or determine if this value can be arbitrarily small.

Input Format

The first line of input contains two integers n n and m m ( 1≤m≤n≤200 000 1 \leq m \leq n \leq 200\,000 ).

The second line contains n−m+1 n-m+1 integers s1,s2,…,sn−m+1 s_1, s_2, \ldots, s_{n-m+1} ( −109≤si≤109 -10^9 \leq s_i \leq 10^9 ).

The third line contains a single integer q q ( 1≤q≤100 000 1 \leq q \leq 100\,000 ).

The j j -th of the next q q lines contains two integers lj l_j and rj r_j ( 1≤lj≤rj≤n 1 \leq l_j \leq r_j \leq n ).

Output Format

Output q q lines. The j j -th line should contain the smallest possible value of max⁡(alj,alj+1,…,arj) \max(a_{l_j}, a_{l_j+1}, \ldots, a_{r_j}) . If that value can be arbitrarily small, output unbounded instead.

8 4
4 -4 2 6 5
4
3 7
4 6
1 8
2 5
2
unbounded
4
-1

Hint

Explanation for the sample input/output #1

For the first query, take A=(9,−4,−3,2,1,2,1,1) A = (9, -4, -3, 2, 1, 2, 1, 1) . The parallel sums of A A are (4,−4,2,6,5) (4, -4, 2, 6, 5) as required. Then $\max(a_3, \ldots, a_7) = \max(-3, 2, 1, 2, 1) = 2$ . It can be shown that 2 2 is the smallest possible value.

For the second query, you can make the value arbitrarily small.

For the third query, take A=(4,−3,0,3,−4,3,4,2) A = (4, -3, 0, 3, -4, 3, 4, 2) . Then max⁡(4,−3,0,3,−4,3,4,2)=4 \max(4, -3, 0, 3, -4, 3, 4, 2) = 4 , which is the smallest possible value.