Finding Treasure

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

요약
양 끝 칸에 보물이 고정된 1차원 격자에서 칸을 켜고 끌 때마다, 각 칸에서 왼쪽과 오른쪽 가장 가까운 보물까지의 거리 곱을 모두 더한 값을 구한다.
난이도

보통10점 중 7점

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

문제

Morgan is designing a treasury for a treasure hunt event. The treasury can be represented as one dimensional grid with N+2N + 2 cells, numbered from 00 to N+1N + 1. Each cell can be empty or contain a treasure. At first, cell 00 and cell N+1N + 1 contain a treasure, while the other cells are currently empty.

While experimenting with the treasury design, Morgan does QQ updates, numbered from 11 to QQ, to his treasury. In update ii, he wants to change cell A_iA\_i. If cell A_iA\_i is empty, he will put a treasure on cell A_iA\_i. If cell A_iA\_i contains a treasure, he will remove the treasure from cell A_iA\_i and the cell becomes empty. It is guaranteed that 1≤A_i≤N1 ≤ A\_i ≤ N for all 1≤i≤Q1 ≤ i ≤ Q, which implies cell 00 and cell N+1N + 1 always contain a treasure.

After each change, Morgan wonders how difficult his treasure hunt is. He defines the difficulty level of standing on cell xx as the multiplication of two values:

  • the distance from xx to the closest treasure on cell y≤xy ≤ x, and
  • the distance from xx to the closest treasure on cell y≥xy ≥ x.

The distance between two cells can be calculated as the absolute difference between the cell numbers. Then, the total difficulty level of his treasure hunt is defined as the sum of difficulty level of standing on cell xx for all 0≤x≤N+10 ≤ x ≤ N + 1.

Help Morgan to determine the total difficulty level of his treasure hunt after each update.

입력

Input begins with two integers NN QQ (1≤N≤100,0001 ≤ N ≤ 100\\, 000; 1≤Q≤100,0001 ≤ Q ≤ 100\\, 000) representing the size of the treasury room and the number of updates, respectively. Each of the next QQ lines contains an integer A_iA\_i (1≤A_i≤N1 ≤ A\_i ≤ N) representing the cell that Morgan wants to change in update ii.

출력

After each update that Morgan makes, output an integer in a single line representing the total difficulty level of his treasure hunts at that time.

예제2

  1. 예제 1

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

    입력
    10 14
    7
    2
    9
    5
    2
    9
    6
    7
    5
    8
    6
    8
    7
    10
    
    예상 출력
    66
    31
    23
    8
    23
    31
    30
    40
    55
    40
    88
    220
    66
    60