If Shelly chooses a range [a,b][a, b], the amount of Tang he can eat each day is the minimum value of AiA_i in this range. Instead of finding the optimal range of the days, let’s find the optimal amount of Tang AiA_i per day. We can find the range [p,q][p,q] where AiA_i is minimum by maintaining a monotonous stack. So, the naive approach will be: iterate from LL to RR and for each ii, calculate the total amount of Tang he can drink if he treats AiA_i as the Tang amount each day. The total amount will be = Ai×(max(L,p)min(R,q)+1)A_i \times (max(L, p) - min(R, q) +1). The maximum of them will be the answer. Now, this approach won’t surely fit in time limit. We need to optimize it.

We can observe that, for each query L,RL, R, we can classify the values AiA_i in some types:

  1. LpqRL \leq p \leq q \leq R. For this type, we can precalculate the total amount Ai×(qp+1)A_i \times (q-p+1) and perform range maximum query using segment tree or similar type data structures.

  2. p<L,qRp<L, q\leq R. For this type, the amount of Tang Shelly can drink is =Ai×(qL+1)=Ai×qAi×(L1)=A_i \times (q-L+1) = A_i\times q - A_i \times (L-1). The only variable part here is L1L-1. It is similar to the line equation y=m×x+cy=m\times x + c. So, we can apply CHT (Convex Hull Trick) to find the maximum amount in this case. Our query xx will be L1L-1.

  3. Lp,q>RL\leq p, q>R. It is similar to type 22.

  4. p<L,q>Rp<L, q>R. The amount of Tang Shelly can drink is =Ai×(RL+1)=A_i \times (R-L+1). As the full range is being covered, we can just find the maximum AiA_i and then multiply it with the necessary term. Range maximum can be found by using a segment tree or similar type data structures.

The problem is how we can apply the proper operation on the proper types efficiently. To do that, we can process the queries offline.

For type 11, we can iterate from nn to 11. When we are on the iith index, we will process those AiA_i that have p=ip=i. We will update the value Ai×(qp+1)A_i \times (q-p+1) on the position qq. After that, we can process those queries that have L=iL=i. We need to perform the max query on the range [i,R][i, R]. As the indexes j<ij<i aren’t processed yet, the already processed pp are L\geq L.

For type 44, we can iterate from nn to 11 like before. When we are on the iith index, we will process those AiA_i that have q=iq=i. We will update the value AiA_i on the position pp. Before that, we need to process those queries that have R=iR=i. We need to perform the max query on the range [1,L1][1, L-1]. As the indexes j>ij>i are processed before the query, the already processed qq are >R> R.

For type 22, we will iterate from 11 to nn. When we are on the iith index, we will process those AiA_i that have p=ip=i. We need to maintain such a segment tree that contains a Dynamic CHT on every node. So, when we perform an update on the position qq, the necessary values will be inserted into the Dynamic CHT of each updatable node. The updatable nodes are the nodes of the ranges in the segment tree that include qq. But before this update, we must process those queries that have L=iL=i. We need to perform the max query on the [i,R][i, R] range. Since the segment tree has Dynamic CHT on each node, we can answer it easily by performing queries on the CHT when we are on the valid nodes. As the indexes j<ij<i are processed before the query, the already processed pp are <L< L. We can get the values for type 33 in a similar fashion.

After we process all the types, we can easily get our desired answer for each query.

Time complexity: O(n×log2(n))\mathcal{O}(n\times log^2(n))

Statistics

67% Solution Ratio
tasmeemrezaEarliest, Jan '23
ChatGPTFastest, 0.3s
ChatGPTLightest, 48 MB
ChatGPTShortest, 5340B
Toph uses cookies. By continuing you agree to our Cookie Policy.