atcoder#ARC126E. [ARC126E] Infinite Operations

[ARC126E] Infinite Operations

Score : 800800 points

Problem Statement

Given are a sequence of NN positive integers A=(A1,A2,,AN)A = (A_1, A_2, \ldots, A_N) and QQ queries. The ii-th query is as follows:

  • given integers xix_i and yiy_i (where 1xiN1\leq x_i\leq N), change AxiA_{x_i} to yiy_i.

Each time the query changes the sequence, find the answer to the following problem modulo 998244353998244353 (see Notes).

Let f(n)f(n) be the maximum total number of points when doing the following operation nn times on AA.

  • Choose i,ji, j such that AiAjA_i\leq A_j and choose non-negative real number xx such that Ai+2xAjA_i + 2x \leq A_j.
  • Add xx to AiA_i and subtract xx from AjA_j.
  • Gain xx points.

It can be proved that the limit limnf(n)\displaystyle \lim_{n\to\infty} f(n) exists. Find this limit.

Notes

We can prove that the limit in question is always a rational number. Additionally, under the Constraints of this problem, when that number is represented as PQ\frac{P}{Q} using two coprime integers PP and QQ, we can prove that there is a unique integer RR such that R×QP(mod998244353)R\times Q\equiv P\pmod{998244353} and 0R<9982443530\leq R < 998244353. Find this RR.

Constraints

  • 2N3×1052\leq N\leq 3\times 10^5
  • 1Q3×1051\leq Q\leq 3\times 10^5
  • 1Ai1091\leq A_i \leq 10^9
  • 1xiN1\leq x_i\leq N
  • 1yi1091\leq y_i\leq 10^9

Input

Input is given from Standard Input in the following format:

NN QQ

A1A_1 A2A_2 \ldots ANA_N

x1x_1 y1y_1

\vdots

xQx_Q yQy_Q

Output

Print QQ lines; the ii-th line should contain the answer to the problem just after the change by the ii-th query.

3 4
7 5 5
1 5
2 6
1 7
3 5
0
1
2
2

The first query changes the sequence to (5,5,5)(5, 5, 5). Here, we have f(n)=0f(n) = 0 for any nn, so the answer is 00.

The second query changes the sequence to (5,6,5)(5, 6, 5). Here, one possible way to do the operations is as follows.

  • Choose (i,j,x)=(3,2,0.4)(i,j,x) = (3,2,0.4), changing the sequence to (5,5.6,5.4)(5, 5.6, 5.4) and gaining 0.40.4 points.
  • Choose (i,j,x)=(1,2,0.3)(i,j,x) = (1,2,0.3), changing the sequence to (5.3,5.3,5.4)(5.3, 5.3, 5.4) and gaining 0.30.3 points.

The above two operations gain 0.70.7 points, from which we can see that f(2)0.7f(2) \geq 0.7.

We can prove that it is impossible to gain more than 11 point in total, and that the total number of points that can be gained by the optimal way to do the operations tends to 11 as the number of operations increases. Thus, we have limnf(n)=1\displaystyle \lim_{n\to\infty} f(n) = 1.

2 4
1 2
2 5
1 3
1 2
2 3
2
1
499122178
499122177