Title Source: POJ 1990 Moofest
Test instructions: Even if the sound between two cows is 2 the maximum value of the cow v * * The distance between the cows
Idea: Follow v from small to large insert a tree array because from small to large sort each insert a cow I current v maximum is the V of the ox I
Statistics x is smaller than his number S1 x is larger than his number S2 S2 is the number of cattle in the current tree-like array minus S1 in minus one.
Statistic X is smaller than his distance L1 and X greater than his distance L2 L2 is all bull X and-L1
X*S1-L1 is the sum of the distance of all cows and cows I on the left side of the ox I
L2-X*S2 is the sum of all cows and cows I on the right left side of the ox.
#include <cstdio> #include <cstring> #include <algorithm> using namespace std;
const int MAXN = 20010; struct Node {int v, x;}
A[MAXN];
Long Long C[2][MAXN];
int n, m;
BOOL CMP (Node A, Node B) {return A.V < B.V;} int lowbit (int x) {return x& (-X);
} void Update (int x, int y, int d) {while (x <= 20000) {c[d][x] + = y;
x + = Lowbit (x);
}} long long sum (int x, int d) {int ret = 0;
while (x > 0) {ret + = c[d][x];
X-= Lowbit (x);
} return ret;
} int main () {scanf ("%d", &n);
for (int i = 1; I <= n; i++) scanf ("%d%d", &A[I].V, &a[i].x);
Sort (a+1, a+n+1, CMP);
A long long ans = 0;
for (int i = 1; I <= n; i++) {int v = A[I].V;
int x = a[i].x;
Long Long S1 = SUM (20000, 0);
Long Long s2 = SUM (x, 0);
Long Long L1 = SUM (20000, 1);
Long Long L2 = SUM (x, 1);
Ans + = (x*s2-l2+l1-l2-x* (i-s2-1)) *v;
Update (a[i].x, 1, 0);
Update (a[i].x, a[i].x, 1);
} printf ("%lld\n", ans);
return 0;
}