Election Queries

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

요약
투표가 갱신될 때마다, 진 부분집합 S에서 최빈 득표자 x를, 여집합에서 최빈 득표자 y를 골라 |x - y|를 최대로 만드는 값을 구한다.
난이도

어려움10점 중 9점

유형
세그먼트 트리, 구현, 수학, 완전 탐색
정답자
아직 제출이 없습니다

문제

Farmer John has NN (2≤N≤2⋅1052 \leq N \leq 2 \cdot 10^5) cows numbered from 11 to NN. An election is being held in FJ's farm to determine the two new head cows in his farm. Initially, it is known that cow ii will vote for cow a_ia\_i (1≤a_i≤N1 \leq a\_i \leq N).

To determine the two head cows, FJ will hold his election in the following process:

  • Choose an arbitrary subset SS of his cows that contains at least one cow but not all cows. FJ is able to choose cow xx as the first head cow if its votes appear most frequently among all votes cast by cows in SS.
  • FJ is able to choose cow yy as the second head cow if its votes appear most frequently among votes cast by cows not in SS.
  • For a fixed subset SS, FJ denotes the diversity between his head cows as ∣x−y∣|x - y|. As FJ does not like having leaders with similar numbers, he wants to choose SS such that the diversity is maximized. Note that if FJ is not able to choose two distinct head cows, then the diversity is 00.

However, some cows keep changing their minds, and FJ may have to rerun the election many times! Therefore, he asks you QQ (1≤Q≤1051 \leq Q \leq 10^5) queries. In each query, a cow changes their vote. After each query, he asks you for the maximum possible diversity among his new head cows.

입력

The first line contains NN and QQ.

The following line contains a_1,a_2,…,a_Na\_1, a\_2, \ldots, a\_N.

The following QQ lines contain two integers ii and xx, representing the update a_i=xa\_i = x (1≤i,x≤N1 \leq i, x \leq N).

출력

Output QQ lines, the ii'th of which is the maximum possible diversity after the first ii queries.

예제2

  1. 예제 1

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

    입력
    8 5
    8 1 4 2 5 4 2 3
    7 4
    8 4
    4 1
    5 8
    8 4
    
    예상 출력
    4
    4
    4
    7
    7