POJ_1990_MooFest_树状数组

MooFest
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 7754 Accepted: 3500

Description

Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a social gathering of cows from around the world. MooFest involves a variety of events including haybale stacking, fence jumping, pin the tail on the farmer, and of course, mooing. When the cows all stand in line for a particular event, they moo so loudly that the roar is practically deafening. After participating in this event year after year, some of the cows have in fact lost a bit of their hearing. 

Each cow i has an associated "hearing" threshold v(i) (in the range 1..20,000). If a cow moos to cow i, she must use a volume of at least v(i) times the distance between the two cows in order to be heard by cow i. If two cows i and j wish to converse, they must speak at a volume level equal to the distance between them times max(v(i),v(j)). 

Suppose each of the N cows is standing in a straight line (each cow at some unique x coordinate in the range 1..20,000), and every pair of cows is carrying on a conversation using the smallest possible volume. 

Compute the sum of all the volumes produced by all N(N-1)/2 pairs of mooing cows. 

Input

* Line 1: A single integer, N 

* Lines 2..N+1: Two integers: the volume threshold and x coordinate for a cow. Line 2 represents the first cow; line 3 represents the second cow; and so on. No two cows will stand at the same location. 

Output

* Line 1: A single line with a single integer that is the sum of all the volumes of the conversing cows. 

Sample Input

4
3 1
2 5
2 6
4 3

Sample Output

57

有 N 头牛成一列,每头牛给定坐标 x 和 v。两头牛之间建立联系,代价是较大的 v * 它俩之间的距离。求任意一头牛都与其他所有牛建立联系的代价之和。


一开始想的是按照 v 从大到小的顺序遍历牛,这样这头牛的花费为 v * 它到剩下的其他牛的距离之和。然后就是怎样快速地求出它到其它牛的距离之和。但是没想出啥好办法。

后来想到按照 v 从小到大的顺序遍历牛, 这样这头牛的花费就是 v * 它到之前的牛的距离之和。求和的方法很关键,前一种思路就是因为没想到这个方法没做出来。算距离的方法是算距离之差。把 x 提出来,就可以算区间距离了。但是这样就不能加绝对值,要考虑正负问题。这就要把已经有的牛分成两部分,x 大的和 x 小的。在提 x 出来的过程中,我们需要知道有多少个比 x 小的(进而也知道了有多少个比 x 大的)。求区间和的问题考虑用树状数组解决。

维护两个树状数组。一个维护的是对应位置上牛的坐标值,用来求已有牛的坐标和;一个维护的是对应位置上是否有牛,用来求比 x 小的牛的数量。


#include<cstdio>
#include<iostream>
#include<algorithm>
#define f(x) p[x].first
#define s(x) p[x].second

using namespace std;
typedef long long LL;
typedef pair<LL, LL> P;

int N;
const int maxn = 20000 + 10;
const int inf  = 20000;//pos最大值
P p[maxn];
LL A[maxn];//times小且pos小的牛的pos和
LL B[maxn];//times小且pos小的牛的数量

void Add(LL *Bit, int i, int x)
{
	while(i <= inf)
	{
		Bit[i] += x;
		i += i&-i;
	}
}

LL Sum(LL *Bit, int i)
{
	LL sum = 0;

	while(i)
	{
		sum += Bit[i];
		i -= i&-i;
	}

	return sum;
}

bool cmp(P x, P y)
{
	return x.first < y.first;
}

int main()
{
	scanf("%d", &N);

	//f是times,s是pos
	for(int i= 1; i<= N; i++)
		scanf("%lld %lld", &f(i), &s(i));

	//times由小到达排序
	sort(p+1, p+1+N, cmp);

	LL res = 0;
	for(int i= 1; i<= N; i++)
	{
		//当前pos小于s(i)的pos和/数量
		LL a = Sum(A, s(i)), b = Sum(B, s(i));

		//前面牛
		res += (s(i) * b - a) * f(i);
		//后面牛
		res += (Sum(A, inf) - a - s(i) * (i-1-b)) * f(i);

		//把这个牛加入树状数组
		Add(A, s(i), s(i));
		Add(B, s(i), 1);
	}

	cout << res << endl;

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值