CF1808B.Playing in a Casino
普及-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述

Galaxy Luck, a well-known casino in the entire solar system, introduces a new card game.
In this game, there is a deck that consists of n cards. Each card has m numbers written on it. Each of the n players receives exactly one card from the deck.
Then all players play with each other in pairs, and each pair of players plays exactly once. Thus, if there are, for example, four players in total, then six games are played: the first against the second, the first against the third, the first against the fourth, the second against the third, the second against the fourth and the third against the fourth.
Each of these games determines the winner in some way, but the rules are quite complicated, so we will not describe them here. All that matters is how many chips are paid out to the winner. Let the first player's card have the numbers a1,a2,…,am, and the second player's card — b1,b2,…,bm. Then the winner of the game gets ∣a1−b1∣+∣a2−b2∣+⋯+∣am−bm∣ chips from the total pot, where ∣x∣ denotes the absolute value of x.
To determine the size of the total pot, it is necessary to calculate the winners' total winnings for all games. Since there can be many cards in a deck and many players, you have been assigned to write a program that does all the necessary calculations.

银河幸运赌场(Galaxy Luck)是整个太阳系内赫赫有名的赌场,现推出一款全新的纸牌游戏。
该游戏使用一副由 n 张牌组成的牌组,每张牌上写有 m 个数字。共有 n 名玩家,每人恰好从牌组中分得一张牌。
随后,所有玩家两两之间进行对战,且每对玩家仅对战一次。例如,若总共有四名玩家,则共进行六局对战:第 1 名玩家对第 2 名玩家、第 1 名玩家对第 3 名玩家、第 1 名玩家对第 4 名玩家、第 2 名玩家对第 3 名玩家、第 2 名玩家对第 4 名玩家、第 3 名玩家对第 4 名玩家。
每局对战均会决出一名胜者,但具体判定规则较为复杂,此处不予详述。我们唯一关心的是:胜者能从总奖池中获得多少枚筹码。设第一名玩家的牌上数字为 a1,a2,…,am,第二名玩家的牌上数字为 b1,b2,…,bm,则该局胜者可获得 ∣a1−b1∣+∣a2−b2∣+⋯+∣am−bm∣ 枚筹码,其中 ∣x∣ 表示 x 的绝对值。
为确定总奖池的规模,需计算所有对战中胜者所获筹码的总和。由于牌组中可能包含大量牌,且玩家人数也可能很多,因此需要你编写一个程序来完成全部必要计算。
输入格式
Each test consists of several test cases. The first line contains one integer t (1≤t≤1000) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n and m (1≤n⋅m≤3⋅105) — the number of cards in the deck and the count of numbers on the one card.
Each of the following n lines of the test case set contains m integers ci,j (1≤ci,j≤106) — a description of the i-th card.
It is guaranteed that the total n⋅m in all tests does not exceed 3⋅105.
每个测试包含若干测试用例。第一行包含一个整数 t(1≤t≤1000),表示测试用例的数量。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 m(1≤n⋅m≤3⋅105),分别表示牌组中的卡片数量以及每张卡片上的数字个数。
接下来的 n 行,每行包含 m 个整数 ci,j(1≤ci,j≤106),表示第 i 张卡片的描述。
保证所有测试用例中 n⋅m 的总和不超过 3⋅105。
输出格式
For each test case, print one number — the total amount of winnings from all games.
对于每个测试用例,输出一个数字——所有游戏的总奖金金额。
输入输出样例
输入#1
3 3 5 1 4 2 8 5 7 9 2 1 4 3 8 5 3 1 1 4 4 15 1 10 4 3 1 2 3 3 2 1 1 2 1 4 2 7
输出#1
50 0 31
说明/提示
Consider the first test case.
In the game between the first and second player, the winner receives ∣1−7∣+∣4−9∣+∣2−2∣+∣8−1∣+∣5−4∣=19 chips.
In the game between the first and third player, the winner receives ∣1−3∣+∣4−8∣+∣2−5∣+∣8−3∣+∣5−1∣=18 in chips.
In the game between the second and third player, the winner receives ∣7−3∣+∣9−8∣+∣2−5∣+∣1−3∣+∣4−1∣=13 chips.
The total is 19+18+13=50 chips.
考虑第一个测试用例。
在第一位玩家与第二位玩家的对局中,获胜者获得 ∣1−7∣+∣4−9∣+∣2−2∣+∣8−1∣+∣5−4∣=19 枚筹码。
在第一位玩家与第三位玩家的对局中,获胜者获得 ∣1−3∣+∣4−8∣+∣2−5∣+∣8−3∣+∣5−1∣=18 枚筹码。
在第二位玩家与第三位玩家的对局中,获胜者获得 ∣7−3∣+∣9−8∣+∣2−5∣+∣1−3∣+∣4−1∣=13 枚筹码。
总计为 19+18+13=50 枚筹码。
输入解题思路,AI测评打分。不知道怎么写?