#DSU0000081. Hợp đồng thời hai đoạn (Range Parallel Unionfind)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 vertices numbered from to , initially containing no edges. Vertex has an associated integer weight .
Process operations. An operation with parameters simultaneously adds edges:
Edges are never deleted. Duplicate edges and self-loops are permitted. An operation with adds no edges.
After each operation, consider all pairs of distinct vertices such that and and are in the same connected component. Define
$$X=\sum_{\substack{0\le u<v<N\\u,v\text{ connected}}}x_ux_v.$$Print modulo after every operation.
Input
- The first line contains the integers .
- The second line contains integers .
- Each of the following lines contains three integers .
Output
Print lines. Line contains after operation .
Subtasks
- Subtask 1 (20 points): , , , , .
- Subtask 2 (30 points): , , , , .
- Subtask 3 (50 points): , , , , .
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 .
After the third operation, vertices and are connected and contribute exactly one pair, giving .
After the fourth operation, vertices belong to one component, which contributes pairs. The next two operations do not change connectivity.
After the final operation, all five vertices are connected, giving .