HDU3015 Disharmony Trees

Disharmony Trees


Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 702 Accepted Submission(s): 329


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


Source
2009 Multi-University Training Contest 12 - Host by FZU

题意:给n棵树,每个数都有位置和高度,对位置和高度排序,相同位置和高度的序号相同(见题目),F为每两棵树的位置之差,S为每两棵数的高度中的小的那个,求所有树的F*S之和。

分析:我认为这题的关键就是想到对S从小到大进行排序,然后求解。我们可以用两个树状数组,一个记录位置的大小,一个记录这个位置有没有树,首先,把所有的点加入树状数组,然后因为S是从小到大排序的,我们从小的选起,每个最小的S对应的位置的大小记为a,找出a之前位置还存在的个数,然后 个数*a-a之前位置大小的总和,同理再找出比a位置大的个数,a之后位置大小的总和-个数*a,然后两个相加再乘上这个位置的S就可以,算完一个点就把这个点从树状数组中删除。。

#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL __int64
using namespace std;
const int MAXN=100010;
struct node
{
    int a,b;
    int num1,num2;
}p[MAXN];
LL c[MAXN],d[MAXN];
bool cmpa(node x,node y)
{
    return x.a<y.a;
}
bool cmpb(node x,node y)
{
    return x.b<y.b;
}
bool cmpn(node x,node y)
{
    if(x.num2==y.num2)
        return x.num1<y.num1;
    return x.num2<y.num2;
}
int lowbit(int x)
{
    return x&(-x);
}
void updatec(int x,int val)
{
    while(x<MAXN)
    {
        c[x]+=val;
        x+=lowbit(x);
    }
}
void updated(int x,int val)
{
    while(x<MAXN)
    {
        d[x]+=val;
        x+=lowbit(x);
    }
}
LL getsumc(int x)
{
    LL ans=0;
    while(x>0)
    {
        ans+=c[x];
        x-=lowbit(x);
    }
    return ans;
}
LL getsumd(int x)
{
    LL ans=0;
    while(x>0)
    {
        ans+=d[x];
        x-=lowbit(x);
    }
    return ans;
}
int main()
{
    int n,i,j;
    while(scanf("%d",&n)==1)
    {
        for(i=1;i<=n;i++)
            scanf("%d%d",&p[i].a,&p[i].b);
        sort(p+1,p+1+n,cmpa);
        for(i=1;i<=n;i++)
        {
            if(i!=1&&p[i].a==p[i-1].a)
                p[i].num1=p[i-1].num1;
            else
                p[i].num1=i;
        }
        sort(p+1,p+1+n,cmpb);
        for(i=1;i<=n;i++)
        {
            if(i!=1&&p[i].b==p[i-1].b)
                p[i].num2=p[i-1].num2;
            else
                p[i].num2=i;
        }
        sort(p+1,p+1+n,cmpn);
        memset(d,0,sizeof(d));
        memset(c,0,sizeof(c));
        for(i=1;i<=n;i++)
        {
            updatec(p[i].num1,p[i].num1);
            updated(p[i].num1,1);
        }
        LL ans=0;
        for(i=1;i<=n;i++)
        {
            LL tmp1=p[i].num1*getsumd(p[i].num1-1)-getsumc(p[i].num1-1);
            LL tmp2=getsumc(MAXN)-getsumc(p[i].num1)-p[i].num1*(getsumd(MAXN)-getsumd(p[i].num1));
            ans=ans+(tmp1+tmp2)*p[i].num2;
            updatec(p[i].num1,-p[i].num1);
            updated(p[i].num1,-1);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值