关闭

Disharmony Trees

标签: 树状数组离散化
25人阅读 评论(0) 收藏 举报
分类:
链接: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;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3205次
    • 积分:597
    • 等级:
    • 排名:千里之外
    • 原创:59篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    友情链接