Farmer John wants to fairly split haybales between his two favorite cows Bessie and Elsie. He has N( 1≤N≤2⋅105) haybales sorted in non-increasing order, where the i-th haybale has aiunits of hay (2⋅105≥a1≥a2≥⋯≥aN≥1).
Farmer John is considering splitting a contiguous range of haybales al,…,ar between Bessie and Elsie. He has decided to process the haybales in order from l
to r, and when processing the i-th 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 Q(1≤Q≤2⋅105) queries, each with three integers l,r,x(1≤l≤r≤N
, |x|≤109). For each query, output how many more units of hay Bessie will have than Elsie after processing haybales l to r, if Bessie starts with x 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):
First line contains N.
Second line contains a1…aN.
Third line contains Q.
Next Q lines contain l,r,x.
OUTPUT FORMAT (print output to the terminal / stdout):
Output Q lines, containing the answer for each query.
SAMPLE INPUT:
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
2
3
-2
-1
0
1
2
-1
0
-1
0
1
0
1
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:
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:
16
12
7
4
1
2
1
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:
Input 3: Q≤100
Inputs 4-6: At most 100
distinct ai
Inputs 7-22: No additional constraints.
Problem credits: Benjamin Qi
扫码免费领取USACO试题答案+详细解析
咨询一对一备赛规划