RoAlgo PreOJI 2024 IX | sircost

This was the problem page during the contest. Access the current page here.
Time limit: 0.15s Memory limit: 64MB Input: sircost.in Output: sircost.out

Task

We define the cost of a sequence differently:

For each position ii, 1i<N1 \leq i < N, proceed as follows:

  • If ai<ai+1a_i < a_{i+1}, then the cost will increase by ii
  • If ai>ai+1a_i > a_{i+1}, then the cost will decrease by ii
  • If ai=ai+1a_i = a_{i+1}, then the cost becomes 00

Given NN numbers, a1a_1, a2a_2, a3a_3, \dots, aNa_N, and QQ questions of the form:

What is the cost of the subarray [st,dr][st, dr]?

Attention!\bold{Attention!} The positions of the subarray asta_{st}, ast+1a_{st+1}, ast+2a_{st+2}, \dots, adr1a_{dr-1}, adra_{dr} do not have the same positions in the initial sequence compared to the subarray [st,dr][st, dr]. For example:

Let N=7N=7, 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 [2,6][2, 6] they are in positions 2, 3, 4, and 5.

Input data

The first line of the input file sircost.in contains the natural number NN. The second line contains NN natural numbers representing the sequence a1a_1, a2a_2, \dots, aNa_N. The third line contains the natural number QQ. The next QQ lines contain two natural numbers stist_i, dridr_i, representing the endpoints of the ii-th subarray.

Output data

The first QQ lines of the output file sircost.out will contain the answers for the QQ queries.

Constraints and clarifications

  • 1N,Q21051 \leq N, Q \leq 2 \cdot 10^5;
  • 0ai1090 \leq a_i \leq 10^9;
# Points Constraints
1 13 a1=a2=a3=...=aNa_1=a_2=a_3=...=a_N
2 14 aiai+1a_i \not= a_{i+1}, 1i<N1 \leq i < N
3 12 The elements of the sequence are pairwise distinct
4 9 1N,Q2001 \leq N, Q \leq 200
5 15 200<N,Q2000200 < N, Q \leq 2000
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 [1,4][1, 4]:

  • 1<31<3, the cost becomes 11
  • 3=33=3, the cost becomes 00
  • 3<43<4, the cost becomes 33

For the subarray [3,7][3, 7]:

  • 3<43<4, the cost becomes 11
  • 4>24>2, the cost becomes 1-1
  • 2>12>1, the cost becomes 4-4
  • 1<91<9, the cost becomes 00

Log in or sign up to be able to send submissions!