HDU1990 MooFest(树状数组)

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

PS:这个题困了我好久,最后终于想通了。题意:当两头牛i,j交流的时候,交流的最小声音为max{v[i],v[j]}*他们之间的距离。现在有n头牛,求他们之间两两交流最少要的音量和。如果用暴力的话肯定会超时的,所以现在可以用树状数组。首先将牛按发出声音的值,按从小到大排序。然后声明两个数组,一个记录当前输入的牛的前面位置比它小的牛的个数,一个记录这些牛的位置和。现在就是怎么求当前牛与其他牛的不同值了。这个分两部分计算,一个是位置比当前牛小的牛和位置比当前牛大的牛。位置小的牛很好计算,就是当前牛的听力值乘以比当前牛位置小的所有牛的位置差。比它大的牛计算有点麻烦。现在用树状数组求出当前输入的所有牛的位置和,减去位置比它小的牛的位置和,位置比它大的牛的位置和。在乘以他的听力值,就是这头牛与其他牛的所有不同值得和,因为按听力排序的,所以当前牛的听力值是最大的,直接乘就行了。

AC代码:

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>
const int maxn=2e4+5;
const int mod=1e9+7;
const int inf=1e9;
#define me(a,b) memset(a,b,sizeof(a))
typedef long long ll;
using namespace std;
ll s[maxn],c[maxn];
struct node
{
    int v,i;
    bool friend operator<(node a,node b)
    {
        if(a.v==b.v)
            return a.i<b.i;
        return a.v<b.v;
    }
}a[maxn];
int lowbit(int x)
{
    return x&(-x);
}
void update(ll a[],int x,int s)
{
    while(x<=maxn)
    {
        a[x]+=s;
        x+=lowbit(x);
    }
}
ll getsum(ll a[],int x)
{
    int sum=0;
    while(x)
    {
        sum+=a[x];
        x-=lowbit(x);
    }
    return sum;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
            scanf("%lld%lld",&a[i].v,&a[i].i);
        sort(a,a+n);
        ll sum=0;
        for(int i=0;i<n;i++)
        {
            ll xzs=getsum(c,a[i].i);//下标比他小的牛的个数
            ll xs=getsum(s,a[i].i);//下标比他小的牛的下标和
            ll ds=getsum(s,maxn);//当前输入所有牛的下标和
            sum+=a[i].v*(xzs*a[i].i-xs+(ds-xs)-(i-xzs)*a[i].i);
            //(xzs*a[i].i-xs),位置比当前牛小的位置差的和。((ds-xs)-(i-xzs)*a[i].i)比当前牛位置大的位置差的和。
            update(c,a[i].i,1);
            update(s,a[i].i,a[i].i);
        }
        printf("%lld\n",sum);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41292370/article/details/81331651