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
题目大意:一群牛参加完牛的节日后都有了不同程度的耳聋,第i头牛听见别人的讲话,别人的音量必须大于v[i],当两头牛i,j交流的时候,交流的最小声音为max{v[i],v[j]}*他们之间的距离。现在有n头牛,求他们之间两两交流最少要的音量和。
最近看书复习期末,感觉自己都变傻了。这么一道题看了很久。一道树状数组的应用,在于求数轴的距离,以我现在水平,看到这道题,一脸蒙蔽。
思路 要求max(v[i],v[j])*(dis[i]-dis[j])由于要求所有牛的总能量,先按坐标排序,如果我们只考虑听力的大小递增是一个方向的那么是可以求的,然后运用树状数组优化,由于这些牛听力不是递增,那么我们可以想到的是,先正向求一遍再反向求一遍。
那么如何快速求到当前牛前面比这头牛听力差的总距离差,用减法,当前牛的坐标乘上小于当前牛听力的牛的数量再减去小于当前听力的这些牛离原点的总距离和,反向也是这样画图表示
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAXN = 20005;
#define ll long long
struct node{
int pos,v;
}p[MAXN];
int n;
ll dis[MAXN],num[MAXN];//dis保存前面所有牛位置的坐标总和。
// num 数组保存的是前面所有牛的个数
bool cmp(node a,node b){
return a.pos < b.pos;
}
int lowbit(int x){
return x&(-x);
}
ll sum(int pos,ll x[]){
ll ans = 0;
while (pos > 0){
ans += x[pos];
pos -= lowbit(pos);
}
return ans;
}
void add(int pos,int num,ll x[]){
while (pos < MAXN){
x[pos] += num;
pos += lowbit(pos);
}
}
int main(){
scanf("%d",&n);
for (int i = 0; i < n; i++)
scanf("%d%d",&p[i].v,&p[i].pos);
sort(p,p+n,cmp);
memset(dis, 0, sizeof(dis));
memset(num, 0, sizeof(num));
ll ans = 0;
for (int i = 0; i < n; i++){
ans += p[i].v * (p[i].pos*sum(p[i].v, num) - sum(p[i].v,dis));
//当前牛的距离*乘上小于这头牛音量的牛的数量,因为按照坐标更新,所有出现过的牛都在当前牛前面
//(不包括当前牛,所以当前牛位置的个数还没更新)得到的是牛之间的距离差
//减去前面所有牛的距离和。就是每一个头牛所在位置的坐标
add(p[i].v, p[i].pos, dis);
add(p[i].v, 1, num);
}
memset(dis,0,sizeof(dis));
memset(num,0,sizeof(num));
for (int i = n-1; i >= 0; i--){
ans += p[i].v * (sum(p[i].v-1,dis) - p[i].pos*sum(p[i].v-1,num));//不可能大于;
//反向求距离差,因为p[i].pos*sum(p[i].v-1,num))不可能大于距离总和。
//总的前面的听力小的牛距离减去听力比当前牛小的正向距离,剩下的就是反向距离,也就是距离差。用数轴表示非常简单,以后要学会画图了。
add(p[i].v, p[i].pos, dis);
add(p[i].v, 1, num);
}
cout<<ans<<endl;
return 0;
}