POJ-1990 MooFest (树状数组 入门题)

来源:互联网 发布:约战竞技场出招优化 编辑:程序博客网 时间:2024/05/17 23:57
MooFest
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 8515 Accepted: 3854

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

43 12 52 64 3

Sample Output

57

#include <stdio.h>  #include <string.h>  #include <algorithm>using namespace std;#define maxn 20005int c[maxn], c1[maxn];struct tree{int pos, val;}a[maxn];bool mysort(tree e1, tree e2){return e1.pos < e2.pos;}inline int lowbit(int x){return x & (-x);}void add(int c[], int x, int v){while(x < maxn){c[x] += v;x += lowbit(x);}}int query(int c[], int x){int ans = 0;while(x){ans += c[x];x -= lowbit(x);}return ans;}int main(){int n;scanf("%d", &n);for(int i = 1; i <= n; ++i){scanf("%d %d", &a[i].val, &a[i].pos);}sort(a + 1, a + 1 + n, mysort);memset(c, 0, sizeof(c));memset(c1, 0, sizeof(c1));long long ans = 0, pos;int cnt;for(int i = 1; i <= n; ++i){cnt = query(c, a[i].val - 1);add(c, a[i].val, 1);pos = query(c1, a[i].val - 1);add(c1, a[i].val, a[i].pos);ans += 1LL * a[i].val * (cnt * a[i].pos - pos);}memset(c, 0, sizeof(c));memset(c1, 0, sizeof(c1));for(int i = n; i >= 1; --i){cnt = query(c, a[i].val);add(c, a[i].val, 1);pos = query(c1, a[i].val);add(c1, a[i].val, a[i].pos);ans += 1LL * a[i].val * (pos - cnt * a[i].pos);}printf("%lld\n", ans);}/*题意:2e4头牛在一条主线上,坐标范围2e4,声量范围2e4,两头牛之间交流的代价是两头牛之间的距离乘上两头牛声量的较大值,求所有牛相互交流的代价和。思路:暴力就是n方了,怎么优化呢,我们可以在声量上做文章。对于一头牛,它和其余声量小于它的牛交流,一定是用它的音量,这样我们只要统计声量小于它的牛的距离和,然后减减乘乘就行了。坐标范围不大,显然可以树状数组搞之。对于所有牛我们按坐标排序,然后统计每头牛前面有多少只牛声量比它小,然后再对距离求和,这都是树状数组简单应用了。正反各搞一遍。*/

原创粉丝点击