Disharmony Trees HDU - 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.

InputThere 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.OutputFor 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

  1. 2
  2. 10 100
  3. 20 200
  4. 4
  5. 10 100
  6. 50 500
  7. 20 200
  8. 20 100

Sample Output

  1. 1
  2. 13

题意:对于n棵树,给出所在位置和高度,然后分别对它的位置和高度做如下处理:

位置:将位置升序排序,最小的定义等级为 1,次小的定义等级为2,但是,要是位置相同的,则等级定义要相同;

例如:位置 1,2,1,5,2,3

等级  1,3,1,6,3,5

对于高度也是做如上处理;

然后,定义f=两树之间的距离差的绝对值,s=两树中最小的高度,求所有树之间f*s和。

题解:树状数组。

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<sstream>
  6. #include<cmath>
  7. #include<stack>
  8. #include<cstdlib>
  9. #include <vector>
  10. #include<queue>
  11. using namespace std;
  12.  
  13. #define ll long long
  14. #define llu unsigned long long
  15. #define INF 0x3f3f3f3f
  16. #define PI acos(-1.0)
  17. const int maxn = 2e5+;
  18. const int mod = 1e9+;
  19.  
  20. struct node
  21. {
  22. ll x,h;
  23. }T[];
  24. int n;
  25.  
  26. bool cmp(node s1,node s2)
  27. {
  28. return s1.x<s2.x;
  29. }
  30. bool cmp1(node s1,node s2)
  31. {
  32. return s1.h<s2.h;
  33. }
  34. int lowbit(int x)
  35. {
  36. return x&(-x);
  37. }
  38. void add(int i,int val,int *s)
  39. {
  40. while(i<=n)
  41. {
  42. s[i] += val;
  43. i += lowbit(i);
  44. }
  45. }
  46. int sum(int i,int *s)
  47. {
  48. int res=;
  49. while(i>)
  50. {
  51. res += s[i];
  52. i -= lowbit(i);
  53. }
  54. return res;
  55. }
  56.  
  57. int main()
  58. {
  59. while(~scanf("%d",&n))
  60. {
  61. int c[],c1[];
  62. for (int i = ; i <= n; i++)
  63. scanf("%lld %lld", &T[i].x, &T[i].h);
  64. sort(T + , T + n + , cmp);
  65. int xx = T[].x;
  66. T[].x = ;
  67. for (int i = ; i <= n; i++) //将T[i].x按照从小到大排序
  68. {
  69. if (T[i].x == xx)
  70. T[i].x = T[i - ].x;
  71. else {
  72. xx = T[i].x;
  73. T[i].x = i;
  74. }
  75. }
  76. int xxmaxn = T[n].x;
  77. sort(T + ,T + n +,cmp1);
  78. xx = T[].h;
  79. T[].h = ;
  80. for (int i = ; i <= n; i++) //将T[i].h按照从小到大排序
  81. {
  82. if (T[i].h == xx)
  83. T[i].h = T[i - ].h;
  84. else {
  85. xx = T[i].h;
  86. T[i].h = i;
  87. }
  88. }
  89. ll ans = ;
  90. sort(T+,T++n,cmp1);
  91. memset(c,,sizeof c);
  92. memset(c1,,sizeof c1);
  93. for(int i=;i<=n;i++)
  94. {
  95. add(T[i].x,T[i].x,c); // 记录所有比这个数小的和,把每个等级的数放到对应的位置上
  96. add(T[i].x,,c1); //记录所有比这个数小的个数 每个点上记为1
  97. }
  98. int xiao,da; //xiao 表示比对应的数a小,反之亦然
  99. for(int i = ;i<n;i++)
  100. {
  101. xiao = sum(T[i].x-,c1) * T[i].x - sum(T[i].x-,c); //找出比这个数小的个数*这个数-比这个数小的所有数之和
  102. da = (sum(xxmaxn,c) - sum(T[i].x,c)) - (sum(xxmaxn,c1) - sum(T[i].x,c1)) * T[i].x; //找出比这个数大的数和-这个数*比这个数大的个数
  103. ans += (xiao + da) * T[i].h;
  104. add(T[i].x,-T[i].x,c); //把这个用过的数删除
  105. add(T[i].x,-,c1); //把这个数位置上减去1
  106. }
  107. printf("%lld\n",ans);
  108. }
  109.  
  110. }

Disharmony Trees HDU - 3015的更多相关文章

  1. Disharmony Trees HDU - 3015 树状数组+离散化

    #include<cstdio> #include<cstring> #include<algorithm> #define ll long long using ...

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

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

  3. Disharmony Trees 树状数组

    Disharmony Trees Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  4. Disharmony Trees

    /* 写完这篇博客有很多感慨,过去一段时间都是看完题解刷题,刷题,看会题解,没有了大一那个时候什么都不会的时候刷题的感觉,这个题做了一天半,从开始到结束都是从头开始自己构思的很有感觉,找回到当初的感觉 ...

  5. C - Visible Trees HDU - 2841 -莫比乌斯函数-容斥

    C - Visible Trees HDU - 2841 思路 :被挡住的那些点(x , y)肯定是 x 与 y不互质.能够由其他坐标的倍数表示,所以就转化成了求那些点 x,y互质 也就是在 1 - ...

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

  7. HDU 3015 Disharmony Trees

    题解:在路边有一行树,给出它们的坐标和高度,先按X坐标排序.记录排名,记为rankx,再按它们的高度排序,记录排名,记为rankh.两颗树i,j的差异度为 fabs(rankx[i]-rankx[j] ...

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

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

  9. Eat the Trees hdu 1693

    Problem DescriptionMost of us know that in the game called DotA(Defense of the Ancient), Pudge is a ...

随机推荐

  1. jquery.validate+jquery.form表单验证提交

    1.通过jquery.validate的submitHandler选项,即当表单通过验证时运行回调函数.在这个回调函数中通过jquery.form来提交表单: <script type=&quo ...

  2. linux安装jdk7步骤

    linux安装jdk7步骤: 1.首先使用命令查看linux系统版本号: lsb_release -a 2.下载对应的jdk版本,笔者使用的是jdk-7u79-linux-x64.tar.gz: 3. ...

  3. node官方docker镜像运行bower 提示 permission denied 解决方法

    在使用node官方docker镜像部署node应用时,应用需要npm的scripts中运行bower install 来安装前端包,但是用docker 构建时失败,提示 permission dein ...

  4. webapp一些样式记录

    图片外面的div设置宽高自适应width: 100vw; max-width: 640px; display: block; height: 43.75vw; max-height: 280px; f ...

  5. Spring @Autowired使用介绍

    参考博客: https://blog.csdn.net/u013412772/article/details/73741710 引用文章地址: https://my.oschina.net/Helio ...

  6. SpringMVC项目的快速搭建

    Spring MVC提供了一个DispatcherServlet来开发Web应用.在Servlet2.5及2以下的时候只要在web.xml下配置<servlet>元素即可. 在Servle ...

  7. shell中的判断语句

    1.字符串判断 str1 = str2 当两个串有相同内容.长度时为真 str1 != str2 当串str1和str2不等时为真 -n str1 当串的长度大于0时为真(串非空,变量) -z str ...

  8. .NET 前台调用后台事件和方法实现小结

    转自:https://www.cnblogs.com/kinger906/p/3431842.html 除了下文讲的方式外,还有一种方式:html里面使用ajax写好提交方式和提交参数,然后以写一行带 ...

  9. IOS tableView 去除分割线 和 不允许选中表格cell

    //去除分割线 self.tableView.backgroundColor=[UIColor colorWithRed:///255.0 alpha:1.0]; self.tableView.sep ...

  10. IOS segue(跳转页面处理)

    ● Storyboard上每一根用来界面跳转的线,都是一个UIStoryboardSegue对象(简称Segue) Segue的属性 ● 每一个Segue对象,都有3个属性 ➢ 唯一标识 @prope ...