HDU3015 Disharmony Trees

来源:互联网 发布:近年物流业发展数据 编辑:程序博客网 时间:2024/05/17 19:21

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 __int64using 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;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 京东白条风控怎么办 我有个破袄…没有衣服怎么办办 轩辕传奇手游灵宠融合错了怎么办 各尧学生不能用怎么办? 山东一卡通商务卡丢了怎么办 和信通过期了怎么办 和信通过期怎么办延期 和信通过期余额怎么办 超市储蓄卡丢了怎么办 提现提到注销卡怎么办 美通卡过期2年了怎么办 物美美通卡丢失怎么办 网上购物电话留错了怎么办 微信斗牛一直输怎么办 微信斗牛输了钱怎么办 微信举报诈骗不成功该怎么办 沙河拿服装太贵怎么办 包上的暗扣掉了怎么办 银手镯暗扣松老是掉怎么办 包包纽扣锁坏了怎么办 包的纽扣坏了怎么办 包上的纽扣坏了怎么办 洗衣服不小心用了色渍净怎么办 洗衣服不小心沾了卫生纸怎么办 麻料裤子扎皮肤怎么办 衣服没洗干净有点发光怎么办 桑蚕丝衣服脏了发光洗不掉怎么办 厨师衣服的油味怎么办 看上夜场的小姐了怎么办 楼卖完了水吧员怎么办 窗帘短了20公分怎么办 白色鞋子沾油了怎么办 面试时没有正装怎么办 宝宝喝了沐浴露怎么办 开实体童装店没人买怎么办 宝宝一岁半了还不会说话怎么办 十个月宝宝不吃辅食怎么办 八个月宝宝不吃米糊怎么办 八个月宝宝不吃辅食怎么办 六个月宝宝不吃米粉怎么办 4个月婴儿积食怎么办