Codeforces 1311 F. Moving Points (树状数组+离散化)

Description:

There are n n points on a coordinate axis OX. The i t h i-th point is located at the integer point x i xi and has a speed v i vi . It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i t h i-th point at the moment t t (t can be non-integer) is calculated as x i + t v i xi+t⋅vi .

Consider two points i i and j j . Let d ( i , j ) d(i,j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d ( i , j ) d(i,j) will be 0.

Your task is to calculate the value 1 i < j n d ( i , j ) \sum_{1≤i<j≤n}d(i,j) (the sum of minimum distances over all pairs of points).

Input

The first line of the input contains one integer n ( 2 n 2 1 0 5 ) n (2≤n≤2⋅10^5) — the number of points.

The second line of the input contains n n integers x 1 , x 2 , , x n ( 1 x i 1 0 8 ) x_1,x_2,…,x_n (1≤x_i≤10^8) , where xi is the initial coordinate of the i-th point. It is guaranteed that all xi are distinct.

The third line of the input contains n n integers v 1 , v 2 , , v n ( 1 0 8 v i 1 0 8 ) v_1,v_2,…,v_n (−10^8≤vi≤10^8) , where v i v_i is the speed of the i t h i-th point.

Output

Print one integer — the value 1 i < j n d ( i , j ) \sum_{1≤i<j≤n}d(i,j) (the sum of minimum distances over all pairs of points).

Examples

input

3
1 3 2
-100 2 3

output

3

input

5
2 1 4 3 5
2 2 2 3 4

output

19

input

2
2 1
-3 0

output

0

解题思路:

给出 n n 个点和他们的初始位置 x x 和移动速度 v v ,问所有点对在任意时刻全部最小值的和是多少,不是同一时刻而是每个点对的最小值的和,可以是很多时刻。

时间可以是任意实数,对于任意两点,如果 x i < = x j x_i<=x_j ,且 x i < = v j x_i<=v_j ,那么 i j i,j 永远不可能相遇。否则肯定可以相遇,所以只需要计算 x i < = x j x_i<=x_j ,且 x i < = v j x_i<=v_j 的情况即可。这种情况贡献就是距离,树状数组维护一个和就好了。

AC代码:

const int N = 5e3 + 5;
int n, d;
int ans[N];
int ch[N];
int fa[N];
int c, now, mx;
int cnt, pos;
int get_dep()
{
    int ans = now;
    c++;
    if (c == mx)
        now++, mx *= 2, c = 1;
    return ans;
}

int main()
{
    int t;
    sd(t);
    while (t--)
    {
        mem(ch, 0);
        sdd(n, d);
        cnt = 0;
        rep(i, 1, n - 1)
        {
            ans[i] = i;
            cnt += i;
        }
        pos = n - 1;
        now = 1, c = 1, mx = 2;
        if (cnt < d)
        {
            puts("NO");
            continue;
        }
        while (cnt > d)
        {
            int dep = get_dep();
            if (dep >= pos)
                break;
            int tmp = cnt - d;
            if (tmp >= (pos - dep))
            {
                cnt -= pos - dep;
                ans[pos] = dep;
            }
            else
            {
                ans[pos] -= tmp;
                cnt = d;
            }
            pos--;
        }
        if (cnt > d)
            puts("NO");
        else
        {
            puts("YES");
            ans[0] = 0;
            rep(i, 1, n - 1)
            {
                rep(j, 0, n - 1) if (ans[j] == ans[i] - 1 && ch[j] < 2)
                {
                    ch[j]++;
                    fa[i] = j;
                    break;
                }
            }
            rep(i, 1, n - 1)
            {
                printf("%d%c", fa[i] + 1, i == n - 1 ? '\n' : ' ');
            }
        }
    }
    return 0;
}
发布了683 篇原创文章 · 获赞 410 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/qq_43627087/article/details/104494396