배열의 값을 하나씩 갱신할 때마다 버블 정렬에 필요한 패스 수를 구한다. 이 값은 각 원소가 왼쪽으로 밀린 거리의 최댓값에 1을 더한 것과 같다.
어려움8세그먼트 트리이분 탐색정렬구현아직 제출이 없습니다시간 제한5초메모리 제한512 MBBubble sort is an algorithm to sort a sequence. Let’s say we are going to sort a sequence A0, A1, . . . , AN−1 of length N in non-decreasing order. Bubble sort swaps two adjacent numbers when they are not in the correct order. Swaps are done by repeatedly passing through the sequence. Precisely speaking, in a pass, we swap Ai and Ai+1 if Ai > Ai+1, for i = 0, 1, . . . , N − 2 in this order. It is known that any sequence can be sorted in non-decreasing order by some passes. For a sequence A, we define the number of passes by bubble sort as the number of passes needed to sort A using the above algorithm.
JOI-kun has a sequence A of length N. He is going to process Q queries of modifying values of A. To be specific, in the (j + 1)-th query (0 ≤ j ≤ Q − 1), the value of AXj is changed into Vj.
JOI-kun wants to know the number of passes by bubble sort for the sequence after processing each query.
Given a sequence A = {1, 2, 3, 4} of length N = 4 and Q = 2 queries: X = {0, 2}, V = {3, 1}.
Bubble sort for A = {3, 2, 3, 4}:
Hence, the number of passes by bubble sort is 1 for A = {3, 2, 3, 4}.
Bubble sort for A = {3, 2, 1, 4}:
Hence, then number of passes by bubble sort is 2 for A = {3, 2, 1, 4}.