Lanterns

아직 제출이 없습니다시간 제한3초메모리 제한1024 MB

문제

Farmer John has taken his herd of cows on a hiking excursion in the Alps! After a while, the sky got dark and the excursion was over. However, some cows remained trapped all along the mountain range, and it is up to John to rescue all of them!

The mountain range that the cows are currently traversing can be represented by a series of nn vertices in a vertical 2D plane. We will call these vertices "peaks". The peaks are numbered from 11 to nn, in order. Peak ii has coordinates (i,h_i)(i, h\_i). The value h_ih\_i denotes the altitude of peak ii. It is guaranteed that h_1,h_2,,h_nh\_1, h\_2, \dots, h\_nform a permutation of 1n1 \dots n. (That is, for each j=1,,nj = 1, \dots, n, we have h_i=jh\_i = j for exactly one i1,,ni \in \\{1, \dots, n\\}.)

For each ii (1i<n1 \le i < n), peaks ii and i+1i+1 are connected by a straight line segment.

As it is nighttime, John cannot travel to any part of the mountain unless he has at least one functioning lantern with him. Luckily, there are kk lanterns available for purchase. For each jj (1jk1 \le j \le k), lantern jj can be bought at peak p_jp\_j for c_jc\_j francs.

Unfortunately, lantern jj only works when John's current altitude is within the range \[a_j,b_j]\[a\_j, b\_j]. In other words, whenever John's current altitude is strictly less than a_ja\_j or strictly greater than b_jb\_j, lantern jj does not work. Note that lanterns do not break when they leave their range. For example, when John's altitude exceeds b_jb\_j, lantern jj will stop working, but as soon as John returns to altitude b_jb\_j the lantern will start working again.

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

  • He can buy one of the lanterns that are available at peak pp. Once he buys a lantern, 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 must never move without a working lantern. He can only walk between two adjacent peaks if at each moment of the walk at least one of the lanterns he already owns will work. (It does not have to be the same lantern during the entire walk.)

For example, suppose that Farmer John is currently located at a peak with altitude 44 and wishes to walk to an adjacent peak with altitude 11. If John has lanterns that function in the altitude ranges \[1,3]\[1, 3] and \[3,4]\[3, 4], this will allow him to walk from one peak to the other.

However, if John has lanterns that only function in the ranges \[1,1]\[1, 1] and \[2,5]\[2, 5], then John will not be able to walk between these two peaks yet: e.g., none of his lanterns will work at altitude 1.471.47.

Your task is to determine the answers to multiple independent questions.

For each 1jk1 \le j \le k satisfying a_jh_p_jb_ja\_j \le h\_{p\_j} \le b\_j, suppose that John begins his search at peak p_jp\_j by buying lantern jj. In order to search the entire mountain range, he must then visit every one of the nn peaks at least once by repeatedly performing one of three actions above. For each of these jj, determine the minimum total number of francs that John needs to spend in order to search the entire mountain range. (This cost includes the initial purchase of lantern jj.)

입력

The first line contains nn and kk (1n20001 \le n \le 2000, 1k20001 \le k \le 2000) – the number of mountain peaks and available lanterns, respectively.

The second line contains nn space-separated integers h_1,h_2,,h_nh\_1, h\_2, \dots, h\_n (1h_in1 \le h\_i \le n): the altitude of each peak. It is guaranteed that the values h_ih\_i are a permutation of 11 through nn.

The jj-th of the next kk lines contains four space-separated integers p_jp\_j, c_jc\_j, a_ja\_j, and b_jb\_j (1p_jn1 \le p\_j \le n, 1c_j1061 \le c\_j \le 10^6, 1a_jb_jn1 \le a\_j \le b\_j \le n) – the mountain peak on which lantern jj can be purchased, its cost and operational range, respectively.

제한

For each jj (1jk1 \le j \le k) output a single line:

  • If h_p_jh\_{p\_j} is outside the range \[a_j,b_j]\[a\_j, b\_j], output 1-1.
  • Else, if John cannot search the entire mountain range by first buying lantern jj, output 1-1.
  • Else, output the minimum total number of francs that John needs to spend in order to search the entire mountain range if he begins by buying lantern jj.

힌트

If John starts by buying lantern 11 on peak 33, he can then perform the following sequence of actions:

  • walk left twice to peak 11
  • buy lantern 22
  • walk right to peak 44
  • buy lantern 33
  • walk right to peak 77

At this point, John has visited each peak at least once and he spent a total of 1+2+4=71 + 2 + 4 = 7 francs.

John can't start by buying lantern 22, 66, or 77, since they don't function at the altitude at which they can be bought. Thus, the answers for each of these lanterns is 1-1.

If John starts by buying lantern 33 or 44, he can then visit all peaks without buying additional lanterns.

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

If John starts by buying lantern 88, he will be stuck at peak 77. Even if he also purchases lantern 77 as well, he still won't be able to walk from peak 77 to peak 66.