#P9312. [EGOI 2021] Lanterns / 灯笼

[EGOI 2021] Lanterns / 灯笼

Background

Day 1 Problem D.

The statement is translated from EGOI2021 lantern.

Problem Description

Farmer John took his herd of cows on a hike in the Alps. After some time, it got dark and the hike ended. However, some cows are still trapped in different parts of the mountains, and now John needs to rescue them.

The mountains the herd is traveling through are represented by nn points in the Cartesian plane. We call these points “peaks”. The peaks are numbered from 11 to nn in order. Peak ii has coordinates (i,hi)(i,h_i). The value hih_i represents the altitude of peak ii. It is guaranteed that h1,h2,,hnh_1,h_2,\ldots,h_n form a permutation of 1n1\sim n.

Peak ii and peak i+1i+1 are connected by a line segment.

Since it is night, John must carry at least one lantern that is working properly in order to move in the mountains. Fortunately, there are kk lanterns available for purchase. Lantern jj can be bought at peak pjp_j for cjc_j francs.

Unfortunately, lantern jj works properly only when John’s altitude is in [aj,bj][a_j,b_j]. In other words, if John’s altitude is strictly lower than aja_j or strictly higher than bjb_j, lantern jj will not work properly. Note that the lantern does not break when leaving this altitude range. For example, when John’s altitude exceeds bjb_j, lantern jj stops working, but as soon as John returns to altitude bjb_j, the lantern starts working again.

If John is currently at peak pp, he can do one of the following three actions:

  • He can buy a lantern sold at pp. After buying it, he can use it forever.
  • If p>1p > 1, he can walk to peak p1p-1.
  • If p<np < n, he can walk to peak p+1p+1.

John cannot move when he has no properly working lantern. He must ensure that at every moment there is at least one lantern working properly in order to move between two peaks. (During walking, it does not have to be the same lantern.)

For example, suppose Farmer John is currently on a peak with altitude 44 and wants to walk to a neighboring peak with altitude 11. If John has two lanterns with altitude ranges [1,3][1,3] and [3,4][3,4], he can walk from one peak to the other.

However, if John only has two lanterns with altitude ranges [1,1][1,1] and [2,5][2,5], he cannot walk between these two peaks. For instance, he has no lantern that can work properly at altitude 1.471.47.

You need to answer multiple independent queries.

For each 1jk1\le j\le k such that ajhpjbja_j\le h_{p_j}\le b_j, suppose John starts at peak pjp_j and buys lantern jj. To search the entire mountain range, he must repeatedly perform the three actions described above. For each jj, compute the minimum number of francs John must spend in order to search the entire mountain range. (This cost includes the initial purchase of lantern jj.)

Input Format

The first line contains two integers n,kn,k — the number of peaks and the number of lanterns.

The second line contains nn integers h1,h2,,hnh_1,h_2,\ldots,h_n, the altitude of each peak. It is guaranteed that hih_i form a permutation of 1n1\sim n.

The next kk lines each contain four integers pj,cj,aj,bjp_j,c_j,a_j,b_j — the selling position, price, and altitude range of lantern jj.

Output Format

For each 1jk1\le j\le k, output one line:

  • If hpjh_{p_j} is not in [aj,bj][a_j,b_j], output 1-1.
  • Otherwise, if John cannot search the entire mountain range when he initially buys lantern jj, output 1-1.
  • Otherwise, output the minimum cost in this case.
7 8
4 2 3 1 5 6 7
3 1 2 4
1 2 1 3
4 4 1 7
6 10 1 7
6 20 6 6
6 30 5 5
7 40 1 6
7 50 7 7
7
-1
4
10
30
-1
-1
-1

Hint

Sample Explanation

If John starts from peak 33 and buys lantern 11, he can do the following:

  • Walk left twice to reach peak 11.
  • Buy lantern 22.
  • Walk right to peak 44.
  • Buy lantern 33.
  • Walk right to peak 77.

Now John has visited every peak at least once, and he spent 1+2+4=71+2+4=7 francs.

John cannot start by buying lanterns 2,6,72,6,7, because they do not work at the altitude where they are purchased. Therefore, their answers are 1-1.

If John starts by buying lantern 33 or 44, he can visit every peak without buying any other lantern.

If John starts by buying lantern 55, he must buy lantern 44.

If John starts by buying lantern 88, he will get stuck at peak 77. Even if he buys lantern 77, he still cannot move from peak 77 to peak 66.


Constraints

For all testdata, 1n,k2×1031\le n,k\le 2\times 10^3, 1hin1\le h_i\le n and hih_i form a permutation of 1n1\sim n, 1pjn1\le p_j\le n, 1cj1061\le c_j\le 10^6, 1ajbjn1\le a_j\le b_j\le n.

  • Subtask 1 (99 points): n20n\le 20, k6k\le 6.
  • Subtask 2 (1212 points): n,k70n,k\le 70.
  • Subtask 3 (2323 points): n,k300n,k\le 300, hi=ih_i=i.
  • Subtask 4 (1616 points): n,k300n,k\le 300.
  • Subtask 5 (4040 points): no special constraints.

Translated by ChatGPT 5