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. 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
 
Recommend
gaojie   |   We have carefully selected several similar problems for you:  3016 3011 3013 3018 3030 
 
  1. //203MS 4548K 1948 B C++
  2. /*
  3. 题意:
  4. 给出n个树的位置和高度,求每两棵数的位置排名差绝对值与高度排名较小的的积的和。
  5.  
  6. 离散化+树状数组:
  7. 不错的一道题,我卡在结果的计算,的确对树状数组计算的能力的理解不够!
  8. 用两个排序排出位置和高度的排名,然后再对高度排名在进行计算,计算公式:
  9. ans+=p[i].hid*(cnt*p[i].xid-sum+tsum-sum-(i-cnt-1)*p[i].xid); //每个高度
  10. 算法时间复杂度为O(n*lgn)
  11.  
  12. */
  13. #include<stdio.h>
  14. #include<string.h>
  15. #include<stdlib.h>
  16. #define N 100005
  17. struct node{
  18. int x,h;
  19. int xid,hid;
  20. int id;
  21. }p[N],p1[N],p2[N];
  22. __int64 cnum[N],clen[N];
  23. int cmpx(const void *a,const void*b)
  24. {
  25. return (*(node*)a).x-(*(node*)b).x;
  26. }
  27. int cmph(const void*a,const void*b)
  28. {
  29. return (*(node*)a).h-(*(node*)b).h;
  30. }
  31. int cmp0(const void*a,const void*b)
  32. {
  33. return (*(node*)b).hid-(*(node*)a).hid;
  34. }
  35. int lowbit(int k)
  36. {
  37. return (-k)&k;
  38. }
  39. void update(__int64 c[],int k,int detal)
  40. {
  41. for(;k<N;k+=lowbit(k))
  42. c[k]+=detal;
  43. }
  44. __int64 getsum(__int64 c[],int k)
  45. {
  46. __int64 s=;
  47. for(;k>;k-=lowbit(k))
  48. s+=c[k];
  49. return s;
  50. }
  51. int main(void)
  52. {
  53. int n,x,h;
  54. while(scanf("%d",&n)!=EOF)
  55. {
  56. memset(cnum,,sizeof(cnum));
  57. memset(clen,,sizeof(clen));
  58. for(int i=;i<=n;i++){
  59. scanf("%d%d",&p[i].x,&p[i].h);
  60. p[i].id=i;
  61. p1[i]=p2[i]=p[i];
  62. }
  63. qsort(p1+,n,sizeof(p1[]),cmpx);
  64. for(int i=,j=;i<=n;i++){
  65. if(i> && p1[i].x!=p1[i-].x) j=i;
  66. p[p1[i].id].xid=j;
  67. }
  68. qsort(p2+,n,sizeof(p2[]),cmph);
  69. for(int i=,j=;i<=n;i++){
  70. if(i> && p2[i].h!=p2[i-].h) j=i;
  71. p[p2[i].id].hid=j;
  72. }
  73. qsort(p+,n,sizeof(p[]),cmp0);
  74. __int64 ans=;
  75. //for(int i=1;i<=n;i++) printf("*%d %d\n",p[i].hid,p[i].xid);
  76. update(cnum,p[].xid,);
  77. update(clen,p[].xid,p[].xid);
  78. for(int i=;i<=n;i++){
  79. __int64 cnt=getsum(cnum,p[i].xid);
  80. __int64 sum=getsum(clen,p[i].xid);
  81. __int64 tsum=getsum(clen,N-);
  82. ans+=p[i].hid*(cnt*p[i].xid-sum+tsum-sum-(i-cnt-)*p[i].xid);
  83. update(cnum,p[i].xid,);
  84. update(clen,p[i].xid,p[i].xid);
  85. }
  86. printf("%I64d\n",ans);
  87. }
  88. return ;
  89. }

hdu 3015 Disharmony Trees (离散化+树状数组)的更多相关文章

  1. 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) 求出 ...

  2. HDU 3015 Disharmony Trees 【 树状数组 】

    题意:给出n棵树,给出横坐标x,还有它们的高度h,先按照横坐标排序,则它们的横坐标记为xx, 再按照它们的高度排序,记为hh 两颗树的差异度为 abs(xx[i] - xx[j]) * min(hh[ ...

  3. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  4. HDU 6318.Swaps and Inversions-求逆序对-线段树 or 归并排序 or 离散化+树状数组 (2018 Multi-University Training Contest 2 1010)

    6318.Swaps and Inversions 这个题就是找逆序对,然后逆序对数*min(x,y)就可以了. 官方题解:注意到逆序对=交换相邻需要交换的次数,那么输出 逆序对个数 即可. 求逆序对 ...

  5. CodeForces 540E - Infinite Inversions(离散化+树状数组)

    花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...

  6. HDOJ/HDU 1556 Color the ball(树状数组)

    Problem Description N个气球排成一排,从左到右依次编号为1,2,3-.N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从 ...

  7. Ultra-QuickSort(归并排序+离散化树状数组)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 50517   Accepted: 18534 ...

  8. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  9. BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组

    BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组 Description 酷爱日料的小Z经常光顾学校东门外的回转寿司店.在这里,一盘盘寿司通过传送带依次呈现在小Z眼前.不同的寿 ...

随机推荐

  1. 长沙Uber优步司机奖励政策(12月28日到1月3日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  2. CSS布局遇到的问题小结

    clear属性的作用 指定某个元素的一侧不能出现浮动元素.它是通过为这个元素在上边距之外增加空间,从而使得这个元素的顶部和浮动元素的底部对齐.这里作用的仅仅是同一个bfc下的浮动元素. This pr ...

  3. 三 Hive 数据处理 自定义函数UDF和Transform

    三  Hive 自定义函数UDF和Transform 开篇提示: 快速链接beeline的方式: ./beeline -u jdbc:hive2://hadoop1:10000 -n hadoop 1 ...

  4. 前端开发工程师 - 05.产品前端架构 - 协作流程 & 接口设计 & 版本管理 & 技术选型 &开发实践

    05.产品前端架构 第1章--协作流程 WEB系统 角色定义 协作流程 职责说明 第2章--接口设计 概述 接口规范 规范应用 本地开发 第3章--版本管理 见 Java开发工程师(Web方向) - ...

  5. zookeeper应用:屏障、队列、分布式锁

    zookeeper工具类: 获取连接实例:创建节点:获取子节点:设置节点数据:获取节点数据:访问控制等. package org.windwant.zookeeper; import org.apac ...

  6. react在安卓下输入框被手机键盘遮挡问题

    问题概述   今天遇到了一个问题,在安卓手机上,当我要点击输入"店铺名称"时,手机软键盘弹出来刚好把输入框挡住了:挡住就算了,关键是页面还不能向上滑动,整个手机窗口被压为原来的二分 ...

  7. loadrunner_遇到cookie接口_3种应对方法

    方法一:是调用登录接口,在调用登录后的接口 方法二:手动储存cookie,写死cookie 方法一:提前登录收集cookie,写成参数化文件 方法一,案例(就是先登录,再写登录后的接口): 注:use ...

  8. fp-growth树创建代码及详细注释

    事务集过滤重排: #FP树节点结构 class treeNode: def __init__(self,nameValue,numOccur,parentNode): self.name=nameVa ...

  9. C Program基础-宏定义

    写好c语言,漂亮的宏定义是非常重要的,我们在阅读别人工程时,往往能看到大量的宏定义,宏定义可以增加代码的可读性,也能增加代码的可移植性,一个好的宏定义甚至是一件艺术品.今天我们就来看看宏定义的方方面面 ...

  10. Centos6配置开启FTP Server

    vsftpd作为FTP服务器,在Linux系统中是非常常用的.下面我们介绍如何在centos系统上安装vsftp. 什么是vsftpd vsftpd是一款在Linux发行版中最受推崇的FTP服务器程序 ...