Disharmony Trees

原创 2016年08月29日 15:39:48
链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3015

题目: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.

题意:给一排树的x坐标和高度,求总不和谐值,不和谐值指任意两棵树的横坐标(要求离散化)的差乘以两者的最小高度(要求离散化)。

分析:当时能想到用树状数组。不过在最后一步不会处理。后来了解到用两个树状数组,分别记录高度和x坐标,并且是从靠后的开始对之前存进去的树进行计算,计算方法还是比较诡异的。我暂时还没有找到很好的理解方式。

题解:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <functional>
#include <cmath>
#include <cctype>
#include <cfloat>
#include <climits>
#include <complex>
#include <deque>
#include <list>
#include <set>
#include <utility>
using namespace std;

int n;

#define BIT_SIZE 100011
class BIT
{
public:
	BIT(){for(int i=1;i<BIT_SIZE;i++)c[i]=0;}
	void update(int i,__int64 val){while(i<BIT_SIZE){c[i]+=val;i+=i&(-i);}}
	__int64 sum(int i){__int64 ans=0;while(i>0){ans+=c[i];i-=i&(-i);}return ans;}
	__int64 get(int i){if(i>0)return sum(i)-sum(i-1);return 0;}
	void clear(){for(int i=1;i<BIT_SIZE;i++)c[i]=0;}
private:
	__int64 c[BIT_SIZE];
};

struct tre{
	int x;
	int h;
}s[100010],st[100010];

bool cmp1(tre p,tre q)
{
	return p.x<q.x;
}

bool cmp2(tre p,tre q)
{
	return p.h<q.h;
}

int main()
{
	//freopen("in.txt","r",stdin);
	while(~scanf("%d",&n))
	{
		BIT bita,bitb;
		memset(s,0,sizeof s);
		for(int i=1;i<=n;i++)
			scanf("%d %d",&s[i].x,&s[i].h);
		sort(s+1,s+n+1,cmp1);
		memcpy(st,s,sizeof s);
		for(int i=1;i<=n;i++){
			if(s[i].x!=s[i-1].x)
				st[i].x=i;
			else
				st[i].x=st[i-1].x;
		}
		sort(st+1,st+n+1,cmp2);
		memcpy(s,st,sizeof s);
		for(int i=1;i<=n;i++){
			if(st[i].h!=st[i-1].h)
				s[i].h=i;
			else
				s[i].h=s[i-1].h;
		}
		__int64 ans=0,temp=0;
		for(int i=n;i>0;i--){
			ans+=bitb.sum(s[i].x-1)*s[i].x*s[i].h;
			ans-=s[i].h*bita.sum(s[i].x-1);
			ans+=s[i].h*(bita.sum(BIT_SIZE-1)-bita.sum(s[i].x));
			ans-=(bitb.sum(BIT_SIZE-1)-bitb.sum(s[i].x))*s[i].x*s[i].h;
			bita.update(s[i].x,s[i].x);
			bitb.update(s[i].x,1);
		}
		printf("%I64d\n",ans);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 3015 Disharmony Trees(树状数组)

Disharmony TreesProblem DescriptionOne day Sophia finds a very big square. There are n trees in the ...

HDU 5876 Disharmony Trees(离散化 树状数组)

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

HDU 3015 Disharmony Trees 树状数组

来源:http://acm.hdu.edu.cn/showproblem.php?pid=3015 题意:有一些树,这些树的高度和位置给出。现在高度和位置都按从小到大排序,对应一个新的rank,任意...
  • wmn_wmn
  • wmn_wmn
  • 2012年09月29日 21:38
  • 1118

HDU 3015 Disharmony Trees

转载请注明出处:忆梦http://blog.csdn.net/yimeng2013/article/details/13033965 题目链接:http://acm.hdu.edu.cn/sho...

hdu3015 Disharmony Trees(树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3015 题意:在路边有一行树,给出它们的坐标和高度,先按X坐标排序。记录排名,记为rankx,再按它们...

poj1990 MooFest && hdu3015 Disharmony Trees (树状数组)

题目:http://poj.org/problem?id=1990 && http://acm.hdu.edu.cn/showproblem.php?pid=3015 题意:这两题题目意思差不多,只说...
  • w20810
  • w20810
  • 2015年07月11日 09:03
  • 273

hdu3015 Disharmony Trees

Problem Description One day Sophia finds a very big square. There are n trees in the square. They...

HDU 3015 Disharmony Trees (线段树)

题目大意:给定一组x[], 一组h[], 求所有的sum(min(h1,h2)*abs(x1,x2))。     解题报告:首先,h1,h2间的较小值,我们可以将h[]从大到小排序,每次取得的h都比先...
  • kbdwo
  • kbdwo
  • 2013年09月26日 13:44
  • 468

hdu 3015 Disharmony Trees

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3015   题目大意: 在一片树林有n棵树(你   思路:最暴力的解法(n*n),n最大是...
  • gotoac
  • gotoac
  • 2012年05月08日 22:52
  • 409

【TOJ 3401.】Disharmony Trees【树状数组】

题意:给出n个树的两个变量,一个是x
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Disharmony Trees
举报原因:
原因补充:

(最多只允许输入30个字)