luogu#P4846. LJJ爱数书
LJJ爱数书
Background
Please refer to the solution article at https://www.cnblogs.com/Blog-of-Eden/p/9367521.html.
Problem Description
LJJ has a “number book” at home, which means the book contains only numbers. LJJ loves it very much.
In the number book there is a sequence . In each operation, you may add or subtract to a continuous interval, and then take modulo . We define the harmony function as the minimum number of operations needed to make all elements of the sequence become .
For example, when and , by changing into , and then changing into , the goal can be achieved, so .
Now, given a sequence of length , let denote the continuous subsequence of from position to position . There are queries. Each query gives . You need to compute the value of .
Input Format
Line : Two integers , meaning the sequence length is , and there are queries.
Line : integers, where the -th integer represents .
Lines to : Each line contains three integers .
Output Format
Output lines: each line contains one integer, representing the answer for each query.
7 2
8 8 8 0 8 8 8
1 7 9
3 5 17
2
16
4 1
5 3 8 2
1 4 9
8
10 10
7 7 6 5 5 2 8 5 0 3
1 8 11
3 10 11
4 7 12
9 10 12
3 5 10
2 7 10
7 9 10
2 7 11
1 4 11
4 7 10
12
15
9
3
5
8
5
9
6
7
Hint
It is guaranteed that for each query, .
For of the testdata: , , .
Translated by ChatGPT 5