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.
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.
* 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
Source
USACO 2004 U S Open
/**
题意:FJ有n头牛,排列成一条直线(不会在同一个点),给出每头牛在直线上的坐标x。
另外,每头牛还有一个自己的声调v,如果两头牛(i和j)之间想要沟通的话,
它们必须用同个音调max(v[i],v[j]),沟通起来消耗的能量为:
max(v[i],v[j]) * 它们之间的距离。
问要使所有的牛之间都能沟通(两两之间),总共需要消耗多少能量。
分析:很明显,必须按照坐标或者v值排序才有优化的余地。
按v值排序后,枚举v值,计算距离和。
题目告诉每头牛坐标唯一,不妨按坐标建立线段数,区间保存牛的个数和坐标和。
然后我们枚举被计算的v[i]:即以v值小大数序依次加入,区间查找左区间的牛的个数a
和坐标和b, 设该牛坐标x,则该v[i]对答案的贡献为 v[i]*(x*a-b)..
同理,可以计算又区间。
注意:v值可能重复,故左右区间一个包含v[i],另一个不包含v[i];
Author : WCB
Time : 2014/11/19
*/
#include <stdio.h>
#include <algorithm>
#include<cstring>
#include <iostream>
using namespace std;
const int N = 20100;
typedef long long LL;
LL xsum[N],cnt[N],sum[N<<2],num[N<<2];
struct Cow
{
int id;
LL v,x;
};
Cow cow[N];
int cmpv(Cow a,Cow b)
{
return a.v<b.v;
}
int cmpx(Cow a, Cow b)
{
return a.x<b.x;
}
void update(int pos, LL d, int l, int r, int rt)
{
if(l==r)
{
num[rt]++;
sum[rt]+=d;
return;
}
int m = l+r>>1;
if(pos<=m) update(pos,d,l,m,rt<<1);
else update(pos,d,m+1,r,rt<<1|1);
sum[rt] =sum[rt<<1] + sum[rt<<1|1];
num[rt] = num[rt<<1]+num[rt<<1|1];
}
void query(int L, int R, LL&a, LL &b, int l, int r, int rt)
{
if(L<=l && R>=r)
{
a+=num[rt];
b+=sum[rt];
return;
}
int m = l+r>>1;
if(L<=m) query(L,R,a,b,l,m,rt<<1);
if(R>m) query(L,R,a,b,m+1,r,rt<<1|1);
}
int main()
{
//freopen("date.in","r",stdin);
int i,j,k,m,n;
scanf("%d",&n);
for(i=1; i<=n; i++) scanf("%I64d %I64d",&cow[i].v,&cow[i].x),cow[i].id=i;
sort(cow+1,cow+1+n,cmpv);
xsum[0] = 0;
cnt[0] = 0;
for(i=1; i<=n; i++) xsum[i] = xsum[i-1]+cow[i].x, cnt[cow[i].id]=i-1;
sort(cow+1,cow+1+n,cmpx);
LL ans = 0;
memset(sum,0,sizeof(sum));
memset(num,0,sizeof(num));
for(i=1; i<=n; i++)
{
LL a = 0, b=0;
query(1,(int)cow[i].v, a, b, 1,20000,1);
ans+=cow[i].v*(a*cow[i].x-b);
update((int)cow[i].v,cow[i].x,1,20000,1);
}
memset(sum,0,sizeof(sum));
memset(num,0,sizeof(num));
for(i=n; i>=1; i--)
{
LL a = 0, b=0;
if(cow[i].v>1) query(1,(int)cow[i].v-1, a, b, 1,20000,1);
ans+=cow[i].v*(-a*cow[i].x+b);
update((int)cow[i].v,cow[i].x,1,20000,1);
}
printf("%I64d\n",ans);
return 0 ;
}