MooFest

Time Limit: 1000MS Memory Limit: 30000K

Description

Every year, Farmer John’s N (1 <= N <= 20,000) cows attend “MooFest”,a social gathering of cows from around the world. MooFest involves a variety of events including haybale stacking, fence jumping, pin the tail on the farmer, and of course, mooing. When the cows all stand in line for a particular event, they moo so loudly that the roar is practically deafening. After participating in this event year after year, some of the cows have in fact lost a bit of their hearing.

Each cow i has an associated “hearing” threshold v(i) (in the range 1…20,000). If a cow moos to cow i, she must use a volume of at least v(i) times the distance between the two cows in order to be heard by cow i. If two cows i and j wish to converse, they must speak at a volume level equal to the distance between them times max(v(i),v(j)).

Suppose each of the N cows is standing in a straight line (each cow at some unique x coordinate in the range 1…20,000), and every pair of cows is carrying on a conversation using the smallest possible volume.

Compute the sum of all the volumes produced by all N(N-1)/2 pairs of mooing cows.

Input

  • Line 1: A single integer, N

  • Lines 2…N+1: Two integers: the volume threshold and x coordinate for a cow. Line 2 represents the first cow; line 3 represents the second cow; and so on. No two cows will stand at the same location.

Output

  • Line 1: A single line with a single integer that is the sum of all the volumes of the conversing cows.

Sample Input

4
3 1
2 5
2 6
4 3

Sample Output

57

思路:

看这题目一开始想到的肯定就是暴力的方式将全部可能算出来,但是题目数据太大,20000如果暴力的话就是O(n ^ 2),所以肯定就不可能暴力枚举,所以要用树状数组,而这题刚开始真的很难看出是树状数组(我也是参考了别人的思路,才懂原来是树状数组),然后就是首先排序,将val从小到大排序,之后因为每次都要取val最大的,所以这样遍历的话直接取就行了,然后就要用到树状数组了,一个取存id数组的和,一个存有多少个id,有多少个id是为了知道有多少小于遍历中的id,以及多少个大于的值,因为等下的相减有正有负,所以就需要将大于和小于的分出来,然后就是ans+=的那个公式就是这个的核心了,首先就是乘以val.
1 cow_num * node[i].id - cow_small就是算出小于node[i].id的和(是针对已经遍历过的元素,不是全部的)。
2 (cow_big - cow_small) - (i - cow_num) * node[i].id就是比id大的和了这样不断相加就是需要求的值了。

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 20010;
struct NODE {
    ll val;
    ll id;
    bool friend operator < (NODE a, NODE b) {
        if (a.val == b.val) return a.id < b.id;
        else return a.val < b.val;
    }
};
NODE node[maxn];
ll bit1[maxn] = {0}, bit2[maxn] = {0};
int lowbit(int x) {
    return x & (-x);
}
void updata(ll* bit, int x, ll val) {
    for (int i = x; i <= maxn; i += lowbit(i)) {
        bit[i] += val;
    }
}
ll getsum(ll* bit, int x) {
    ll sum = 0;
    for (int i = x; i >= 1; i -= lowbit(i)) {
        sum += bit[i];
    }
    return sum;
}
int main() {
    int n;
    ll ans = 0;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%lld %lld", &node[i].val, &node[i].id);
    sort(node, node + n);
    for (int i = 0; i < n; i++) {
        ll cow_num = getsum(bit1, node[i].id);
        ll cow_small = getsum(bit2, node[i].id);
        ll cow_big = getsum(bit2, maxn);
        ans += node[i].val * (cow_num * node[i].id - cow_small + (cow_big - cow_small) - (i - cow_num) * node[i].id);
        updata(bit1, node[i].id, 1);
        updata(bit2, node[i].id, node[i].id);
    }
    printf("%lld", ans);
    return 0;
}