QOJ.ac

QOJ

時間限制: 2.0 s 記憶體限制: 512 MB 總分: 100 可 Hack ✓

#17148. Sliding Window

统计

Consider an array of numbers $b_1, \dots, b_m$. We define sliding windows of length $k$ ($k \le m$) on this array as all subsegments of length $k$, that is, the segments $[b_1, \dots, b_k], [b_2, \dots, b_{k+1}], \dots, [b_{m-k+1}, \dots, b_m]$.

Given an array of numbers $a_1, \dots, a_n$ of length $n$.

You need to answer $q$ queries of the following type regarding this array: for given $l, r, k$, find the sum of the minimums of all sliding windows of length $k$ within the subsegment $[a_l, \dots, a_r]$.

Input

The first line of input contains two integers $n$ and $q$ ($1 \le n, q \le 100\,000$) — the length of the array and the number of queries.

The second line contains $n$ integers $a_1, \dots, a_n$ ($1 \le a_i \le 10^9$) — the values of the numbers in the array.

The next $q$ lines contain the queries. The $i$-th line contains three integers $l_i, r_i$, and $k_i$ ($1 \le l \le r \le n$, $1 \le k \le r - l + 1$) — the left and right boundaries of the subsegment and the length of the sliding window in the $i$-th query.

Output

Output $q$ lines with the answers to the queries. In the $i$-th line, output a single number — the sum of the minimums of the sliding windows of length $k_i$ on the subsegment $[a_{l_i}, \dots, a_{r_i}]$.

Subtasks

Subtask Points Additional Constraints Required Subtasks
1 6 $n, q \le 300$
2 12 $n, q \le 4000$ 1
3 8 $n, q \le 10\,000$ 1, 2
4 11 $n \le 4000$ 1, 2
5 10 $k_i$ are equal in all queries
6 14 $a_i \le 2$
7 7 $a_i \le 20$ 6
8 15 $l_i = 1, r_i = n$
9 17 none 1–8

Examples

Input 1

6 3
4 6 1 2 5 3
2 5 2
2 4 1
1 6 6

Output 1

4
9
1

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.