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

Disharmony Trees

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 90 Accepted Submission(s): 56
 
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
2009 Multi-University Training Contest 12 - Host by FZU
 
Recommend
gaojie
 
/*
错误思路:
按照高度排好序之后整个的求值过程就变成了h[1]*abs((n*a[1]-(a[2]+a[3]+..+a[n])))+h[2]*abs(((n-1)*a[2]-(a[3]+a[4]+..+a[n])))+.....
然后用树状数组维护a序列的值
解析:
因为绝对值的问题所以(n*a[1]-(a[2]+a[3]+..+a[n]))这个位置不能直接减去,要考虑大小才能把括号去掉 正确思路:
按照高排好序之后,每棵树的min_h肯定是本身的h,abs_a的话需要考虑从i到n的a与本身的大小
树状数组维护两个值比自己小的有多少个,还有维护a数组,
*/
#include<bits/stdc++.h>
#define N 100005
#define ll long long
#define lowbit(x) x&(-x)
using namespace std;
struct node
{
ll x,h;
ll rankx,rankh;
node(){}
node(ll a,ll b)
{
x=a;
h=b;
}
};
ll n;
ll x,h;
ll c[N];//用来构建树状数组维护到当前结点比当前结点小的个数
ll sum[N];//用来维护到当前结点比当前结点小的数的总和
node f[N];
bool cmp1(node a,node b)
{
return a.x<b.x;
}
bool cmp2(node a,node b)
{
return a.h<b.h;
}
bool cmp3(node a,node b)//这次排序从大到小排序,这样能使从0到i的所有计算中用到的h都是h[i];
{
return a.h>b.h;
}
void init()
{
memset(c,,sizeof c);
memset(sum,,sizeof sum);
}
void update1(ll x,ll val)
{
while(x<N)
{
c[x]+=val;
x+=lowbit(x);
}
}
void update2(ll x,ll val)
{
while(x<N)
{
sum[x]+=val;
x+=lowbit(x);
}
}
ll getsum1(ll x)//这个是获取有多少个比自己小的
{
ll s=;
while(x>)
{
s+=c[x];
x-=lowbit(x);
}
return s;
}
ll getsum2(ll x)//这个是获取比自己小的和
{
ll s=;
while(x>)
{
s+=sum[x];
x-=lowbit(x);
}
return s;
}
int main()
{
//freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
while(scanf("%lld",&n)!=EOF)
{
init();
f[]=node(-,-);
for(int i=;i<=n;i++)
{
scanf("%lld%lld",&x,&h);
f[i]=node(x,h);
}//处理输入 sort(f+,f+n+,cmp1);//先按照坐标的大小进行排序
for(int i=;i<=n;i++)
{
if(f[i].x==f[i-].x)
f[i].rankx=f[i-].rankx;
else
f[i].rankx=i;
} sort(f+,f+n+,cmp2);//然后按照树的高度的大小进行排序
for(int i=;i<=n;i++)
{
if(f[i].h==f[i-].h)
f[i].rankh=f[i-].rankh;
else
f[i].rankh=i;
}
sort(f+,f+n+,cmp3);//第三次排序保证每次运算用到的h都是h[i](也就是使得h[i]在0到i中最小;
ll cur=;
ll total=;
ll same=;//和自己相同的个数
ll low=;//比自己小的个数
ll big=;//比自己大的个数
ll low_sum=;//比自己小的和
ll big_sum=;//比自己大的和
//for(int i=1;i<=n;i++)
// cout<<f[i].rankx<<" ";
//cout<<endl;
for(int i=;i<=n;i++)
{
total+=f[i].rankx;//表示到当前位置的总坐标值 /*更新当前的信息*/
update1(f[i].rankx,);
update2(f[i].rankx,f[i].rankx);
//和当前位置相同的个数
same=getsum1(f[i].rankh)-getsum1(f[i].rankh-);
//比自己小的个数;
low=getsum1(f[i].rankx-);
//比自己大的个数
big=i-same-low;
//比自己小的和
low_sum=getsum2(f[i].rankx-);
//比自己大的和
big_sum=total-low_sum-same*f[i].rankx;
//cout<<"total ="<<total<<endl;
//cout<<"getsum1(x) ="<<getsum1(x)<<endl;
//cout<<"i-getsum1(x)-1 ="<<i-getsum1(x)-1<<endl;
//cout<<"getsum2(x) ="<<getsum2(x)<<endl;
//cout<<"total-getsum2(x)="<<total-getsum2(x)<<endl;
//cout<<endl; //比自己大的和减去比当前值大的和
cur+=f[i].rankh*((low*f[i].rankx-low_sum)+(big_sum-big*f[i].rankx));
//a[i]乘上比自己小的个数减去比自己小的和
}
printf("%lld\n",cur);
}
return ;
}

Disharmony Trees的更多相关文章

  1. Disharmony Trees 树状数组

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

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

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

  3. Disharmony Trees HDU - 3015

    Disharmony Trees HDU - 3015 One day Sophia finds a very big square. There are n trees in the square. ...

  4. hdu3015 Disharmony Trees

    Problem Description One day Sophia finds a very big square. There are n trees in the square. They ar ...

  5. HDU-3015 Disharmony Trees [数状数组]

    Problem Description One day Sophia finds a very big square. There are n trees in the square. They ar ...

  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. Disharmony Trees HDU - 3015 树状数组+离散化

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

随机推荐

  1. 《HiBlogs》重写笔记[1]--从DbContext到依赖注入再到自动注入

    本篇文章主要分析DbContext的线程内唯一,然后ASP.NET Core的注入,再到实现自动注入. DbContext为什么要线程内唯一(非线程安全) 我们在使用EF的时候,可能使用相关框架封装过 ...

  2. ASP.NET Core 认证与授权[1]:初识认证

    在ASP.NET 4.X 中,我们最常用的是Forms认证,它既可以用于局域网环境,也可用于互联网环境,有着非常广泛的使用.但是它很难进行扩展,更无法与第三方认证集成,因此,在 ASP.NET Cor ...

  3. mongoDB学习手记1--Windows系统下的安装与启动

    第一步:下载安装包 我们首先需要下载 mongodb 的安装包,直接到官网下载即可.地址为:https://www.mongodb.com/download-center#community. 看下自 ...

  4. Nginx学习——Nginx简单介绍和Linux环境下的安装

    一:Nginx的简介 百科百科:Nginx Nginx 是一个俄罗斯的哥们开发的,并将其进行了开源. Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器, ...

  5. 关于VisualStudio一运行带中文程序就出错或输出乱码问题的解决

    昨晚纠结了老半天,各种查资料最后终于解决了此问题.今天上午便来编写这篇随笔了!(由于问题已解决,未附上出状况的截图)以下是解决办法: 此问题的原因应是文件的编码问题,选定好出错的文件后,在菜单栏中选择 ...

  6. Ubuntu安装opencv3.x系列

    p { margin-bottom: 0.25cm; direction: ltr; color: rgb(0, 0, 0); line-height: 120% } p.western { font ...

  7. input输入中文时,拼音在输入框内会触发input事件的问题。

    问题描述: 监听文本输入框的input事件,在拼写汉字(输入法)但汉字并未实际填充到文本框中(选词)时会触发input事件,如图: 需要完成的需求就是在输入阶段不触发input中的事件,选词之后文字落 ...

  8. xml解析总结-常用需掌握

    Xml文档的解析 XML解析方式分为两种:DOM方式和SAX方式 DOM:Document Object Model, 文档对象模型.这种方式是W3C推荐的处理XML的一种方式. SAX:Simple ...

  9. PWA学习心得

    PWA学习心得 一.什么是PWA Progressive  Web  App , (渐进式增强 WEB 应用) 简称 PWA ,是提升WebApp的体验的一种新方法,能给用户原生应用的体验. PWA ...

  10. 易语言关于使用CURL,网页_访问,网页_访问S,网页_访问_对象,鱼刺(winHttpW)发送Get性能测试

    易语言关于使用 CURL,网页_访问,网页_访问S,网页_访问_对象,鱼刺(winHttpW)发送Get性能测试 测试模块情况: |-精易模块5.8  |-鱼刺类Http  |-libCURL +++ ...