# Practice on Toph

Participate in exhilarating programming contests, solve unique algorithm and data structure challenges and be a part of an awesome community.

Participate in exhilarating programming contests, solve unique algorithm and data structure challenges and be a part of an awesome community.

Let’s get into the problem directly without much description this time :D .

You are given an array **A** of **N** integers. The elements of the array are **A**_{1}, **A**_{2}, **A**_{3} … **A**_{N} . You are also given **Q** queries to perform on this array **A**. In each query, you will be given three integers **L, R, X** where **[L, R]** denotes a sub-segment of the array **A** starting at **A**_{L} and ending at **A**_{R}. You have to find the minimum number of operations needed to make all values of the sub-segment, **[L, R]** of the array **A** equal to **X** i.e. **A**_{L} = **A**_{L+1} = **A**_{L+2} = … = **A**_{R} = **X**.

There are two types of operations. You can apply any of the two operations any number of times.

Two types of operations are described below-**Operation 1:** you can choose any index i **(1 ≤** i **≤ N)** of the array **A** and then perform **A**_{i} **=** **A**_{i} **+ 1**.**Operation 2:** you can choose any index i **(1 ≤** i **≤ N)** of the array **A** and then perform **A**_{i} **=** **A**_{i} **- 1**.

**Note: Every query is independent.**

First line contains an integer **T(1 ≤ T ≤ 5)**, the number of test case.

Every test case starts with an integer **N(1 ≤ N ≤ 10 ^{5})**, the number of elements of the array

Next line will contain the elements of the array

Next line will contain an integer

Next Q lines will contain three integers

There are at most

Original Constraints.

For each case, print the case number in a single line.

Then for every query print the minimum number of operations needed to make all values of the sub-segment, **[L, R]** of the array **A** equal to **X**.

Input | Output |
---|---|

1 3 4 5 6 2 1 3 6 1 3 4 | Case 1: 3 3 |

In the **1 ^{st}** query, you have to apply

In the **2 ^{nd}** query, you have to apply

34% Solution Ratio

prodip_bsmrstuEarliest,

EgorKulikovFastest, 0.3s

prishan076Lightest, 12 MB

MursaleenShortest, 1682B

Login to submit

Subtask 1: We will solve subtask 1 with bruteforce. So, for all index i in the sub-segment [L - R]...