PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000081. Hợp đồng thời hai đoạn (Range Parallel Unionfind)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Hợp đồng thời hai đoạn (Range Parallel Unionfind)

Range Parallel Unionfind

Source: Library Checker

Version: Phuoc Hung OJ Extended

Problem Statement

Consider an undirected graph with NN vertices numbered from 00 to N−1N-1, initially containing no edges. Vertex ii has an associated integer weight xix_i.

Process QQ operations. An operation with parameters k,a,bk,a,b simultaneously adds kk edges:

(a+i, b+i),i=0,1,…,k−1.(a+i,\ b+i),\qquad i=0,1,\ldots,k-1.

Edges are never deleted. Duplicate edges and self-loops are permitted. An operation with k=0k=0 adds no edges.

After each operation, consider all pairs of distinct vertices (u,v)(u,v) such that 0≤u<v<N0\le u<v<N and uu and vv are in the same connected component. Define

$$X=\sum_{\substack{0\le u<v<N\\u,v\text{ connected}}}x_ux_v.$$

Print XX modulo 998244353998244353 after every operation.

Input

  • The first line contains the integers N,QN,Q.
  • The second line contains NN integers x0,x1,…,xN−1x_0,x_1,\ldots,x_{N-1}.
  • Each of the following QQ lines contains three integers k,a,bk,a,b.

Output

Print QQ lines. Line jj contains X mod 998244353X\bmod998244353 after operation jj.

Subtasks

  • Subtask 1 (20 points): 1≤N≤251\le N\le25, 1≤Q≤801\le Q\le80, 0≤xi≤1000\le x_i\le100, 0≤k≤N0\le k\le N, 0≤a,b≤N−k0\le a,b\le N-k.
  • Subtask 2 (30 points): 1≤N≤25001\le N\le2500, 1≤Q≤70001\le Q\le7000, 0≤xi≤1060\le x_i\le10^6, 0≤k≤N0\le k\le N, 0≤a,b≤N−k0\le a,b\le N-k.
  • Subtask 3 (50 points): 1≤N≤5⋅1051\le N\le5\cdot10^5, 1≤Q≤5⋅1051\le Q\le5\cdot10^5, 0≤xi<9982443530\le x_i<998244353, 0≤k≤N0\le k\le N, 0≤a,b≤N−k0\le a,b\le N-k.

Examples

Input

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

Output

0
0
1
6
6
6
10

Explanation

The first two operations do not connect any two distinct vertices, so the result is 00.

After the third operation, vertices 00 and 22 are connected and contribute exactly one pair, giving X=1X=1.

After the fourth operation, vertices 0,1,2,30,1,2,3 belong to one component, which contributes (42)=6\binom{4}{2}=6 pairs. The next two operations do not change connectivity.

After the final operation, all five vertices are connected, giving X=(52)=10X=\binom{5}{2}=10.