310727: CF1876G. Clubstep

Memory Limit:1024 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

G. Clubsteptime limit per test3 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard output

There is an extremely hard video game that is one of Chaneka's favourite video games. One of the hardest levels in the game is called Clubstep. Clubstep consists of $n$ parts, numbered from $1$ to $n$. Chaneka has practised the level a good amount, so currently, her familiarity value with each part $i$ is $a_i$.

After this, Chaneka can do several (possibly zero) attempts on Clubstep. In each attempt, she dies on one of the $n$ parts. If an attempt dies on part $p$, that means it only successfully passes through every part $k$ for all $1 \leq k \leq p-1$ and it does not reach any part $k$ for all $p+1 \leq k \leq n$. An attempt that dies on part $p$ takes $p$ seconds.

It is known that Chaneka improves much more on the part she dies on than anything else. It is also known that during an attempt, Chaneka does not get to practise that much on the parts she does not reach. So, the effect of an attempt that dies on part $p$ is as follows:

  • Chaneka's familiarity value with part $p$ increases by $2$.
  • Chaneka's familiarity value with each part $k$ for all $1 \leq k \leq p-1$ increases by $1$.
There will be $q$ questions. For the $j$-th question, you are given three integers $l_j$, $r_j$, and $x_j$. Then, you are asked to find out the minimum time (in seconds) for Chaneka to make it such that the familiarity value for every part $p$ ($l_j \leq p \leq r_j$) is at least $x_j$.

Note that each question is independent, so the attempt Chaneka does on a question does not affect the familiarity values of any other questions.

Input

The first line contains a single integer $n$ ($1 \leq n \leq 3\cdot10^5$) — the number of parts in Clubstep.

The second line contains $n$ integers $a_1,a_2,a_3,\ldots,a_n$ ($1\leq a_i\leq10^9$) — Chaneka's familiarity value with each part.

The third line contains a single integer $q$ ($1\leq q\leq3\cdot10^5$) — the number of questions.

The $j$-th of the next $q$ lines contains three integers $l_j$, $r_j$, and $x_j$ ($1\leq l_j\leq r_j\leq n$; $1\leq x_j\leq10^9$) — the description of the $j$-th question.

Output

Output $q$ lines with an integer in each line. The integer in the $j$-th line represents the minimum time (in seconds) for Chaneka to make it such that the familiarity value for every part $p$ ($l_j \leq p \leq r_j$) is at least $x_j$.

ExampleInput
5
1 3 2 1 2
3
1 5 5
2 4 5
3 3 1
Output
15
11
0
Note

For the $1$-st question, one possible strategy is to do the following:

  1. Do $1$ attempt that dies on part $1$. This takes $1$ second. The familiarity values become $[3, 3, 2, 1, 2]$.
  2. Do $1$ attempt that dies on part $4$. This takes $4$ seconds. The familiarity values become $[4, 4, 3, 3, 2]$.
  3. Do $2$ attempts that die on part $5$. This takes $10$ seconds. The familiarity values become $[6, 6, 5, 5, 6]$.

The total time taken (in seconds) is $1+4+10=15$.

For the $2$-nd question, one possible strategy is to do the following:

  1. Do $1$ attempt that dies on part $3$. This takes $3$ seconds. The familiarity values become $[2, 4, 4, 1, 2]$.
  2. Do $2$ attempts that die on part $4$. This takes $8$ seconds. The familiarity values become $[4, 6, 6, 5, 2]$.

The total time taken (in seconds) is $3+8=11$.

Output

题目大意:
有一个名为 Clubstep 的游戏关卡,分为 n 个部分,编号从 1 到 n。玩家 Chaneka 每个部分的熟悉度分别为 a_i。每次尝试,Chaneka 在某个部分失败,此次尝试对熟悉度的影响为:失败部分 +2,之前所有部分 +1。有 q 个问题,每个问题给定三个整数 l_j, r_j, x_j,要求计算 Chaneka 使每个部分 p (l_j≤p≤r_j) 的熟悉度至少达到 x_j 所需的最短时间。

输入输出数据格式:
输入:
- 第一行:一个整数 n (1≤n≤3×10^5) — Clubstep 的部分数量。
- 第二行:n 个整数 a_1,a_2,a_3,…,a_n (1≤a_i≤10^9) — Chaneka 每个部分的熟悉度。
- 第三行:一个整数 q (1≤q≤3×10^5) — 问题数量。
- 接下来 q 行:每行三个整数 l_j, r_j, x_j (1≤l_j≤r_j≤n; 1≤x_j≤10^9) — 第 j 个问题的描述。

输出:
- q 行,每行一个整数,表示 Chaneka 使每个部分 p (l_j≤p≤r_j) 的熟悉度至少达到 x_j 所需的最短时间。题目大意: 有一个名为 Clubstep 的游戏关卡,分为 n 个部分,编号从 1 到 n。玩家 Chaneka 每个部分的熟悉度分别为 a_i。每次尝试,Chaneka 在某个部分失败,此次尝试对熟悉度的影响为:失败部分 +2,之前所有部分 +1。有 q 个问题,每个问题给定三个整数 l_j, r_j, x_j,要求计算 Chaneka 使每个部分 p (l_j≤p≤r_j) 的熟悉度至少达到 x_j 所需的最短时间。 输入输出数据格式: 输入: - 第一行:一个整数 n (1≤n≤3×10^5) — Clubstep 的部分数量。 - 第二行:n 个整数 a_1,a_2,a_3,…,a_n (1≤a_i≤10^9) — Chaneka 每个部分的熟悉度。 - 第三行:一个整数 q (1≤q≤3×10^5) — 问题数量。 - 接下来 q 行:每行三个整数 l_j, r_j, x_j (1≤l_j≤r_j≤n; 1≤x_j≤10^9) — 第 j 个问题的描述。 输出: - q 行,每行一个整数,表示 Chaneka 使每个部分 p (l_j≤p≤r_j) 的熟悉度至少达到 x_j 所需的最短时间。

加入题单

上一题 下一题 算法标签: