HDU3015Disharmony Trees(树状数组+离散化)

Problem Description

One day Sophia finds a very big square. There are n trees in the square. They are all so tall. Sophia is very interesting in them.


She finds that trees maybe disharmony and the Disharmony Value between two trees is associated with two value called FAR and SHORT.

The FAR is defined as the following:If we rank all these trees according to their X Coordinates in ascending order.The tree with smallest X Coordinate is ranked 1th.The trees with the same X Coordinates are ranked the same. For example,if there are 5 tree with X Coordinates 3,3,1,3,4. Then their ranks may be 2,2,1,2,5. The FAR of two trees with X Coordinate ranks D1 and D2 is defined as F = abs(D1-D2).

The SHORT is defined similar to the FAR. If we rank all these trees according to their heights in ascending order,the tree with shortest height is ranked 1th.The trees with the same heights are ranked the same. For example, if there are 5 tree with heights 4,1,9,7,4. Then their ranks may be 2,1,5,4,2. The SHORT of two trees with height ranks H1 and H2 is defined as S=min(H1,H2).

Two tree’s Disharmony Value is defined as F*S. So from the definition above we can see that, if two trees’s FAR is larger , the Disharmony Value is bigger. And the Disharmony value is also associated with the shorter one of the two trees. 

Now give you every tree’s X Coordinate and their height , Please tell Sophia the sum of every two trees’s Disharmony value among all trees.

Input

There are several test cases in the input

For each test case, the first line contain one integer N (2 <= N <= 100,000) N represents the number of trees.

Then following N lines, each line contain two integers : X, H (0 < X,H <=1,000,000,000 ), indicating the tree is located in Coordinates X and its height is H.

Output

For each test case output the sum of every two trees’s Disharmony value among all trees. The answer is within signed 64-bit integer.

思路:

结构体存储数据,然后根据数据进行排序,将排好的数据放入树状数组中,具体看代码注释。

#include <bits/stdc++.h>
#define mem(ar,num) memset(ar,num,sizeof(ar))
#define me(ar) memset(ar,0,sizeof(ar))
#define lbt(x) (x&(-x))
#define IOS ios::sync_with_stdio(false)
#define ll long long
using namespace std;
int n,w[100010],e[100010];
struct node{
    int x,h,xx,hh;
};
void add(int c[],int x,int d){
    while(x <= n){
        c[x]+=d;x+=lbt(x);
    }
}
ll sum(int c[],int x){
    ll ret=0;
    while(x>0){
        ret+=c[x]; x-=lbt(x);
    }
    return ret;
}
node a[100010];
void add1(int i,int x){
    for(;i<=n;i+=lbt(i)){
        a[i].x+=x;
    }
}
void add2(int i,int x){
    for(;i<=n;i+=lbt(i)){
        a[i].h+=x;
    }
}
bool cmp1(node a,node b){
    return a.xx<b.xx;
}
bool cmp2(node a,node b){
    return a.hh<b.hh;
}
bool cmp3(node a,node b){
    return a.h>b.h;
}
int main(){IOS;
    while(cin>>n){
        memset(w,0,sizeof(w));
        memset(e,0,sizeof(e));
        for(int i=1;i<=n;i++){
            cin>>a[i].xx>>a[i].hh;
        }
        sort(a+1,a+n+1,cmp1);a[1].x=1;
        for(int i=2;i<=n;i++){
            if(a[i].xx!=a[i-1].xx){
                a[i].x=i;
            }
            else
                a[i].x=a[i-1].x;
        }
        sort(a+1,a+n+1,cmp2);a[1].h=1;
        for(int i=2;i<=n;i++){
            if(a[i].hh!=a[i-1].hh){
                a[i].h=i;
            }
            else
                a[i].h=a[i-1].h;
        }
        ll ans=0;
        sort(a+1,a+n+1,cmp3);
        for(int i=1;i<=n;i++){
            ll x = sum(w,a[i].x);//a[i].xx之前的个数
            ll totalfront = sum(e,a[i].x);//a[i].xx之前的数目和
            ll total = sum(e,n);//总得和
            ans+=a[i].h*(x*a[i].x-totalfront+total-totalfront-(i-x-1)*a[i].x);
            add(w,a[i].x,1);
            add(e,a[i].x,a[i].x);//关键在于这里对树状数组的离散化
        }
        cout<<ans<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Endeavor_G/article/details/85270629
今日推荐