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
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.
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.
//203MS 4548K 1948 B C++
/*
题意:
给出n个树的位置和高度,求每两棵数的位置排名差绝对值与高度排名较小的的积的和。 离散化+树状数组:
不错的一道题,我卡在结果的计算,的确对树状数组计算的能力的理解不够!
用两个排序排出位置和高度的排名,然后再对高度排名在进行计算,计算公式:
ans+=p[i].hid*(cnt*p[i].xid-sum+tsum-sum-(i-cnt-1)*p[i].xid); //每个高度
算法时间复杂度为O(n*lgn) */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 100005
struct node{
int x,h;
int xid,hid;
int id;
}p[N],p1[N],p2[N];
__int64 cnum[N],clen[N];
int cmpx(const void *a,const void*b)
{
return (*(node*)a).x-(*(node*)b).x;
}
int cmph(const void*a,const void*b)
{
return (*(node*)a).h-(*(node*)b).h;
}
int cmp0(const void*a,const void*b)
{
return (*(node*)b).hid-(*(node*)a).hid;
}
int lowbit(int k)
{
return (-k)&k;
}
void update(__int64 c[],int k,int detal)
{
for(;k<N;k+=lowbit(k))
c[k]+=detal;
}
__int64 getsum(__int64 c[],int k)
{
__int64 s=;
for(;k>;k-=lowbit(k))
s+=c[k];
return s;
}
int main(void)
{
int n,x,h;
while(scanf("%d",&n)!=EOF)
{
memset(cnum,,sizeof(cnum));
memset(clen,,sizeof(clen));
for(int i=;i<=n;i++){
scanf("%d%d",&p[i].x,&p[i].h);
p[i].id=i;
p1[i]=p2[i]=p[i];
}
qsort(p1+,n,sizeof(p1[]),cmpx);
for(int i=,j=;i<=n;i++){
if(i> && p1[i].x!=p1[i-].x) j=i;
p[p1[i].id].xid=j;
}
qsort(p2+,n,sizeof(p2[]),cmph);
for(int i=,j=;i<=n;i++){
if(i> && p2[i].h!=p2[i-].h) j=i;
p[p2[i].id].hid=j;
}
qsort(p+,n,sizeof(p[]),cmp0);
__int64 ans=;
//for(int i=1;i<=n;i++) printf("*%d %d\n",p[i].hid,p[i].xid);
update(cnum,p[].xid,);
update(clen,p[].xid,p[].xid);
for(int i=;i<=n;i++){
__int64 cnt=getsum(cnum,p[i].xid);
__int64 sum=getsum(clen,p[i].xid);
__int64 tsum=getsum(clen,N-);
ans+=p[i].hid*(cnt*p[i].xid-sum+tsum-sum-(i-cnt-)*p[i].xid);
update(cnum,p[i].xid,);
update(clen,p[i].xid,p[i].xid);
}
printf("%I64d\n",ans);
}
return ;
}
hdu 3015 Disharmony Trees (离散化+树状数组)的更多相关文章
- 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) 求出 ...
- HDU 3015 Disharmony Trees 【 树状数组 】
题意:给出n棵树,给出横坐标x,还有它们的高度h,先按照横坐标排序,则它们的横坐标记为xx, 再按照它们的高度排序,记为hh 两颗树的差异度为 abs(xx[i] - xx[j]) * min(hh[ ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- HDU 6318.Swaps and Inversions-求逆序对-线段树 or 归并排序 or 离散化+树状数组 (2018 Multi-University Training Contest 2 1010)
6318.Swaps and Inversions 这个题就是找逆序对,然后逆序对数*min(x,y)就可以了. 官方题解:注意到逆序对=交换相邻需要交换的次数,那么输出 逆序对个数 即可. 求逆序对 ...
- CodeForces 540E - Infinite Inversions(离散化+树状数组)
花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...
- HDOJ/HDU 1556 Color the ball(树状数组)
Problem Description N个气球排成一排,从左到右依次编号为1,2,3-.N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从 ...
- Ultra-QuickSort(归并排序+离散化树状数组)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 50517 Accepted: 18534 ...
- hdu 5517 Triple(二维树状数组)
Triple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组
BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组 Description 酷爱日料的小Z经常光顾学校东门外的回转寿司店.在这里,一盘盘寿司通过传送带依次呈现在小Z眼前.不同的寿 ...
随机推荐
- STM32堆栈指针疑问
1. 下面的代码看的不是很明白,百为stm32开发板光盘\测试程序\CortexM3\Mode_Privilege\project,堆是程序员分配和使用的,栈是编译器指定的,存放函数参数,临时变量. ...
- 阅读笔记《JavaScript语言精粹》
阅读笔记<JavaScript语言精粹> 对象 1.检索属性 使用[]和. 2.引用传递 JavaScript的简单数据类型包括数字.字符串.布尔值.null值和undefined值.其它 ...
- Webservice HTTP
由于项目需要:自己写了一个WebserviceDemo,把遇到的问题记下来. 方式一 :使用代理类来访问Webservice,此方式不讲解,感觉复杂(神坑). (生成的代理路径 C:\Users\ad ...
- 第二篇 CSS快速入门
学CSS 和 JS的路线: 1. 首先,学会怎么找到标签.只有找到标签,才能操作标签——CSS通过选择器去找标签 2. 其次,学会怎么操作标签对象. CSS概述 CSS是Cascading Style ...
- jmeter关联三种常用方法
在LR中有自动关联跟手动关联,但在我看来手动关联更准确,在jmeter中,就只有手动关联 为什么要进行关联:对系统进行操作时,本次操作或下一次操作对服务器提交的请求,这参数里边有部分参数需要服务器返回 ...
- SQL学习(时间,存储过程,触发器)
SQL学习 几个操作时间的函数 --datapart 获取时间中的年月日时分秒等部分 select DATEPART(year,current_timestamp); select DATEPART( ...
- linux 学习总结---- mysql 总结
用户的创建 ---->修改 ---->删除用户 create alter drop (数据定义语言 DDL) 授权: insert update delete grant *.* revo ...
- [Clr via C#读书笔记]Cp18 定制Attribute
Cp18 定制Attribute 意义 利用Attribute,可以声明性的给自己的代码结构创建注解,从而实现一些特殊的功能:最终在元数据中生成,这种可扩展的元数据信息可以在运行时的时候查询,从而动态 ...
- TensorFlow | ReluGrad input is not finite. Tensor had NaN values
问题的出现 Question 这个问题是我基于TensorFlow使用CNN训练MNIST数据集的时候遇到的.关键的相关代码是以下这部分: cross_entropy = -tf.reduce_sum ...
- 官方文档 恢复备份指南二 Getting Started with RMAN
本章对RMAN进行基本的熟悉和了解 1.Overview of the RMAN Environment RMAN运行时需要的最小环境: target database ...