There are $$N$$ towns in a coordinate plane. Town $$i$$ is located at coordinate \((x_i,y_i)\). The distance between town $$i$$ and town $$j$$ is \(|x_i-x_j|+|y_i-y_j|\). Your task is to compute the sum of the distance between each pair of towns.
Input format
- The first line contains an integer $$T$$ denoting the number of test cases.
- The first line of each test case contains an integer $$N$$ denoting the total number of towns.
- Next $$N$$ lines contain two space-separated integers \(x_i\) and \(y_i\) denoting a town at coordinate \((x_i,y_i)\).
Output format
For each test case, print the sum of the distance between each pair of towns in a new line.
Constraints
It is guaranteed that the sum of $$N$$ over $$T$$ test cases does not exceed $$1e6$$.
For the first testcase, there are no possible pairs of town. Therefore, the sum is 0.
For the second testcase, there are only 3 possible pairs (town1,town2), (town1,town3) and (town2,town3). Therefore, the sum of distance between every pair of towns is 12.
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Login to unlock the editorial
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor