您的位置:首页 > 大数据 > 人工智能

HDU 3015——Disharmony Trees(树状数组 2009 Multi-University Training Contest 12 - Host by FZU)

2014-08-30 23:45 716 查看


Disharmony Trees

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 731 Accepted Submission(s): 345



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.



Sample Input

2
10 100
20 200
4
10 100
50 500
20 200
20 100




Sample Output

1
13




————————————————————分割线————————————————

题目大意:

给定n棵树,每棵树的横坐标x,高度h 。 每两棵树之间的比较值为abs(x1-x2)*min(y1,y2),n棵树总共有(n*(n-1)/2)次的比较,求总和

思路:

这题其实思路跟POJ1990是一样的,但是自己还是ac的好艰难,太弱了。

n的范围是10万,而x,h的范围是10的9次方,所以要分别对x,h进行离散化。离散化后放到一个结构体里。

之后的操作是跟poj1990是一模一样的,但是我自己却在求比x大的个数的时候 因为i加了1,求错了,自己还不知道,调了好几个小时~

今天就这样过了,明天也将是。最近好消沉。无力ing

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define ll __int64
const int M=100001;
using namespace std;
int n;
ll c[2][M+5];
int p[M+5],q[M+5];
struct node
{
    int x,y;
    bool operator<(const node&A)const
    {
        return y>A.y;
    }
}f[M+5];
struct node1
{
    int x,index;
    bool operator<(const node1&A)const
    {
        return x<A.x;
    }
}a[M+5],b[M+5];
void discretize(node1 *A,int *F)
{
    int key=1;
    F[A[0].index]=1;
    for(int i=1;i<n;++i){
        if(A[i].x>A[i-1].x) F[A[i].index]=key=i+1;
        else F[A[i].index]=key;
    }
}
void update(int k,int x,int v)
{
    for(int i=x;i<=M;i+=i&-i){
        c[k][i]+=v;
    }
}
ll getsum(int k,int x)
{
    ll sum=0;
    for(int i=x;i>0;i-=i&-i){
        sum+=c[k][i];
    }
    return sum;
}
int main()
{
    while(scanf("%d",&n)!=EOF){
        memset(c,0,sizeof(c));
        for(int i=0;i<n;++i){
            scanf("%d %d",&a[i].x,&b[i].x);
            a[i].index=b[i].index=i;
        }
        sort(a,a+n),sort(b,b+n);
        discretize(a,p),discretize(b,q);
        for(int i=0;i<n;++i){
            f[i].x=p[i];
            f[i].y=q[i];
        }
        sort(f,f+n);
        ll cnt=0;
        for(int i=0;i<n;++i){
            ll sum1=getsum(0,f[i].x)*f[i].x-getsum(1,f[i].x);
            ll sum2=getsum(1,n)-getsum(1,f[i].x)-(i-getsum(0,f[i].x))*f[i].x;
            //cout<<sum1<<" "<<sum2<<endl;
            cnt+=(sum1+sum2)*f[i].y;
            update(0,f[i].x,1);
            update(1,f[i].x,f[i].x);
        }
        printf("%I64d\n",cnt);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐