首页 > 代码库 > POJ 1990 MooFest(树状数组)

POJ 1990 MooFest(树状数组)

MooFest
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 5395 Accepted: 2329

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


先按听力值从小到大排个序,保证当前的牛与前面的联系时,取当前牛的听力值,再用两个树状数组,一个记录所有所有坐标点的位置个数,这样就可以查询大于当前坐标的个数和小于当前坐标的个数,另一个记录所有坐标点的和,这样就可以查询小于和大于当前坐标的所有坐标的和,最后用当前听力值*(大于当前坐标的坐标和—大于当前坐标的个数*当前坐标+小于于当前坐标的个数*当前坐标—小于当前坐标的坐标和)。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=20000+1000;
struct node
{
    int x;
    int y;
}a[maxn];
long long b[maxn];
int c[maxn];
bool cmp(node p,node q)
{
    return p.x<q.x;
}
int low(int k)
{
    return (k&(-k));
}
void update(int k,int v)
{
    while(k<maxn)
    {
        b[k]+=v;
        k+=low(k);
    }
}
void update2(int k,int v)
{
    while(k<maxn)
    {
        c[k]+=v;
        k+=low(k);
    }
}
long long getsum(int k)
{
    long long ans=0;
    while(k>0)
    {
        ans+=b[k];
        k-=low(k);
    }
    return ans;
}
int getsum2(int k)
{
    int ans=0;
    while(k>0)
    {
       ans+=c[k];
       k-=low(k);
    }
    return ans;
}
int main()
{
    long long ans;
    int n;
    while(~scanf("%d",&n))
    {
        memset(a,0,sizeof(a));
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
        }
        ans=0;
        sort(a,a+n,cmp);
        long long temp=0;
        for(int i=0;i<n;i++)
        {
            long long x1=getsum(a[i].y);//小于当前坐标的坐标和
            long long y1=temp-x1;//大于当前坐标的坐标和
            int x2=getsum2(a[i].y);//小于当前坐标的个数
            int y2=i-x2;//大于当前坐标的个数
            ans+=(long long)a[i].x*(y1-a[i].y*y2);
            ans+=(long long)a[i].x*(a[i].y*x2-x1);
            update(a[i].y,a[i].y);
            update2(a[i].y,1);
            temp+=a[i].y;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


POJ 1990 MooFest(树状数组)