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.
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.
* 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.
* 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.
* Line 1: A single line with a single integer that is the sum of all the volumes of the conversing cows.
4 3 1 2 5 2 6 4 3
57
题解:求两只牛之间距离与两只牛听力最大值的乘积--dis*max(v1,v2).首先将听力按从小到大排序,这样后面的牛与前面的牛的听力的最大值就是后面的牛的听力值,再用四个树状数组分别记录比这只牛听力小和比这只牛听力大的牛的听力总和,和牛的数量,ss(sumsmall),sl(sumlarge),cs(cntsmall),cl(cntlarge),听力小的采用向上更新,向下求和,听力大的采用向下更新,向上求和。累加ans+=(pos*cs-ss+sl-pos*cl)*v.(当然有更好的保存和更新方式,这种比较简单粗暴)
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long ll;
const int Maxn=20005;
int n,sums[Maxn],cnts[Maxn],suml[Maxn],cntl[Maxn];
struct node{
int v,pos;
}a[Maxn];
bool cmp(node a,node b){
return a.v<b.v;
}
int lowbit(int x){
return x&(-x);
}
void updata(int x,int val){
while(x<=Maxn){
sums[x]+=val;
cnts[x]++;
x+=lowbit(x);
}
}
void downdata(int x,int val){
while(x>0){
suml[x]+=val;
cntl[x]++;
x-=lowbit(x);
}
}
void getSums(int x,int &s,int &c){
while(x>0){
s+=sums[x];
c+=cnts[x];
x-=lowbit(x);
}
}
void getSuml(int x,int &s,int &c){
while(x<=Maxn){
s+=suml[x];
c+=cntl[x];
x+=lowbit(x);
}
}
int main(){
//freopen("in.txt","r",stdin);
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d%d",&a[i].v,&a[i].pos);
sort(a+1,a+1+n,cmp);
ll ans=0;int ss,cs,sl,cl;
for(int i=1;i<=n;i++){
ss=cs=sl=cl=0;
getSums(a[i].pos,ss,cs);
getSuml(a[i].pos,sl,cl);
ans+=(long long)(a[i].pos*(cs-cl)-ss+sl)*a[i].v;
updata(a[i].pos,a[i].pos);
downdata(a[i].pos,a[i].pos);
}
printf("%I64d\n",ans);
}