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 worl D. Moofest involves a variety of events including haybale stacking, fence jumping, pin the tail on the farmer, and of Cour SE, mooing. When the cows all stand on 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 has in fact lost a bit of their hearing.
Each cow I have 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 both cows in order to be hear D by cow I. If the 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 are standing in a straight line (each cow at some unique x coordinate in the range 1..20,000), a nd every pair of cows is carrying on a conversation using the smallest possible volume.
Compute the sum of 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. Cows'll stand at the same.
Output
* Line 1: A single line with a single integer this is the sum of the volumes of the conversing cows.
Sample Input
43 12 52) 64 3
Sample Output
57
First according to the hearing value from small to large order, to ensure that the current cow with the front of the contact, take the current cow's hearing value, and then use two tree-like array, a record of all the coordinates of the number of locations, so that you can query the number is greater than the current coordinates and less than the current number of coordinates, and another record all This makes it possible to query all coordinates that are less than and greater than the current coordinates, and finally use the current listening value * (greater than the current coordinates and-greater than the current coordinates) * current coordinates + less than the current coordinate number * Current coordinates-smaller than the current coordinates and the coordinates.
Code:
#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);//is less than the coordinates of the current coordinate and long long y1=temp-x1;//is greater than the coordinates of the current coordinates and int x2=getsum2 (A[I].Y),///less than the current coordinate number int y2=i-x2;//is greater than the number of current coordinates 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 (tree-like array)