poj 1990 MooFest (树状数组)

MooFest
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 4888   Accepted: 2058

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

 1 //600K    63MS    C++    1220B    2014-05-04 20:52:18
 2 /* 
 3 
 4     题意:
 5         知道v和x,求每对间 max(vi,vj)与abs(vi-vj)的乘积的和,对v排序,然后每个v的计算公式:
 6             ans+=p[i].v*(cnt*p[i].x-sum+tsum-sum-(i-cnt-1)*p[i].x);
 7         知道计算的方法还是挺简单的! 
 8 
 9 */
10 #include<stdio.h>
11 #include<string.h>
12 #include<stdlib.h>
13 #define N 20005
14 struct node{
15     int v,x;
16 }p[N];
17 __int64 cnum[N],clen[N];
18 int cmp(const void*a,const void*b)
19 {
20     return (*(node*)a).v-(*(node*)b).v;
21 }
22 int lowbit(int k)
23 {
24     return (-k)&k;
25 }
26 void update(__int64 c[],int k,int detal)
27 {
28     for(;k<N;k+=lowbit(k))
29         c[k]+=detal;
30 }
31 __int64 getsum(__int64 c[],int k)
32 {
33     __int64 s=0;
34     for(;k>0;k-=lowbit(k))
35         s+=c[k];
36     return s;
37 }
38 int main(void)
39 {
40     int n;
41     while(scanf("%d",&n)!=EOF)
42     {
43         memset(cnum,0,sizeof(cnum));
44         memset(clen,0,sizeof(clen));
45         for(int i=1;i<=n;i++)
46             scanf("%d%d",&p[i].v,&p[i].x);
47         qsort(p+1,n,sizeof(p[0]),cmp);
48         __int64 ans=0;
49         update(cnum,p[1].x,1);
50         update(clen,p[1].x,p[1].x);
51         for(int i=2;i<=n;i++){
52             __int64 cnt=getsum(cnum,p[i].x);
53             __int64 sum=getsum(clen,p[i].x);
54             __int64 tsum=getsum(clen,N-1);
55             ans+=p[i].v*(cnt*p[i].x-sum+tsum-sum-(i-cnt-1)*p[i].x);
56             update(cnum,p[i].x,1);
57             update(clen,p[i].x,p[i].x);
58         }
59         printf("%I64d\n",ans);
60     }
61     return 0;
62 }

 

posted @ 2014-05-04 20:55  heaventouch  阅读(375)  评论(0编辑  收藏  举报