https://codeforces.com/contest/1311F. Moving Points

F. Moving Points

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There are nn points on a coordinate axis OXOX. The ii-th point is located at the integer point xixi and has a speed vivi. It is guaranteed that no two points occupy the same coordinate. All nn points move with the constant speed, the coordinate of the ii-th point at the moment tt (tt can be non-integer) is calculated as xi+t⋅vixi+t⋅vi.

Consider two points ii and jj. 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 ii and jj coincide at some moment, the value d(i,j)d(i,j) will be 00.

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

Input

The first line of the input contains one integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of points.

The second line of the input contains nn integers x1,x2,…,xnx1,x2,…,xn (1≤xi≤1081≤xi≤108), where xixi is the initial coordinate of the ii-th point. It is guaranteed that all xixi are distinct.

The third line of the input contains nn integers v1,v2,…,vnv1,v2,…,vn (−108≤vi≤108−108≤vi≤108), where vivi is the speed of the ii-th point.

Output

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

Examples

input

Copy

3
1 3 2
-100 2 3

output

Copy

3

input

Copy

5
2 1 4 3 5
2 2 2 3 4

output

Copy

19

input

Copy

2
2 1
-3 0

output

Copy

0

【题意】

给你n个点,每个点有一个速度v,对于任意时刻,每个点的位置为x+vt。问对于所有点i,j,∑1≤i<j≤n d(i,j)的最小值是多少。
题解:简单推算可以发现,对于每个位置,考虑位置在它左面且速度比他小的或者在它右面速度比他大的,只有这两种情况答案不为0,其他早晚会相遇,为0,这两种情况是我们需要计算的值,这两种其实是同一种情况,以每个位置为右端点,这样只需找出第i个位置左面所有比他速度小的数。将速度离散化(和速度无关)后,将位置排序,对于每个位置,ans+=这个位置的x*(比他速度小的点的数量)-(比他速度小的点的x的和)。求和就要想到树状数组,用两个树状数组num,和sum分别维护这两个变量。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxx=2e5+100;
struct node
{
	int x,v;
	bool operator<(const node &a)const //先把x排序,直接排完
	{
		return x<a.x;
	}
}p[maxx];
ll num[maxx],sum[maxx];
int b[maxx];
int n;
void init()
{
	memset(num,0,sizeof(num));
	memset(sum,0,sizeof(sum));
}
int lowbit(int x)
{
    return x&-x;
}
ll query(ll c[],int x)
{
	ll ans=0;
	while(x)
	{
		ans+=c[x];
		x-=lowbit(x);
	}
	return ans;
}
void add(ll c[],int x,ll v)
{
	while(x<maxx)
	{
		c[x]+=v;
		x+=lowbit(x);
	}
}
int main()
{   
    ios::sync_with_stdio(false);
	cin>>n;
	for(int i=1;i<=n;i++) 
        cin>>p[i].x;
	for(int i=1;i<=n;i++) 
	cin>>p[i].v,b[i]=p[i].v;
	sort(p+1,p+1+n);
	sort(b+1,b+1+n);
	int len=unique(b+1,b+1+n)-b-1;//速度去重
	init();
	ll ans=0;
	for(int i=1;i<=n;i++)
	{
		int pos=lower_bound(b+1,b+1+len,p[i].v)-b;//有多少个速度比它小的
		ans+=query(num,pos)*(ll)p[i].x-query(sum,pos);
		add(num,pos,1);//前面比他速度小的点的数量
		add(sum,pos,p[i].x);//前面比他速度小的点的x之和
	}
	cout<<ans<<endl;
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值