Problem11253--[yosupo] Data Structure - Range Parallel Unionfind

11253: [yosupo] Data Structure - Range Parallel Unionfind

[Creator : ]
Time Limit : 6.000 sec  Memory Limit : 512 MiB

Description

There's an undirected graph $G$ with $N$ vertices and $0$ edges, along with a sequence of integers $x_0, \ldots, x_{N-1}$. Please process $Q$ queries of the following format:

- `$k$ $a$ $b$`: Add edge $(a + i, b + i)$ to $G$ for each $i=0,1,\ldots,k-1$.

After processing each query, output the remainder when $X$ defined below modulo $998244353$:

- Define $\mathrm{same}(i,j)$ as $1$ if vertices $i$ and $j$ belong to the same connected component in $G$, and $0$ otherwise, for $0\leq i,j \leq N-1$.
- Define $X = \sum_{0\leq i<j\leq N-1}\mathrm{same}(i,j)x_ix_j$.

Input

$N$ $Q$
$x_0$ $\cdots$ $x_{N-1}$
$k$ $a$ $b$
$\vdots$
$k$ $a$ $b$

Constraints

- $1 \leq N \leq 5\times 10^5$
- $1 \leq Q \leq 5\times 10^5$
- $0 \leq x_i < 998244353$
- $0 \leq k\leq N$
- $0 \leq a,b \leq N-k$

Sample 1 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

Sample 1 Output

0
0
1
6
6
6
10

Sample 2 Input

5 7
12 34 56 78 90
0 0 0
1 0 0
1 0 2
2 2 1
2 0 1
4 0 0
3 2 1

Sample 2 Output

0
0
672
10940
10940
10940
27140

Source/Category