I-moofest
Time limit:1000 ms
Memory limit:30000kb
64bit Io format:% I64d & % i64usubmit 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 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
X, Y
Sort X in ascending order and Y in ascending order
Therefore, you only need to consider the cost of the current cow and the cost of each front cow.
Three tree trees are used.
One is to record the number of head cows in front of the Current Cow, which is smaller than its seat
One is to record the total value of all the positions in front of the Current Cow smaller than its seat
One is to record the total value of all positions before the current cow, which is greater than its position
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 (. V = B. v) return. x <B. x; 21 return. 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", & 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); // number of cows with smaller positions than the current position 76 B = getnum2 (so [I]. x); // The total value of the position smaller than the current position 77 C = getnum3 (so [I]. x); // The total value of the position greater than the current position 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}