CF1857E.Power of Points
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given n points with integer coordinates x1,…xn, which lie on a number line.
For some integer s, we construct segments [s,x1], [s,x2], …, [s,xn]. Note that if xi<s, then the segment will look like [xi,s]. The segment [a,b] covers all integer points a,a+1,a+2,…,b.
We define the power of a point p as the number of segments that intersect the point with coordinate p, denoted as fp.
Your task is to compute p=1∑109fp for each s∈x1,…,xn, i.e., the sum of fp for all integer points from 1 to 109.
For example, if the initial coordinates are [1,2,5,7,1] and we choose s=5, then the segments will be: [1,5],[2,5],[5,5],[5,7],[1,5]. And the powers of the points will be: f1=2,f2=3,f3=3,f4=3,f5=5,f6=1,f7=1,f8=0,…,f109=0. Their sum is 2+3+3+3+5+1+1=18.
给你 n 个具有整数坐标的点 x1,…,xn,它们位于一条数轴上。
对某个整数 s,我们构造线段 [s,x1], [s,x2], …, [s,xn]。注意:若 xi<s,则该线段写作 [xi,s]。线段 [a,b] 覆盖所有整数点 a,a+1,a+2,…,b。
我们定义点 p 的权值为覆盖坐标 p 的线段数量,记作 fp。
你的任务是:对每个 s∈{x1,…,xn},计算 p=1∑109fp,即对从 1 到 109 的所有整数点 p,求其权值 fp 的总和。
例如,若初始坐标为 [1,2,5,7,1],且选择 s=5,则所构造的线段为:[1,5], [2,5], [5,5], [5,7], [1,5]。各点的权值为:f1=2, f2=3, f3=3, f4=3, f5=5, f6=1, f7=1, f8=0, …, f109=0。其总和为 2+3+3+3+5+1+1=18。
输入格式
The first line contains an integer t (1≤t≤104) — the number of test cases.
The first line of each test case contains an integer n (1≤n≤2⋅105) — the number of points.
The second line contains n integers x1,x2…xn (1≤xi≤109) — the coordinates of the points.
It is guaranteed that the sum of the values of n over all test cases does not exceed 2⋅105.
第一行包含一个整数 t(1≤t≤104)——测试用例的数量。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105)——点的数量。
每个测试用例的第二行包含 n 个整数 x1,x2…xn(1≤xi≤109)——各点的坐标。
保证所有测试用例中 n 的总和不超过 2⋅105。
输出格式
For each test case, output n integers, where the i-th integer is equal to the sum of the powers of all points for s=xi.
对于每个测试用例,输出 n 个整数,其中第 i 个整数等于当 s=xi 时所有点的幂之和。
输入输出样例
输入#1
3 3 1 4 3 5 1 2 5 7 1 4 1 10 100 1000
输出#1
8 7 6 16 15 18 24 16 1111 1093 1093 2893
说明/提示
In the first test case we first choose s=x1=1, then the following segments are formed: [1,1],[1,4],[1,3].
The powers of the points will be as follows: f1=3,f2=2,f3=2,f4=1,f5=0… The sum of powers of the points: 3+2+2+1+0+⋯+0=8.
After that we choose s=x2=4. Then there will be such segments: [1,4],[4,4],[3,4], and powers of the points are f1=1,f2=1,f3=2,f4=3.
At the end we take s=x3=3 and the segments look like this: [1,3],[3,4],[3,3], the powers of the points are f1=1,f2=1,f3=3,f4=1.
在第一个测试用例中,我们首先选择 s=x1=1,随后形成的区间为:[1,1]、[1,4]、[1,3]。
各点的权值如下:f1=3, f2=2, f3=2, f4=1, f5=0 …。所有点的权值之和为:3+2+2+1+0+⋯+0=8。
接着我们选择 s=x2=4,此时形成的区间为:[1,4]、[4,4]、[3,4],各点的权值为 f1=1, f2=1, f3=2, f4=3。
最后我们取 s=x3=3,此时区间为:[1,3]、[3,4]、[3,3],各点的权值为 f1=1, f2=1, f3=3, f4=1。
输入解题思路,AI测评打分。不知道怎么写?