Moving Points CodeForces - 1311F(离散化+树状数组)

There are n points on a coordinate axis OX. The i-th point is located at the integer point xi and has a speed 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-th point at the moment t (t can be non-integer) is calculated as xi+t⋅vi.

Consider two points i and j. Let 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) will be 0.

Your task is to calculate the value ∑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⋅105) — the number of points.

The second line of the input contains n integers x1,x2,…,xn (1≤xi≤108), 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 integers v1,v2,…,vn (−108≤vi≤108), where vi is the speed of the i-th point.

Output
Print one integer — the value ∑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
思路:这个题目,读懂题是很关键的,我当初就是没读懂题目。。这个最小距离不是按照时间来算的,而是按照点对来算的。如果有一种点对,xi<xj&&vi<vj,这一种永远也不可能相遇而且越来越远的,最初距离就是贡献的。但是剩下的,肯定有一个时间会让他们相遇(不一定是整数)。因此我们用树状数组来处理这一问题。
代码如下:

#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{
		return x<a.x;
	}
}p[maxx];
ll num[maxx],sum[maxx];
int b[maxx];
int n;

inline void init()
{
	memset(num,0,sizeof(num));
	memset(sum,0,sizeof(sum));
}
inline int lowbit(int x){return x&-x;}
inline ll query(ll c[],int x)
{
	ll ans=0;
	while(x)
	{
		ans+=c[x];
		x-=lowbit(x);
	}
	return ans;
}
inline void add(ll c[],int x,ll v)
{
	while(x<maxx)
	{
		c[x]+=v;
		x+=lowbit(x);
	}
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&p[i].x);
	for(int i=1;i<=n;i++) scanf("%d",&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;//离散化,因为和v没有直接关系。
	init();
	ll ans=0;
	for(int i=1;i<=n;i++)
	{
		int pos=lower_bound(b+1,b+1+len,p[i].v)-b;//计算之前有多少个v小于当前v
		ans+=query(num,pos)*(ll)p[i].x-query(sum,pos);//计算出个数以及前缀和
		add(num,pos,1);
		add(sum,pos,p[i].x);//边处理边添加
	}
	cout<<ans<<endl;
	return 0;
}

努力加油a啊,(o)/~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值