MooFest
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 7679 | Accepted: 3453 |
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
题目的要点在于用两个树状数组来维护,首先让每头牛以音量由小到大来排序,保证后面的牛都可以以自己的临界音量来和他前面 的牛交流。然后开两个树状数组,一个用于维护音量比第i头牛小的牛里面,下标比i小的牛的头数c1;另一个用于维护,下标比i小的牛所有下表和c2。
那么sum += 这头牛的音量*(下标比他小的所有牛和他的距离差之和+下标比他大的所有牛和他的距离差之和)。
so sum += cow[i].v*((下标比他小的牛的头数)*cow[i].id-(下标比他小的所有牛的下标和)+(下标比他大的所有牛的下标和)-(下标比他大的牛的头数)*cow[i].id);
知道这些跑树状数组维护就可以了。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 20000+10;
int n;
LL c1[maxn];//牛的个数
LL c2[maxn];//牛的下标和
struct node{
LL id;
LL v;
}cow[maxn];
bool cmp(node a,node b){
if(a.v == b.v)
return a.id < b.id;
return a.v < b.v;
}
int lowbit(int x){
return x&(-x);
}
void Add(int x,LL add,LL C[]){
for(int i = x; i <= 20000; i += lowbit(i))
C[i] += add;
}
LL sum(int x,LL C[]){
LL ans = 0;
for(int i = x; i > 0; i -= lowbit(i))
ans += C[i];
return ans;
}
int main(){
scanf("%d",&n);
LL Sum = 0;
for(int i = 0; i < n; i++)
scanf("%lld %lld",&cow[i].v,&cow[i].id);
sort(cow,cow+n,cmp);
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));
for(int i = 0; i < n; i++){
LL cownum = sum(cow[i].id,c1);//下标比他小的牛的个数
LL cowstep = sum(cow[i].id,c2);//下标比他小的牛的下标和
LL cowstepsum = sum(20000,c2);//下标比他大的牛的下标和
Sum += cow[i].v*(cownum*cow[i].id-cowstep+(cowstepsum-cowstep)-(i-cownum)*cow[i].id);
Add(cow[i].id,1,c1);
Add(cow[i].id,cow[i].id,c2);
}
printf("%lld\n",Sum);
return 0;
}