#P1015. Problem 2. Splitting Haybales

Problem 2. Splitting Haybales

当前没有测试数据。

一、Problem Name

Splitting Haybales

二、Problem Description

Farmer John wants to fairly split haybales between his two favorite cows Bessie and Elsie. He has NN (1N21051\leq N\leq2\cdot10^5) haybales sorted in non-increasing order, where the iith haybale has aia_i units of hay (2105a1a2aN12\cdot10^5\geq a_1\geq a_2\geq\cdots\geq a_N\geq1).

Farmer John is considering splitting a contiguous range of haybales al,,ara_l,\cdots,a_r between Bessie and Elsie. He has decided to process the haybales in order from ll to rr, and when processing the iith haybale he will give it to the cow who currently has less hay (if it is a tie, he will give it to Bessie).

You are given QQ (1Q21051\leq Q\leq2\cdot10^5) queries, each with three integers l,r,xl,r,x (1lrN1\leq l\leq r\leq N, x109\vert x\vert\leq10^9). For each query, output how many more units of hay Bessie will have than Elsie after processing haybales ll to rr, if Bessie starts with xx more units than Elsie. Note that this value is negative if Elsie ends up with more haybales than Bessie.

三、Input Format (input arrives from the terminal / stdin)

  1. First line contains NN.
  2. Second line contains a1aNa_1\cdots a_N.
  3. Third line contains QQ.
  4. Next QQ lines contain l,r,xl,r,x.

四、Output Format (print output to the terminal / stdout)

Output QQ lines, containing the answer for each query.

五、Sample Input and Output

Sample Input 1

2
3 1
15
1 1 -2
1 1 -1
1 1 0
1 1 1
1 1 2
1 2 -2
1 2 -1
1 2 0
1 2 1
1 2 2
2 2 -2
2 2 -1
2 2 0
2 2 1
2 2 2

Sample Output 1

1
2
3
-2
-1
0
1
2
-1
0
-1
0
1
0
1

Explanation: For the 1st query, Elsie starts with 2 more hay than Bessie. Then, after processing haybale 1, Bessie will receive 3 hay, and Bessie will have 1 more hay than Elsie. For the 3rd query, Elsie and Bessie start with the same number of hay. After processing haybale 1, Bessie will receive 3 hay, and Bessie will have 3 more hay than Elsie. For the 9th query, Bessie starts with 1 more hay than Elsie, then after processing the 1st haybale, has 2 less hay than Elsie, and after processing the 2nd haybale, has 1 less hay than Elsie.

Sample Input 2

5
4 4 3 1 1
7
1 1 20
1 2 20
1 5 20
1 1 0
1 5 0
1 4 0
3 5 2

Sample Output 2

16
12
7
4
1
2
1

Explanation: In the 5th query, there are 5 haybales to process. Bessie receives 4 hay, then Elsie receives 4 hay, then Bessie receives 3 hay, then Elsie receives 1 hay, then Elsie receives 1 hay.

六、Scoring Rules

  1. Input 3: Q100Q\leq100.
  2. Inputs 4 - 6: At most 100 distinct aia_i.
  3. Inputs 7 - 22: No additional constraints.

Problem credits: Benjamin Qi.