poj1990 MooFest
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.
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.
* 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
这题可以用两个一维树状数组做,先按分贝大小v进行排序,然后分别维护x位置的总和以及x位置前的点的个数。具体看代码。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 20050
#define ll long long
struct node{
int x,v;
}a[maxn];
int b1[maxn],b2[maxn];
bool cmp(node a,node b){
return a.v<b.v;
}
int lowbit(int x){
return x&(-x);
}
void update1(int pos,int num)
{
while(pos<=maxn){
b1[pos]+=num;pos+=lowbit(pos);
}
}
ll getsum1(int x)
{
ll num=0;
while(x>0){
num+=b1[x];x-=lowbit(x);
}
return num;
}
void update2(int pos,int num)
{
while(pos<=maxn){
b2[pos]+=num;pos+=lowbit(pos);
}
}
int getsum2(int x)
{
int num=0;
while(x>0){
num+=b2[x];x-=lowbit(x);
}
return num;
}
int main()
{
int n,m,i,j,num1,num2;
ll sum,t,sum1;
while(scanf("%d",&n)!=EOF)
{
memset(b1,0,sizeof(b1));
memset(b2,0,sizeof(b2));
for(i=1;i<=n;i++){
scanf("%d%d",&a[i].v,&a[i].x);
}
sort(a+1,a+1+n,cmp);
sum=sum1=0;
for(i=1;i<=n;i++){
update1(a[i].x,a[i].x);
update2(a[i].x,1);
if(i==1){
sum1+=a[i].x;continue;
}
num1=getsum2(a[i].x);
num2=i-1-num1;
t=getsum1(a[i].x);
sum+=a[i].v*( num1*a[i].x-t+sum1-t-a[i].x*num2 );
sum1+=a[i].x;
}
printf("%lld\n",sum);
}
return 0;
}