luogu#P5044. [IOI 2018] meetings 会议

[IOI 2018] meetings 会议

Background

This is an interactive problem, but here you should submit a complete program.

Problem Description

There are NN mountains arranged in a horizontal line, numbered from 00 to N1N-1 from left to right. The height of mountain ii is HiH_i (0iN10\leq i\leq N-1). Exactly one person lives on the top of each mountain.

You plan to hold QQ meetings, numbered from 00 to Q1Q-1. The participants of meeting jj (0jQ10\leq j\leq Q-1) are the people living on mountains from LjL_j to RjR_j (including LjL_j and RjR_j) (0LjRjN10\leq L_j\leq R_j\leq N-1). For this meeting, you must choose some mountain xx as the meeting location (LjxRjL_j\leq x\leq R_j). The cost of holding the meeting depends on your choice, and is computed as follows:

  • The cost for each participant coming from a mountain yy (LjyRjL_j\leq y\leq R_j) equals the maximum height among all mountains between xx and yy (including xx and yy). In particular, the cost for the participant from mountain xx is HxH_x, i.e. the height of mountain xx.

  • The cost of the meeting equals the sum of the costs of all its participants.

You want to hold each meeting with the minimum possible cost.

Note that all participants return to their own mountains after each meeting, so the cost of a meeting is not affected by previous meetings.

Input Format

The first line contains two positive integers NN and QQ, as described above.

The second line contains NN positive integers H0,H1,,HN1H_0,H_1,\cdots, H_{N-1}, representing the heights of the mountains.

Line 3+j3+j (0jQ10\leq j\leq Q-1) contains two integers Lj,RjL_j, R_j, describing the participant range of the meeting.

Output Format

Output QQ lines. Line 1+j1+j (0jQ10\leq j\leq Q-1) contains one integer CjC_j, the minimum possible cost of holding meeting jj.

4 2
2 4 3 5
0 2
1 3

10
12

3 3
2 1 2
0 0
0 1
0 2

2
3
5

5 1
1000000000 1000000000 1 1000000000 1000000000
0 4

4000000001

15 10
10 71 84 33 6 47 23 25 52 64 70 31 22 31 2
5 10
3 7
0 13
8 12
0 0
1 3
7 13
1 13
10 12
1 1

281
180
828
263
10
201
364
744
123
71

Hint

Explanation of Sample #1

Meeting j=0j=0 has Lj=0L_j=0 and Rj=2R_j=2, so it will be attended by the people living on mountains 00, 11, and 22. If mountain 00 is chosen as the meeting location, the cost of meeting 00 is computed as follows:

  • The cost for the participant from mountain 00 is max{H0}=2\max\lbrace H_0\rbrace=2.
  • The cost for the participant from mountain 11 is max{H0,H1}=4\max\lbrace H_0,H_1\rbrace=4.
  • The cost for the participant from mountain 22 is max{H0,H1,H2}=4\max\lbrace H_0,H_1,H_2\rbrace=4.
  • Therefore, the total cost of meeting 00 is 2+4+4=102+4+4=10.

It is not possible to hold meeting 00 at a lower cost, so the minimum cost of meeting 00 is 1010.

Meeting j=1j=1 has Lj=1L_j=1 and Rj=3R_j=3, so it will be attended by the people living on mountains 11, 22, and 33. If mountain 22 is chosen as the meeting location, the cost of meeting 11 is computed as follows:

  • The cost for the participant from mountain 11 is max{H1,H2}=4\max\lbrace H_1,H_2\rbrace=4.
  • The cost for the participant from mountain 22 is max{H2}=3\max\lbrace H_2\rbrace=3.
  • The cost for the participant from mountain 33 is max{H1,H2,H3}=5\max\lbrace H_1,H_2,H_3\rbrace=5.
  • Therefore, the total cost of meeting 11 is 4+3+5=124+3+5=12.

It is not possible to hold meeting 11 at a lower cost, so the minimum cost of meeting 11 is 1212.

Constraints

  • 1N750 0001\leq N\leq 750\space000
  • 1Q750 0001\leq Q\leq 750\space000
  • 1Hi1 000 000 0001\leq H_i\leq1\space000\space000\space000
  • 0LjRjN1(0jQ1)0\leq L_j\leq R_j\leq N-1(0\leq j\leq Q-1)
  • (Lj,Rj)(Lk,Rk)(0j<kQ1)(L_j,R_j)\neq(L_k,R_k)(0\leq j<k\leq Q-1)

Subtasks

  1. (4 points) N3000,Q10N\leq3000,Q\leq10.
  2. (15 points) N5000,Q5000N\leq5000,Q\leq5000.
  3. (17 points) $N\leq100\space000,Q\leq100\space000,H_i\leq2(0\leq i\leq N-1)$.
  4. (24 points) $N\leq100\space000,Q\leq100\space000,H_i\leq20(0\leq i\leq N-1)$.
  5. (40 points) No additional constraints.

Author

Riku Kawasaki (Japan).

Source

IOI 2018 D2T3.

Translated by ChatGPT 5