Task
We define the cost of a sequence differently:
For each position , , proceed as follows:
- If , then the cost will increase by
- If , then the cost will decrease by
- If , then the cost becomes
Given numbers, , , , , , and questions of the form:
What is the cost of the subarray ?
The positions of the subarray , , , , , do not have the same positions in the initial sequence compared to the subarray . For example:
Let , and the sequence 1 3 3 4 2 1 9
, the subarray 3 4 2 1
has positions 3
, 4
, 5
, and 6
in the initial sequence, but in the subarray they are in positions 2
, 3
, 4
, and 5
.
Input data
The first line of the input file sircost.in
contains the natural number . The second line contains natural numbers representing the sequence , , , . The third line contains the natural number . The next lines contain two natural numbers , , representing the endpoints of the -th subarray.
Output data
The first lines of the output file sircost.out
will contain the answers for the queries.
Constraints and clarifications
- ;
- ;
# | Points | Constraints |
---|---|---|
1 | 13 | |
2 | 14 | , |
3 | 12 | The elements of the sequence are pairwise distinct |
4 | 9 | |
5 | 15 | |
6 | 37 | No other constraints |
Example 1
sircost.in
7
1 3 3 4 2 1 9
2
1 4
3 7
sircost.out
3
0
Explanation
For the subarray :
- , the cost becomes
- , the cost becomes
- , the cost becomes
For the subarray :
- , the cost becomes
- , the cost becomes
- , the cost becomes
- , the cost becomes