luogu#P8939. [DTOI 2023] B. 去年 11 月卵梦蕾简易钨丝

[DTOI 2023] B. 去年 11 月卵梦蕾简易钨丝

Background

The large sample has been fixed.

Problem Description

Given a sequence {an}\{a_n\}, it supports two types of operations of the form opt x.

  1. 1 x: Delete one number xx. If there is no xx in the sequence, output -1 and skip this operation. If there are multiple xx, delete only one of them.

  2. 2 x: Insert one number xx into the sequence.

For each operation that is not skipped, find a permutation pp of aa that minimizes i=1npi+1pi\sum \limits_{i=1}^{n} \lvert p_{i+1}-p_i\rvert, that is, minimize $\lvert p_2-p_1\rvert+\lvert p_3-p_2\rvert+\dots+\lvert p_{n+1}-p_n\rvert$, where pn+1=p1p_{n+1}=p_1.

It is guaranteed that at any time there is at least 11 number in the sequence.


pp is a permutation of aa if and only if for all xx, [pi=x]=[ai=x]\sum [p_i=x]=\sum [a_i=x].

In short, pp is the result of rearranging aa in some way.

For example, {1,1,4,5,1,4}\{1,1,4,5,1,4\} is a permutation of {1,5,4,1,4,1}\{1,5,4,1,4,1\}, but {1,5,4,1,4,7}\{1,5,4,1,4,7\} is not.

Input Format

The input has q+2q + 2 lines in total.

Line 11 contains two positive integers n,qn, q.

Line 22 contains nn non-negative integers a1,a2,,ana_1, a_2, \dots, a_n, representing the initial sequence.

Lines 33 to q+2q + 2 each contain two numbers opt,xopt, x, representing one query.

Output Format

There are multiple lines of output.

Each line outputs 11 number, representing the answer for a query that is not ignored; otherwise output -1.

5 3
1 2 3 4 10
1 4
1 10
2 9
18
4
16

Hint

Sample 1 Explanation.

For the first query, the number 44 is deleted from the sequence, so the current sequence is 1,2,3,101, 2, 3, 10. It can be proven that 1818 is the minimum answer for the current sequence.

For the second query, the number 1010 is deleted from the sequence, so the current sequence is 1,2,31, 2, 3. It can be proven that 44 is the minimum answer for the current sequence.

For the third query, the number 99 is added to the sequence, so the current sequence is 1,2,3,91, 2, 3, 9. It can be proven that 1616 is the minimum answer for the current sequence.

Sample 2.

See abs/abs2.in and abs/abs2.out in the additional files.

This sample satisfies the constraints of test points 141\sim 4.

Sample 3.

See abs/abs3.in and abs/abs3.out in the additional files.

This sample satisfies the constraints of test points 7107\sim 10.

Constraints and Hints.

Let ww be the value range size. For all testdata, it is guaranteed that n,q106n,q\leq 10^6 and 0w1060\leq w\leq 10^6.

The specific constraints for each test point are shown in the table below.

Test Point ID n,qn,q\leq ww
141\sim 4 100100 1010
565\sim 6 10310^3
7107\sim 10 10610^6

Translated by ChatGPT 5