#P5294. [HNOI2019] 序列

[HNOI2019] 序列

Description

Given a sequence A1,A2,,AnA_1,A_2,\dots,A_n of length nn, and mm operations. Each operation changes some AiA_i to kk. Before the first change and after each change, you need to find a non-decreasing sequence B1,B2,,BnB_1,B_2,\dots,B_n of the same length nn, such that i=1n(AiBi)2\sum\limits_{i=1}^n (A_i-B_i)^2 is minimized, and output this minimum value.

Note that the effect of each operation is independent, i.e. each operation only affects the current query.

To avoid precision issues, we guarantee that this minimum value is a fraction, i.e. it can be written as xy\dfrac{x}{y} where xx and yy are non-negative integers. Then you should output (x×yP2modP)(x \times y^{P-2} \bmod P), which represents xy\dfrac{x}{y} under modulo arithmetic. Here P=998244353P=998244353 is a large prime.

Input Format

The first line contains two non-negative integers n,mn,m, representing the sequence length and the number of operations.

The second line contains nn positive integers separated by spaces, representing A1AnA_1\sim A_n.

The next mm lines each contain two positive integers i,ki,k, meaning to change AiA_i to kk.

Output Format

Output m+1m+1 lines, each containing one integer. The ii-th line should output the answer after the (i1)(i-1)-th change. In particular, the first line should be the answer for the initial state.

5 3
9 2 4 6 4
1 1
1 4
5 6
28
2
4
26

Hint

Sample Explanation

For the first query, an optimal sequence BB is {5,5,5,5,5}\{5, 5, 5, 5, 5\}.

For the second query, an optimal sequence BB is {1,2,4,5,5}\{1, 2, 4, 5, 5\}.

For the third query, an optimal sequence BB is {3,3,4,5,5}\{3, 3, 4, 5, 5\}.

For the fourth query, an optimal sequence BB is {5,5,5,6,6}\{5, 5, 5, 6, 6\}.

The sample is a special case where there exists an optimal solution with all BiB_i being integers.

Constraints and Notes

For the first 10%10\% of the testdata, it is guaranteed that n,m10n,m\le 10, k,Ai103k,A_i\le 10^3, and there exists an optimal solution with all BiB_i being integers.

For the first 30%30\% of the testdata, it is guaranteed that n,m100n,m\le 100.

For another 20%20\% of the testdata, it is guaranteed that m=0m = 0.

For another 20%20\% of the testdata, it is guaranteed that n,m3×104n,m \le 3 \times 10^4.

For all testdata, it is guaranteed that 3n1053 \le n \le 10^5, 0m1050 \le m \le 10^5, 1k,Ai1091 \le k, A_i \le 10^9.

Translated by ChatGPT 5