hdu3015 Disharmony Trees(线段树)

                                                Disharmony Trees

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


 

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

 

 

Recommend

gaojie

 

 

一、原题地址
点我传送

 

二、大致题意

有n棵树,每棵树有x和h权值。但是读入后要按照他们对应的x和h重新排一个等级再赋值回去给x和y。

 

两颗树的贡献为 abs(x[i] - x[j]) * min(h[i],h[j])  .现在询问的所有的贡献的和。

 

三、大致思路

按照h从大到小排序后,对于一棵当前更新到的树,他的h值肯定小于前面所有更新过的树,所以此时只需要统计这棵树到其他点的x差值就行了。这两种值可以在一个线段树上同时更新。查询一下左右边贡献就行了。

 

四、代码

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const LL INF=1e17+10;



int n;
struct Node
{
    int h,x;
}np[100005];
LL hh[100005],hx[100005];

const int maxn = 100005 * 4;	

struct Tree
{
	int l, r;
	LL num;
	LL sum;
};

Tree node[maxn];		

void PushUp(int i)
{
    node[i].sum = node[i << 1].sum + node[(i << 1) | 1].sum;
    node[i].num = node[i << 1].num + node[(i << 1) | 1].num;
}

void build(int i, int l, int r)
{
	node[i].l = l; node[i].r = r;
	if (l == r)
	{
	    node[i].num = 0;
		node[i].sum = 0;
		return;
	}
	int mid = (l + r) / 2;
	build(i << 1, l, mid);
	build((i << 1) | 1, mid + 1, r);
	PushUp (i);
}

LL getsum(int i, int l, int r)
{
	if (node[i].l == l&&node[i].r == r)
    {
        return node[i].sum;
    }

	int mid = (node[i].l + node[i].r) / 2;
	if (r <= mid) return getsum(i << 1, l, r);
	else if (l > mid) return getsum((i << 1) | 1, l, r);
	else return getsum(i << 1, l, mid) + getsum((i << 1) | 1, mid + 1, r);
}

LL getNum(int i, int l, int r)
{
	if (node[i].l == l&&node[i].r == r)
		return node[i].num;
	int mid = (node[i].l + node[i].r) / 2;
	if (r <= mid) return getNum(i << 1, l, r);
	else if (l > mid) return getNum((i << 1) | 1, l, r);
	else return getNum(i << 1, l, mid) + getNum((i << 1) | 1, mid + 1, r);
}

void add(int i, int k, LL v)	
{
	if (node[i].l == k&&node[i].r == k)
	{
		node[i].num += v;
		node[i].sum=(LL)node[i].l*node[i].num;
		return;
	}
	int mid = (node[i].l + node[i].r) / 2;
	if (k <= mid) add(i << 1, k, v);
	else add((i << 1) | 1, k, v);
	PushUp (i);
}

void read()
{
    for(int i=1;i<=n;i++)
    {
        scanf("%d %d",&np[i].x,&np[i].h);
        hh[i]=np[i].h;
        hx[i]=np[i].x;
    }
    sort(hh+1,hh+1+n);
    sort(hx+1,hx+1+n);
    for(int i=1;i<=n;i++)
    {
        np[i].h=lower_bound(hh+1,hh+1+n,np[i].h)-hh;
        np[i].x=lower_bound(hx+1,hx+1+n,np[i].x)-hx;
    }
}

bool cmp(Node xx,Node yy)
{
    return xx.h>yy.h;
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        read();
        build(1,1,n);
        sort(np+1,np+1+n,cmp);
        LL ans=0;
        for(int i=1;i<=n;i++)
        {
            ans+=abs( (getsum(1,1,np[i].x)-getNum(1,1,np[i].x)*(LL)np[i].x) )*np[i].h;
            //左边的贡献

            ans+=abs( (getsum(1,np[i].x,n)-getNum(1,np[i].x,n)*(LL)np[i].x) )*np[i].h;
            //右边的贡献
    
            add(1,np[i].x,1);
            //更新

        }
        printf("%lld\n",ans);
    }


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值