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
Num Array maintains a smaller number of cows than the current position
The DIS array maintains a distance of the cows smaller than the current position and
CNT is the total distance (I-NUM1) * a[i].x is greater than the distance of the current cow
1#include <cstdio>2#include <cstring>3#include <queue>4#include <cmath>5#include <algorithm>6#include <Set>7#include <iostream>8#include <map>9#include <stack>Ten#include <string> One#include <vector> A #definePi ACOs (-1.0) - #defineEPS 1e-6 - #defineFi first the #defineSe Second - #defineLson l,m,rt<<1 - #defineRson m+1,r,rt<<1|1 - #defineBug printf ("******\n") + #defineMem (A, B) memset (A,b,sizeof (a)) - #defineFuck (x) cout<< "[" <<x<< "]" <<endl + #defineF (a) a*a A #defineSF (n) scanf ("%d", &n) at #defineSFF (A, b) scanf ("%d%d", &a, &b) - #defineSFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) - #definePF printf - #defineFRE (i,a,b) for (i = A; I <= b; i++) - #defineFree (i,a,b) for (i = A; I >= b; i--) - #defineFRL (i,a,b) for (i = A; I < b; i++) in #defineFrll (i,a,b) for (i = A; i > b; i--) - #defineFIN freopen ("DATA.txt", "R", stdin) to #defineLowbit (x) x&-x + #pragmaComment (linker, "/stack:102400000,102400000") - the using namespacestd; *typedefLong LongLL; $ Const intMAXN = 2e5 +Ten;Panax Notoginseng intNUM[MAXN], DIS[MAXN], N; - structNode { the intv, x; + } A[MAXN]; A intCMP (node A, node B) { the returnA.V <B.V; + } - voidUpdateintXintKeyint*d) { $ while(x <=41000) { $D[X] + =key; -X + =lowbit (x); - } the } -LL sum (intXint*d) {WuyiLL ret =0; the while(X >0) { -RET + =D[x]; WuX-=lowbit (x); - } About returnret; $ } - intMain () { - while(SCANF ("%d", &n)! =EOF) { -MEM (num,0); AMem (DIS,0); + for(inti =0; I < n; i++) thescanf"%d%d", &A[I].V, &a[i].x); -Sort (A, A +N, CMP); $LL ans =0, cnt =0; the for(inti =0; I < n; i++) { theLL NUM1 =sum (a[i].x, num); theLL len =sum (a[i].x, dis); theAns + = A[I].V * (NUM1 * a[i].x-len + (Cnt-len)-(I-NUM1) *a[i].x); -CNT + =a[i].x; in Update (a[i].x, a[i].x, dis); theUpdate (a[i].x,1, num); the } Aboutprintf"%lld\n", ans); the } the return 0; the}
Moofest POJ-1990 tree-like array