Rogu P2345 and p2345
Background
MooFest, 2004 Open
Description
John's N cows attend the "Golden Week" every year ". The Golden Week Conference is a big event in the dairy industry. The activities at the meeting were very
Many, such as heap hay, cross-fence, and cowboy ass. They gather together during the activity. The coordinates of the I-head cows are Xi, and the coordinates of no two cows are the same. The cows call a lot. When the I-th and j-th cows communicate, the max {Vi; Vj} × | xi−xj | volume is displayed, vi and Vj are respectively the listening of the I and j cows. Assuming that every pair of cows are talking at the same time, calculate the total volume of all cows.
Input/Output Format
Input Format:
• Row 1: single integer N, 1 ≤ N ≤ 20000
• Line 2 to line N + 1: line I + 1 has two integers Vi and Xi, 1 ≤ Vi ≤ 20000; 1 ≤ Xi ≤ 20000
Output Format:
• Single INTEGER: The sum of the volume produced by all cows
Input and Output sample input sample #1: Copy
43 12 52 64 3
Output example #1: Copy
57
Description
Simple O (N2)
Similar to binary O (N logN) in Merge Sorting)
Tree array O (N logN)
V * + abs (a_1-X) + V * abs (a_2-X) + V * abs (a_3-X) + ....
\ = V * (abs (a_1-X) + abs (a_2-X) + abs (a_3-X) + .......)
\\
= V * (a_1-X + a_2-X + X-a_3)
\\
= V * (-N * X + (a_1 + a_2 +... + a_N) + M * X-(a_3 +... a_M)
Question of the formula
Set the volume of the current cow to V, the coordinates to X, and the ai to represent the coordinates of the first cow.
Suppose a1, a2> X, a3 <X (easy to understand)
We found that abs does not meet the allocation rate (that is, abs (a + B )! = Abs (a) + abs (B ))
In this case, we will discuss the situation
The coordinates of n cows are greater than X, and the coordinates of m cows are smaller than X.
So for N, M, a1 + a2 +..., a3 + ,,,
The idea of reverse order pair using a tree Array
We can use two tree Arrays for maintenance.
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <algorithm> 6 # include <deque> 7 # include <queue> 8 # define LL long 9 # define lb (x) (x) & (-x) 10 using namespace std; 11 const ll maxn = 40000001; 12 inline LL read () 13 {14 char c = getchar (); LL x = 0, f = 1; 15 while (c <'0' | c> '9') {if (c = '-') f =-1; c = getchar ();} 16 while (c> = '0' & c <= '9') x = x * 10 + c-48, c = getchar (); return x * f; 17} 18 struct node19 {20 LL v, x; 21} cow [MAXN]; 22 LL n; 23 int comp (const node &, const node & B) 24 {25 return. v <B. v; 26} 27 LL tree_num [MAXN]; 28 LL tree_sum [MAXN]; 29 ll maxx; 30 inline void Point_Add (LL pos, LL val, bool how) 31 {32 while (pos <= MAXX) 33 {34 if (how = 1) tree_num [pos] + = val; 35 else tree_sum [pos] + = val; 36 pos + = lb (pos); 37} 38} 39 inline LL Interval_Ask (LL pos, bool how) 40 {41 LL ans = 0; 42 while (pos) 43 {44 if (how = 1) ans = ans + tree_num [pos]; 45 else ans = ans + tree_sum [pos]; 46 pos-= lb (pos ); 47} 48 return ans; 49} 50 int main () 51 {52 n = read (); 53 for (LL I = 1; I <= n; I ++) cow [I]. v = read (), cow [I]. x = read (), MAXX = max (MAXX, cow [I]. x); 54 sort (cow + 1, cow + n + 1, comp); 55 LL ans = 0; 56 for (LL I = 1; I <= n; I ++) 57 {58 // 1: number 0: and 59 ans + = cow [I]. v * (-cow [I]. x * (Interval_Ask (MAXX, 1)-Interval_Ask (cow [I]. x, 1) + (Interval_Ask (MAXX, 0)-Interval_Ask (cow [I]. x, 0) + 60 cow [I]. x * Interval_Ask (cow [I]. x, 1)-(Interval_Ask (cow [I]. x, 0); 61 Point_Add (cow [I]. x, 1, 1); 62 Point_Add (cow [I]. x, cow [I]. x, 0); 63} 64 printf ("% lld", ans); 65 return 0; 66}