1. 程式人生 > >POJ1990 MooFest 樹狀陣列 題解(附圖)

POJ1990 MooFest 樹狀陣列 題解(附圖)

MooFest

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions:9207 Accepted: 4167

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. 

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

題意:現在有n頭牛,每頭有一個聽力v和距離x,兩頭牛之間說活需要的能量為max(v[1],v[2])*\left | x[1]-x[2] \right |

x[i]代表牛i所在的位置

v[i]代表牛i所需的能量

現在讓求\sum max(v[i],v[j])*abs(x[i]-x[j]);

思路:

先對牛按照V從小到大排序,這樣上式就可以改成\sum v[i]*abs(x[i]-x[j]) (i>j);

    而對於abs(x[i]-x[j]),我們可以將牛i之前的所有牛分成兩部分:x小於牛i的,x大於牛i

在數軸上表示,就是在i的左邊或者右邊

    對於i左邊的數,我們可以用x[i]*bufn - bufw (bufw為小於x的距離總和,bufn為x小於牛i的個數),這樣用容斥可以求出i左邊的絕對值之和

    對於i右邊的數,我們用所有距離減去牛i左邊的線條,再減去(牛i右邊的牛的數目*牛i的位置)就是右邊的絕對值之和

如下圖藍色部分就是我們要減去的部分

有了圖配合一下程式碼一下就能看出來了

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <string>
#include <vector>
#define For(a,b) for(ll a=1;a<=b;a++)
#define mem(a,b) memset(a,b,sizeof(a))
#define _mem(a,b) memset(a,0,(b+1)<<2)
#define lowbit(a) ((a)&-(a))
using namespace std;
typedef long long ll;
const ll maxn =  1e5;
const ll INF = 0x3f3f3f3f;
ll c[2][maxn];
ll n;
struct edge{
    ll v,w;        
}e[maxn];
bool cmp(edge a,edge b){return a.v<b.v;}
void update(ll x,ll d){
    for(ll i=x;i<=maxn-1;i+=lowbit(i))
        c[d][i] += d?x:1;
}
ll getsum(ll x,ll d){
    ll ans = 0;
    for(ll i=x;i;i-=lowbit(i))
        ans += c[d][i];
    return ans;
}
//0:數量統計
//1:座標和

int main()
{
    cin >> n;
    For(i,n)
        cin >> e[i].v >> e[i].w;
    sort(e+1,e+1+n,cmp);
    ll ans = 0;
    ll cnl,cnr;
    ll bufn,bufw;
    ll all = 0;
    For(i,n){
        bufn = getsum(e[i].w,0);        //比牛i小的牛的個數
        bufw = getsum(e[i].w,1);        //比牛i小的牛的位置總和
        cnl = bufn*e[i].w-bufw;         //牛i左邊
        cnr = all - bufw - (i-bufn-1)*e[i].w;//牛i右邊
        ans += (cnl+cnr)*e[i].v;
        all += e[i].w;                  //總長
        update(e[i].w,0);
        update(e[i].w,1);
    }
    cout << ans << endl;

    return 0;
}