hdu3015 Disharmony Trees

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

这道题和之前那题差不多,开两个一维树状数组b1,b2,分别维护x的位置和小于等于x位置的个数。先对x,h进行离散化,用r1,r2储存他们的名次,然后按h大小从大到小的顺序依次枚举就行了。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
struct node{
	int x,h,r1,r2;
}a[100006];
int b1[100006],b2[100006],n;

bool cmp1(node a,node b){
	return a.x<b.x;
}
bool cmp2(node a,node b){
	return a.h<b.h;
}

int lowbit(int x){
	return x&(-x);
}
void update1(int pos,int num)
{
	while(pos<=n){
		b1[pos]+=num;pos+=lowbit(pos);
	}
}
int getsum1(int pos)
{
	int num=0;
	while(pos>0){
		num+=b1[pos];pos-=lowbit(pos);
	}
	return num;
}

void update2(int pos,int num)
{
	while(pos<=n){
		b2[pos]+=num;pos+=lowbit(pos);
	}
}
int getsum2(int pos)
{
	int num=0;
	while(pos>0){
		num+=b2[pos];pos-=lowbit(pos);
	}
	return num;
}

int main()
{
	int m,i,j,p,t1,t2;
	__int64 sum,sum1;
	while(scanf("%d",&n)!=EOF)
	{
		for(i=1;i<=n;i++){
			scanf("%d%d",&a[i].x,&a[i].h);
			b1[i]=b2[i]=0;
		}
		sort(a+1,a+1+n,cmp1);
		a[1].r1=1;p=1;
		for(i=2;i<=n;i++){
			if(a[i].x==a[p].x){
				a[i].r1=a[p].r1;
			}
			else{
				a[i].r1=i;p=i;
			}
		}
		sort(a+1,a+1+n,cmp2);
		a[1].r2=1;p=1;
		for(i=2;i<=n;i++){
			if(a[i].h==a[p].h){
				a[i].r2=a[p].r2;
			}
			else{
				a[i].r2=i;p=i;
			}
		}

		sum=0;sum1=0;
		for(i=n;i>=1;i--){
			t1=getsum1(a[i].r1);
			t2=getsum2(a[i].r1);
			sum+=a[i].r2*( t2*a[i].r1-t1 + sum1-t1-(n-i-t2)*a[i].r1 );
			sum1+=a[i].r1;
			update1(a[i].r1,a[i].r1);
			update2(a[i].r1,1);
			//printf("%I64d\n",sum);
		}
		printf("%I64d\n",sum);
	}
}

时间: 2024-11-05 01:01:46

hdu3015 Disharmony Trees的相关文章

hdu3015 Disharmony Trees(树状数组+排序)

题目链接:点击打开链接 解题思路: 1.首先对x和高度h分别从小到大排序记录排名 2.然后对高度h按从大到小排序(保证当前要计算的树的高度是所有已经遍历的树中最小高度,便于计算S=min(h1,h2)) 3.循环遍历数组,每次遍历向树状数组C中t[i].rx位置增加t[i].rx,向树状数组C1中t[i].rx位置增加1 解析:C记录排名和,C1记录个数 所以以t[i].rh为最小值的点对的和为 t[i].rh*(sum(n)-sum(t[i].rx)-(LL)t[i].rx*(sum1(n)-

hdu 3015 Disharmony Trees (离散化+树状数组)

Disharmony Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 663    Accepted Submission(s): 307 Problem Description One day Sophia finds a very big square. There are n trees in the square. T

Disharmony Trees

/* 写完这篇博客有很多感慨,过去一段时间都是看完题解刷题,刷题,看会题解,没有了大一那个时候什么都不会的时候刷题的感觉,这个题做了一天半,从开始到结束都是从头开始自己构思的很有感觉,找回到当初的感觉 */ Disharmony Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 90 Accepted Submission(s):

Disharmony Trees 树状数组

Disharmony Trees Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status 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.

HDU 3015 Disharmony Trees(树状数组)

题意:给你n棵树,每棵树上有两个权值X H 对于X离散化 :3 7 1 5 3 6 -> 2 6 1 4 2 5,对于H一样 然后F = abs(X1-X2)   S=min(H1,H2) 求出每一对F*S的总和 可以看到一边是求每个数与其他数的最小值,一边是求每个数与其他数的差距.因此我们可以排序一边,处理另一边. 我们排序H,因为这样对于固定一个Xi Hi,从小到大每次都是Hi去乘以Xi与剩下的所有X的差的总和. 这样我们就可以使用树状数组维护两个值:每个位置值的个数,每个位置值的总大小,接

HDU 3015 Disharmony Trees 【 树状数组 】

题意:给出n棵树,给出横坐标x,还有它们的高度h,先按照横坐标排序,则它们的横坐标记为xx, 再按照它们的高度排序,记为hh 两颗树的差异度为 abs(xx[i] - xx[j]) * min(hh[i],hh[j]),求所有的差异度的和 和上面一道题一样,只不过这题是要Min的值,就将h从大到小排序,保证每一个h都是当前最小的 然后维护比当前x小的坐标的个数,当前区间的总和,前面比x小的坐标的和 1 #include<iostream> 2 #include<cstdio> 3

hdu-3015 Disharmony Trees---离散化+两个树状数组

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3015 题目大意: 有一些树,这些树的高度和位置给出.现在高度和位置都按从小到大排序,对应一个新的rank,任意两棵树的值为min(高度的rank) * abs(位置差的绝对值).问所有任意两棵树的值的和是多少. 解题思路: 按照题意离散化,然后对H从大到小排序,这样可以保证前面的树高度都比当前的高(或者相等).在计算的时候就可以使用当前的H. 和POJ-1990类似 1 #include<iost

树状数组详解(图形学算法)

目录 一.从图形学算法说起 1.Median Filter 概述 2.r pixel-Median Filter 算法 3.一维模型 4.数据结构的设计 5.树状数组华丽登场 二.细说树状数组 1.树 or 数组? 2.结点的含义 3.求和操作 4.更新操作 5.lowbit函数O(1)实现 6.小结 三.树状数组的经典模型 1.PUIQ模型 2.IUPQ模型 3.逆序模型 4.二分模型 5.再说Median Filter 6.多维树状数组模型 四.树状数组题集整理 一.从图形学算法说起 1.M

HDU 专题分类

[背包问题] 2602 Bone Collector 1114 Piggy-Bank 1203 I NEED A OFFER! 1171 Big Event in HDU 1059 Dividing 2844 Coins 2191 悼念512汶川大地震遇难同胞--珍惜现在,感恩生活 2159 FATE 1561 The more, The Better 1011 Starship Troopers 2639 Bone Collector II 3033 I love sneakers! 2955