/* <Br/> purely reference: http://hi.baidu.com/forverlin1204/blog/item/98cb564dcdcc25f5d62afc61.html <br/> tree array <br/> Title Description: Given a sequence of length N, the element has two properties X, h (x represents the coordinate, h Represents the height) <br/> calculate the sum of the un-adjusted values of any vertex (I, j, the un-adjusted value consists of F and S. <br/> F refers to the X coordinate of I and J (after discretization) absolute Value of difference F = ABS (X [I]-X [J]) <br/> S refers to the height of I and J (after discretization) S = min (H [I], H [J]). The product f * s of the two is the (I, j) value not adjustable <br/> analysis: we first sort by height from large to small, and then calculate the statistics so that we solve the problem of the smallest height <br/> for elements at the I position, the statistical height is greater than the X coordinate sum of H [I] than the X coordinate sum of H [I, number of smaller coordinates than x <br/> num = sum | x [I]-y [J] | * H [I] (j <I) sets the number of query1 (x) queries smaller than X, query2 (X) queries smaller than X and <br/> are equivalent to a * X [I]-sum1 + sum2-b * X [I] <br/> where a represents less than the current point X number of points, B indicates the number of points larger than the current vertex x, and sum1 indicates the X sum smaller than the current vertex x, sum2 indicates the sum of x and <br/> greater than the current vertex x, which can be expressed as a = query1 (X [I]-1), B = query1 (N) -query1 (X [I]), sum1 = query2 (X [I]-1), sum2 = query2 (N)-query2 (X [I]) <br/> ans = query (X [I]-1) * X [I]-query2 (X [I]-1) + query2 (N) -query2 (X [I])-(query1 (N)-query1 (X [I]) * X [I] <br/> then ans * = H [I], which is the smallest contribution of H as the current item <br/> */<br/> # Inc Lude <iostream> <br/> # include <cstdio> <br/> # include <algorithm> <br/> # define lowbit (x) (X & (-x )) <br/> using namespace STD; <br/> const int n = 100005; <br/> // typedef long ll; <br/> typedef _ int64 ll; <br/> struct node {<br/> int X, H; <br/>} node [N]; <br/> int N; <br/> ll sum [2] [N]; <br/> bool readint (Int & RET) {<br/> int SGN; <br/> char C; <br/> C = getchar (); <br/> If (C = EOF) <br/> return tr UE; <br/> while (C! = '-' & C <'0' | C> '9') <br/> C = getchar (); <br/> SGN = (C = '-')? -1: 1; <br/> ret = (C = '-')? 0: (C-'0'); <br/> while (C = getchar ()> = '0' & C <= '9 ') <br/> ret = RET * 10 + (c-'0'); <br/> RET * = SGN; <br/> return false; <br/>}< br/> void Init (int n) {<br/> for (INT I = 0; I <= N; I ++) <br/> sum [0] [I] = sum [1] [I] = 0; <br/>}< br/> void add (INT X, int Val, int flag) {<br/> while (x <= N) {<br/> sum [flag] [x] + = val; <br/> X + = lowbit (x); <br/>}< br/> ll query (int x, int flag) {<br/> ll NS = 0; <br/> while (x> 0) {<br/> ans + = sum [flag] [x]; <br/> X-= lowbit (x); <br/>}< br/> return ans; <br/>}< br/> bool cmpx (const node & A, const node & B) {return. x <B. X ;}< br/> bool cmph1 (const node & A, const node & B) {return. h <B. h ;}< br/> bool cmph2 (const node & A, const node & B) {return. h> B. h ;}< br/> int main () {<br/> while (scanf ("% d", & N )! = EOF) {<br/> for (INT I = 0; I <n; I ++) {<br/> // scanf ("% d ", & node [I]. x, & node [I]. h); <br/> readint (node [I]. x); <br/> readint (node [I]. h); </P> <p >}< br/> sort (node, node + N, cmpx); <br/> for (INT I = 0; I <n; I ++) {// X ranking <br/> int temp = node [I]. x; <br/> Int J = I + 1; <br/> node [I]. X = I + 1; <br/> while (temp = node [J]. X & J <n) {<br/> node [J]. X = I + 1; <br/> J ++; <br/>}< br/> I = J-1; <br/>}</P> <p> sort (node, node + N, cmph1); <br/> for (INT I = 0; I <N; I ++) {// H ranking <br/> int temp = node [I]. h; <br/> Int J = I + 1; <br/> node [I]. H = I + 1; <br/> while (temp = node [J]. H & J <n) {<br/> node [J]. H = I + 1; <br/> J ++; <br/>}< br/> I = J-1; <br/>}</P> <p> sort (node, node + N, cmph2); // height to small <br/> ll ans = 0; <br/> Init (n); <br/> for (INT I = 0; I <n; I ++) {<br/> Add (node [I]. x, 1, 0); <br/> Add (node [I]. x, node [I]. x, 1); <br/> int A = query (node [I]. x-1, 0); <br/> int B = query (n, 0)-query (node [I]. x, 0); <br/> ll sum1 = query (node [I]. x-1, 1); <br/> ll sum2 = query (n, 1)-query (node [I]. x, 1); <br/> ans + = (A * node [I]. x-sum1 + sum2-B * node [I]. x) * node [I]. h; <br/>}< br/> printf ("% i64d/N", ANS); <br/>}< br/>}