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
题解:
这道题想了挺久的,主要一直没搞懂这个点与后面点的距离,只专注于树状数组的录入应从小到大。
然后今天豁然开朗了。首先,用结构体,按音量从小到大排序,这个就可以保证,求和时,当前点的音量与前面比便是最大的。然后定义两个树状数组,一个记录这个点与前面点的距离,一个记录这个点前面点的个数。可以把这个位置放在一个数轴上,首先这个点前面的点的距离我们很好理解tree[i].a*(tree[i].bp-sum),然后就是求后面的距离,你想1-5和5-1是一样的,因为1的音量在 5 6 后面,证明有两条路径 1-6,1-5,我们可以求总距离sum(20000,1),然后减去前面的距离和,然后减去后面的个数乘tree[i].b,便是与后面的距离差。tree[i].a(sum1(20000,1)-sum-(i-p-1)*tree[i].b)。比如算3这个位置时,3的坐标前面有1,后面有 5 6,我们知道的是 3-1,3-6,3-5。
好好理解一下就好,上代码。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#define lowbit(x) x&(-x)
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
ll t[maxn];
int s[maxn];
struct node
{
int a,b;
}tree[maxn];
int cmp(node x,node y)
{
if(x.a==y.a)
return x.b<y.b;
return x.a<y.a;
}
void init1(int x,int y)//记录的是与这个点的距离
{
if(x==0)
return;
while(x<=maxn)
{
t[x]+=y;
x+=lowbit(x);
}
}
void init2(int x)//记录的是与这个点前面点的个数
{
if(x==0)
return;
while(x<=maxn)
{
s[x]+=1;
x+=lowbit(x);
}
}
int sum1(int x)//距离的和
{
ll ans=0;
while(x>0)
{
ans+=t[x];
x-=lowbit(x);
}
return ans;
}
int sum2(int x)//个数的和
{
ll ans=0;
while(x>0)
{
ans+=s[x];
x-=lowbit(x);
}
return ans;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d %d",&tree[i].a,&tree[i].b);
}
sort(tree+1,tree+n+1,cmp);
ll ans=0;
for(int i=1;i<=n;i++)
{
ll sum=sum1(tree[i].b);//距离
int p=sum2(tree[i].b);//个数
ans+=tree[i].a*(tree[i].b*p-sum)+tree[i].a*(sum1(20000)-sum-(i-p-1)*tree[i].b);//距离等于这个位置前面的数的距离和这个位置后面的距离,因为按照音量排序,所以这个点现在是最大的v
init1(tree[i].b,tree[i].b);
init2(tree[i].b);
}
printf("%lld\n",ans);
return 0;
}