Moofest
Time limit:1000 ms |
|
Memory limit:30000 K |
Total submissions:5235 |
|
Accepted:2260 |
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 special 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
--------------------Split line--------------
Question:
Given the hearing and coordinates of an nheaded ox, it is required for every two cows to talk (max (V (I), V (j) * ABS (DIS [I]-Dis [J]). A total of N * (n-1)/2 (n/1) conversations are required.
Ideas:
Sort the listening of the ox from small to large, so the I-th ox conversation needs to be calculated.
1: Number A, coordinate, and B of cattle whose coordinates are smaller than the number I of cattle
2: Number of cows whose coordinates are greater than the number of cows whose heads are I. C, coordinates, and D
In this case, we need:
(D-C * A [I]. Y + A * A [I]. Y-B) * A [I]. x
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#define M 20000+10#define ll __int64using namespace std;ll b[M],c[M];struct node{ int x,y; bool operator<(const node&a)const { return x<a.x; }}a[M];void update(int x,int v,ll *h){ for(int i=x;i<=M;i+=i&-i){ h[i]+=v; }}ll getsum(int x,ll *h){ ll sum=0; for(int i=x;i>0;i-=i&-i){ sum+=h[i]; } return sum;}int main(){ int n; scanf("%d",&n); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); for(int i=1;i<=n;++i){ scanf("%d %d",&a[i].x,&a[i].y); } sort(a+1,a+1+n); ll ans=0; for(int i=1;i<=n;++i){ ll x1=(ll)(getsum(a[i].y,b)*a[i].y-getsum(a[i].y,c)); ll x2=(ll)(getsum(M,c)-getsum(a[i].y,c)-(i-1-getsum(a[i].y,b))*a[i].y); ans+=(ll)(x1+x2)*a[i].x; update(a[i].y,1,b); update(a[i].y,a[i].y,c); } printf("%I64d\n",ans); return 0;}
Poj 1990 -- moofest (2 tree arrays)