Splitting Haybales

시간 제한2초메모리 제한1024 MB

요약
각 질의마다 부분 배열에서 현재 더 적게 가진 소에게 건초를 차례로 주며, 시작 차이 x일 때 최종 차이를 구한다.
난이도

어려움10점 중 8점

유형
이분 탐색, 그리디, 누적 합, 정렬
정답자
아직 제출이 없습니다

문제

Farmer John wants to fairly split haybales between his two favorite cows Bessie and Elsie. He has NN ( 1≤N≤2⋅1051\le N\le 2\cdot 10^5) haybales sorted in non-increasing order, where the ii-th haybale has a_ia\_i units of hay (2⋅105≥a_1≥a_2≥⋯≥a_N≥12\cdot 10^5\ge a\_1\ge a\_2 \ge \dots \ge a\_N \ge 1).

Farmer John is considering splitting a contiguous range of haybales a_l,…,a_ra\_l, \dots, a\_r between Bessie and Elsie. He has decided to process the haybales in order from ll to rr, and when processing the ii-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 QQ (1≤Q≤2⋅1051\le Q\le 2\cdot 10^5) queries, each with three integers l,r,xl,r,x (1≤l≤r≤N1\le l\le r\le N, ∣x∣≤109|x|\le 10^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.

입력

First line contains NN.

Second line contains a_1…a_Na\_1\dots a\_N.

Third line contains QQ.

Next QQ lines contain l,r,xl, r, x.

출력

Output QQ lines, containing the answer for each query.

예제2

  1. 예제 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
    
    예상 출력
    1
    2
    3
    -2
    -1
    0
    1
    2
    -1
    0
    -1
    0
    1
    0
    1
    
  2. 예제 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
    
    예상 출력
    16
    12
    7
    4
    1
    2
    1