#abc306f. F - Merge Sets

F - Merge Sets

Score : 525525 points

问题描述

对于两个整数集合 AABB,满足 AB=A \cap B = \emptyset,我们定义 f(A,B)f(A,B) 如下:

  • 定义序列 C=(C1,C2,,CA+B)C=(C_1,C_2,\dots,C_{|A|+|B|}) 包含集合 ABA \cup B 的所有元素,并按升序排列。
  • k1,k2,,kAk_1,k_2,\dots,k_{|A|} 使得 A={Ck1,Ck2,,CkA}A=\lbrace C_{k_1},C_{k_2},\dots,C_{k_{|A|}}\rbrace。然后令 f(A,B)=i=1Aki\displaystyle f(A,B)=\sum_{i=1}^{|A|} k_i

例如,若 A={1,3}A=\lbrace 1,3\rbraceB={2,8}B=\lbrace 2,8\rbrace,则 C=(1,2,3,8)C=(1,2,3,8),所以 A={C1,C3}A=\lbrace C_1,C_3\rbrace;因此,f(A,B)=1+3=4f(A,B)=1+3=4

我们有 NN 个包含 MM 个整数的集合 S1,S2,,SNS_1,S_2,\dots,S_N,其中每个集合为 Si={Ai,1,Ai,2,,Ai,M}S_i = \lbrace A_{i,1},A_{i,2},\dots,A_{i,M}\rbrace。这里保证了任意 iji \neq j 时,SiSj=S_i \cap S_j = \emptyset

1i<jNf(Si,Sj)\displaystyle \sum_{1\leq i<j \leq N} f(S_i, S_j)

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

For two sets of integers, AA and BB, such that AB=A \cap B = \emptyset, we define f(A,B)f(A,B) as follows.

  • Let C=(C1,C2,,CA+B)C=(C_1,C_2,\dots,C_{|A|+|B|}) be a sequence consisting of the elements of ABA \cup B, sorted in ascending order.
  • Take k1,k2,,kAk_1,k_2,\dots,k_{|A|} such that A={Ck1,Ck2,,CkA}A=\lbrace C_{k_1},C_{k_2},\dots,C_{k_{|A|}}\rbrace. Then, let f(A,B)=i=1Aki\displaystyle f(A,B)=\sum_{i=1}^{|A|} k_i.

For example, if A={1,3}A=\lbrace 1,3\rbrace and B={2,8}B=\lbrace 2,8\rbrace, then C=(1,2,3,8)C=(1,2,3,8), so A={C1,C3}A=\lbrace C_1,C_3\rbrace; thus, f(A,B)=1+3=4f(A,B)=1+3=4.

We have NN sets of integers, S1,S2,SNS_1,S_2\dots,S_N, each of which has MM elements. For each i (1iN)i\ (1 \leq i \leq N), Si={Ai,1,Ai,2,,Ai,M}S_i = \lbrace A_{i,1},A_{i,2},\dots,A_{i,M}\rbrace. Here, it is guaranteed that SiSj= (ij)S_i \cap S_j = \emptyset\ (i \neq j).

Find 1i<jNf(Si,Sj)\displaystyle \sum_{1\leq i<j \leq N} f(S_i, S_j).

Constraints

  • 1N1041\leq N \leq 10^4
  • 1M1021\leq M \leq 10^2
  • 1Ai,j1091\leq A_{i,j} \leq 10^9
  • If i1i2i_1 \neq i_2 or j1j2j_1 \neq j_2, then Ai1,j1Ai2,j2A_{i_1,j_1} \neq A_{i_2,j_2}.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN MM

A1,1A_{1,1} A1,2A_{1,2} \dots A1,MA_{1,M}

\vdots

AN,1A_{N,1} AN,2A_{N,2} \dots AN,MA_{N,M}

Output

Print the answer as an integer.

Sample Input 1

3 2
1 3
2 8
4 6

Sample Output 1

12

S1S_1 and S2S_2 respectively coincide with AA and BB exemplified in the problem statement, and f(S1,S2)=1+3=4f(S_1,S_2)=1+3=4. Since f(S1,S3)=1+2=3f(S_1,S_3)=1+2=3 and f(S2,S3)=1+4=5f(S_2,S_3)=1+4=5, the answer is 4+3+5=124+3+5=12.

Sample Input 2

1 1
306

Sample Output 2

0

Sample Input 3

4 4
155374934 164163676 576823355 954291757
797829355 404011431 353195922 138996221
191890310 782177068 818008580 384836991
160449218 545531545 840594328 501899080

Sample Output 3

102

update @ 2024/3/10 08:39:40