首页 > 代码库 > poj 1990 mooFest

poj 1990 mooFest

I - MooFest
Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u
Submit Status

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

43 12 52 64 3

Sample Output

57
x,y
对x按升序,y按升序排序之后
所以要求所有的费用,只需要考虑当前的cow和它前面每头cow的费用
要用到三个树状树状
一个是记录当前cow前面有多少头cow的位置比它的位子小

一个是记录当前cow前面所有位置比它的位子小的总价值
一个是记录当前cow前面所有位置比它的位置大的总价值
 1 #include<iostream> 2 #include<string> 3 #include<cstdio> 4 #include<vector> 5 #include<queue> 6 #include<stack> 7 #include<algorithm> 8 #include<cstring> 9 #include<stdlib.h>10 #include<string>11 #include<cmath>12 using namespace std;13 #define pb push_back14 int n;15 __int64 p[20100],q[20100],qq[20100];16 struct node{17     int v,x;18 }so[20100];19 int cmp(node a,node b){20     if(a.v==b.v) return a.x<b.x;21     return a.v<b.v;22 }23 void update1(int pos,int num){24     while(pos<=20000){25         p[pos]+=num;26         pos+=pos&(-pos);27     }28 }29 __int64 getnum1(int pos){30    __int64 sum=0;31     while(pos>=1){32         sum+=p[pos];33         pos-=pos&(-pos);34     }35     return sum;36 }37 void update2(int pos,int num){38     while(pos<=20000){39         q[pos]+=num;40         pos+=pos&(-pos);41     }42 }43 __int64 getnum2(int pos){44     __int64 sum=0;45     while(pos>=1){46         sum+=q[pos];47         pos-=pos&(-pos);48     }49     return sum;50 }51 void update3(int pos,int num){52     while(pos>=1){53         qq[pos]+=num;54         pos-=pos&(-pos);55     }56 }57 __int64 getnum3(int pos){58       __int64 sum=0;59         while(pos<=20000){60             sum+=qq[pos];61             pos+=pos&(-pos);62         }63         return sum;64 }65 int main(){66     while(cin>>n){67         memset(p,0,sizeof(p));68         memset(q,0,sizeof(q));69         for(int i=1;i<=n;i++)70             scanf("%d%d",&so[i].v,&so[i].x);71         sort(so+1,so+1+n,cmp);72         __int64 sum=0;73         for(int i=1;i<=n;i++){74             __int64 a,b,c;75             a=getnum1(so[i].x);//位置比当前位置小的cow的数量76             b=getnum2(so[i].x);//位置比当前位置小的总价值77             c=getnum3(so[i].x);//位置比当前位置大的总价值78             sum+=(a*so[i].x-b+c-(i-1-a)*so[i].x)*so[i].v;79             update1(so[i].x,1);80             update2(so[i].x,so[i].x);81             update3(so[i].x,so[i].x);82         }83         printf("%I64d\n",sum);84     }85 }