POJ 1990 MooFest 树状数组

MooFest
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 9189   Accepted: 4158

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

Source

USACO 2004 U S Open

题意 给你若干牛的声贝值和他们的x坐标 叫你求每两个牛之间的距离乘上他们两个的最大声贝值

我们怎么分析呢?我们这样考虑

大声贝的牛出现 会影响乘法的声贝值

那么我们按照声贝排序 按小到大的顺序 这样每次我乘法的声贝值都是取新加的这个牛

这个牛x坐标前面有几个牛呢? 维护一个lsum 左边的牛 那么右边的牛数目自然为i-1-lsum

那么左边的牛的距离和是多少呢?是lsum * x - lsumdisx(左边牛的距离和) 那么右边牛的距离自然为sumdisx(总距离和)-lsumdisx-(i-1-lsum)*x

那么我们这样树状数组 维护一个 数量前缀数组a

距离前缀和数组b 就可以实现了

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAX_N = 20024;
long long a[MAX_N],b[MAX_N],xsum[MAX_N];
struct node {
    int x;
    int v;
    bool operator < ( const node &t) const {
         return v<t.v;
    }
}col[MAX_N];
long long getsum(int x,long long *arr){
    long long ans = 0;
    for(int i = x;i;i-=i&(-i)) ans+=arr[i];
    return ans;
}
void change(int x,long long dis,long long *arr,long long v){
   for(;x<=dis;x+=x&(-x)) arr[x]+=v;
}

int main(){
    int n;
    scanf("%d",&n);
    long long dis = -1;
    for(int i=1;i<=n;++i){
        scanf("%d%d",&col[i].v,&col[i].x);
        if(col[i].x>=dis) dis= col[i].x;
    }
    sort(col+1,col+1+n);
    long long ans = 0;
    for(int i=1;i<=n;++i) xsum[i] = xsum[i-1]+col[i].x;
    for(int i=1;i<=n;++i){
      long long dl = getsum(col[i].x,a);
      long long dr = i-1-dl;
      long long lsum = getsum(col[i].x,b);
      long long rsum = xsum[i-1]-lsum;
      ans+=col[i].v*(dl*col[i].x-lsum);
      ans+=col[i].v*(rsum-dr*col[i].x);
      change(col[i].x,dis,a,1);
      change(col[i].x,dis,b,col[i].x);
    }
    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/heucodesong/article/details/80696818