# 洛谷P2345 奶牛集会

## 题目背景

MooFest, 2004 Open

## 输入输出格式

• 第一行：单个整数N，1 ≤ N ≤ 20000

• 第二行到第N + 1 行：第i + 1 行有两个整数Vi 和Xi，1 ≤ Vi ≤ 20000; 1 ≤ Xi ≤ 20000

• 单个整数：表示所有奶牛产生的音量之和

## 输入输出样例

4
3 1
2 5
2 6
4 3

57

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

long long n,c[50010],maxn,num[50010],ans;

struct node
{
long long v,x;
}e[20010];

long long lowbit(long long x)
{
return x & (-x);
}

void add(long long x,long long d)
{
while (x <= maxn)
{
c[x] += d;
x += lowbit(x);
}
}

long long query(long long x)
{
long long cnt = 0;
while (x)
{
cnt += c[x];
x -= lowbit(x);
}
return cnt;
}

void add2(long long x,long long d)
{
while (x <= maxn)
{
num[x] += d;
x += lowbit(x);
}
}

long long query2(long long x)
{
long long cnt = 0;
while (x)
{
cnt += num[x];
x -= lowbit(x);
}
return cnt;
}

bool cmp(node a,node b)
{
return a.v < b.v;
}

int main()
{
scanf("%lld",&n);
for (int i = 1; i <= n; i++)
{
scanf("%lld%lld",&e[i].v,&e[i].x);
maxn = max(maxn,e[i].x);
}
sort(e + 1,e + 1 + n,cmp);
long long t = 0;
for (int i = 1; i <= n; i++)
{
long long x = e[i].x,v = e[i].v;
long long x1 = query(x - 1),num1 = query2(x - 1);
long long num2 = i - 1 - num1,x2 = t - x1;
t += e[i].x;
ans += v * (num1 * x - x1 + x2 - num2 * x);