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
* 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
Sample Input
4
3 1
2 5
2 6
4 3
Sample Output
57
我觉得我需要收回当初说树状数组比线段树简单这句话。。太坑了,一题比一题坑。。完全按题解写的
题意:给v【i】,x【i】要求所以的牛的音量和即x【i】-x【j】*max(v【i】,v【j】)之和
题解:两个树状数组数组一起使用,一个求x之前的比x坐标小的数(a),一个求x之前的比x坐标小的坐标和(b);
那么比x小的坐标和x的坐标的总坐标差是a*(e[i].x)-b;比x大的坐标和x的坐标的总坐标差是总坐标-b-(i-1-a)*e[i].x
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f3f; int s[][N];
struct edge{
ll v,x;
}e[N]; bool comp(const edge &a,const edge &b)
{
return a.v<b.v;
}
void add(int i,ll x,int d)
{
while(i<=N){
s[d][i]+=x;
i+=i&(-i);
}
}
ll sum(int i,int d)
{
ll ans=;
while(i>){
ans+=s[d][i];
i-=i&(-i);
}
return ans;
} int main()
{
ios::sync_with_stdio(false);
cin.tie();
// cout<<setiosflags(ios::fixed)<<setprecision(2);
int n;
while(cin>>n){
memset(s,,sizeof s);
for(int i=;i<=n;i++)cin>>e[i].v>>e[i].x;
sort(e+,e++n,comp);
ll ans=;
for(int i=;i<=n;i++)
{
ll a=sum(e[i].x,),b=sum(e[i].x,);
// cout<<sum(N,1)-b-(i-1-a)*e[i].x<<endl;
ans+=(a*e[i].x-b+sum(N,)-b-(i--a)*e[i].x)*e[i].v;
add(e[i].x,,);//0是比x小的牛的个数
add(e[i].x,e[i].x,);//1是比x小的牛的距离和
}
cout<<ans<<endl;
}
return ;
}