Permutation and Queries

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

요약
순열에서 두 원소를 교환할 때마다 모든 쌍 i, j에 대한 |i j| * |p_i p_j|의 최솟값을 갱신해 출력한다.
난이도

어려움10점 중 9점

유형
수학, 정렬, 완전 탐색, 기하
정답자
아직 제출이 없습니다

문제

You are given a permutation p_1,p_2,…,p_np\_1, p\_2, \ldots, p\_n of size nn. Calculate the value f(p)=min⁡_i≠j∣i−j∣⋅∣p_i−p_j∣.f(p) = \min\limits\_{i \neq j} |i - j| \cdot |p\_i - p\_j|\text{.} 

You are also given qq queries. The ii-th query consists of two indices a_ia\_i and b_ib\_i. You should swap the elements at these positions (swap p_a_ip\_{a\_i} and p_b_ip\_{b\_i}), and then recalculate the value f(p)f(p). Note that the changes persist between queries: after ii-th query, there are ii swaps made.

A permutation of size nn is a sequence of nn distinct integers from 11 to nn.

입력

The first line contains two integers: the permutation size nn (2≤n≤1052 \le n \le 10^5) and the number of queries qq (1≤q≤1051 \le q \le 10^5).

The second line describes the permutation pp.

Each of the next qq lines describes a query. The ii-th of these lines contains two integers a_ia\_i and b_ib\_i (1≤a_i,b_i≤n1 \le a\_i, b\_i \le n; a_i≠b_ia\_i \ne b\_i): the indices of elements you should swap.

출력

Print q+1q + 1 lines: the value f(p)f(p) before all queries and after each of the qq queries.

예제1

  1. 예제 1

    입력
    6 5
    2 4 1 6 3 5
    1 2
    3 5
    1 2
    5 3
    5 6
    
    예상 출력
    2
    1
    1
    1
    2
    1