1990
Question:
Each ox has two attributes: V, X, and Sigma (max (V [I], V [J]) * ABS (X [I]-X [J]). 1 <= I <j <= N
Analysis: For the max function, we can sort the values of V from small to large, then ans = sigma (V [J] * ABS (X [I]-X [J]). 1 <= I <j <= N and V [I] <= V [J]
So how to deal with ABS functions? Separate discussion
Ans = sigma (V [J] * (X [I]-X [J]) x [I]> = x [J]
+ Sigma (V [J] * (X [J]-X [I]) x [I] <X [J]
How can we quickly obtain the sigma value? Tree array !!
Then we get the algorithm:
Sort by the V value from small to large, and maintain two arrays Dist using a tree array. The Dist [I] maintains the number coordinate (X axis) less than or equal to the I distance prefix and, CNT [I] indicates that at present, coordinate I has no ox (1 has, 0 has none ),
Maintains the number of cows whose coordinates are in [1, I] after the current sorting.
Then, after sorting out the order, enumerate the number of L coordinates of the current oX I, and the number of R coordinates of the current oX I, then, calculate the weight from ox I to other cattle (to ensure that ox I is currently used to calculate the maximum value of ox V)
Weight = V [I] * (L * X [I]-Dist. sum (X [I]) and + (Dist. sum (maxn)-Dist. sum (X [I])-R * X [I]) right weight and)
Dist. sum (X [I]) indicates the sum of all coordinates of the current coordinate on the left side of niui. Then l * X [I] Is sigma (X [I]-X [J]). X [I]> = x [J]
Dist. sum (maxn)-Dist. sum (X [I]) is the coordinate in [x [I], the-R * X [I] is the sum of the coordinates of all cattle between maxn]. sigma (X [J]-X [I]) x [I] <X [J]
After calculating the weight value, add the current cow information to the tree array.
CNT. Add (X [I], 1 );
Dist. Add (X [I], X [I]);
Single point update CNT [x [I] = 1, DIST [x [I] = x [I]
Code Attached
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <set> 5 # include <algorithm> 6 # include <map> 7 # include <queue> 8 # include <cmath> 9 # include <vector> 10 # define maxn 10001011 # define maxm 10001012 # define mod 100000000000000000013 # define INF 0x3f3f3f14 using namespace STD; 15 typedef long ll; 16 inline int lowbit (int x) {17 return X &-X; 18} 19 struct bit {20 int N; 21 LL Bit [maxn + 10]; 22 void Init (int n) {23 n = N; 24 for (INT I = 0; I <= N; ++ I) bit [I] = 0; 25} 26 void add (int x, int v) {27 for (INT I = x; I <= N; I + = lowbit (I) bit [I] + = V; 28} 29 ll sum (int x) {30 LL ans = 0; 31 for (INT I = X; i> 0; I-= lowbit (I) ans + = bit [I]; 32 return ans; 33} 34} Dist, CNT; 35 pair <int, int> cow [maxn]; 36 int main () {37 int N; 38 While (scanf ("% d", & N )! = EOF) {39 Dist. init (maxn); 40 CNT. init (maxn); 41 for (INT I = 1; I <= N; ++ I) {42 scanf ("% d", & cow [I]. first, & cow [I]. second); 43} 44 sort (cow + 1, cow + n + 1); 45 ll ans = 0; 46 for (INT I = 1; I <= N; ++ I) {47 int v = cow [I]. first; 48 int x = cow [I]. second; 49 ll l = CNT. sum (x); // The number of 50 LL r = CNT on the left. sum (maxn)-CNT. sum (x); // Number of 51 ans + = V * (L * x-dist.sum (x) on the right )); // The left weight and 52 ans + = V * (Dist. sum (maxn)-Dist. sum (x)-R * X); // right weight and 53 CNT. add (x, 1); 54 Dist. add (x, x); 55} 56 printf ("% LLD \ n", ANS); 57} 58}
View code