Great City Saint Petersburg

시간 제한5초메모리 제한2048 MB

요약
담장 사이에 고이는 빗물의 양을 구하고, 구간 높이 증가가 일어날 때마다 갱신된 총량을 출력하는 문제입니다.
난이도

어려움10점 중 8점

유형
배열, 세그먼트 트리, 누적 합, 구현
정답자
아직 제출이 없습니다

문제

Saint Petersburg is the most beautiful city in the world unless it is raining. For the sake of this problem, we will assume it is raining every single day.

One of the streets in Saint Petersburg has an unusual shape --- it is a narrow stripe of nn sections 11 meter long each, where section ii is at the height a_ia\_i meters from the ground. The stripe is 11 meter deep and bounded on the front and on the back by incredibly high buildings. Because of this, when it is raining, a certain amount of rain will accumulate, unable to flow out of the street from either its leftmost or rightmost end. Given the heights a_1a\_1, a_2a\_2, \ldots, a_na\_n, you need to determine the amount of rain (in cubic meters) which will accumulate on the street.

Moreover, your colleagues from the metropolitan construction company will be visiting for qq days and on day ii they will be laying asphalt on all sections from l_il\_i to r_ir\_i inclusive, thus increasing the height of each section l_il\_i, l_i+1l\_i+1, \ldots, r_ir\_i by 11 meter. You need to determine the total amount of water which accumulates on the street before the construction works, and also after every single day of the construction works.

입력

The first line contains the number of blocks nn and the number of construction events qq (1≤n,q≤2⋅1051 \le n, q \le 2 \cdot 10^5). The second line contains nn integers a_1,a_2,…,a_na\_1, a\_2, \ldots, a\_n (1≤a_i≤1091 \le a\_i \le 10^9) --- the height of each section before all the events. Each of the following qq lines contains a pair of integers l_il\_i, r_ir\_i (1≤l_i≤r_i≤n1 \le l\_i \le r\_i \le n), denoting the construction work from l_il\_i to r_ir\_i inclusive.

출력

Print q+1q+1 integers --- the amount of water on the street before all updates, and also after every update.

힌트

The picture illustrates the amount of water accumulating on the street in the first example.

예제2

  1. 예제 1

    입력
    5 4
    3 2 1 2 3
    1 5
    2 4
    1 2
    5 5
    
    예상 출력
    4
    4
    1
    1
    3
    
  2. 예제 2

    입력
    7 3
    1 1000000000 1 1 1 1000000000 1
    1 3
    4 5
    5 7
    
    예상 출력
    2999999997
    2999999996
    2999999994
    2999999996