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. 

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
 
 #include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define ll long long
typedef struct abcd
{
ll x,f,i;
} abcd;
abcd a[];
bool cmp1(abcd x,abcd y)
{
return x.x<y.x;
}
bool cmp2(abcd x,abcd y)
{
return x.f<y.f;
}
typedef struct abc
{
ll sum,ci;
}abc;
abc aa[];
ll need,need1,n;
ll lowbit(ll x)
{
return x&(-x);
}
void update(ll x)
{
ll y=x;
while(x<=n)
{
aa[x].ci++;
aa[x].sum+=y;
x+=lowbit(x);
}
}
ll fun(ll x,ll y)
{
ll ans,nu=,sum=,z=x;
while(x>)
{
nu+=aa[x].ci;
sum+=aa[x].sum;
x-=lowbit(x);
}
ans=y*((need-sum)-(need1-nu)*z)+y*(nu*z-sum);
return ans;
}
int main()
{
int i,j,now,noww;
ll ans;
while(~scanf("%d",&n))
{
for(i=; i<n; i++)
scanf("%I64d%I64d",&a[i].x,&a[i].f);
sort(a,a+n,cmp1);
now=a[].x,a[].x=,noww=;
for(i=; i<n; i++)
{
if(a[i].x==now)a[i].x=a[i-].x;
else now=a[i].x,a[i].x=noww;
noww++;
}
sort(a,a+n,cmp2);
now=a[].f,a[].f=,noww=;
for(i=; i<n; i++)
{
if(a[i].f==now)a[i].f=a[i-].f;
else now=a[i].f,a[i].f=noww;
noww++;
}
need=,need1=;
ans=;
memset(aa,,sizeof(aa));
for(i=n-;i>=;i--)
{
ans+=fun(a[i].x,a[i].f);
update(a[i].x);
need+=a[i].x;
need1++;
}
cout<<ans<<endl;
}
}
 

Disharmony Trees 树状数组的更多相关文章

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

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

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

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

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

  4. POJ 2029 Get Many Persimmon Trees (二维树状数组)

    Get Many Persimmon Trees Time Limit:1000MS    Memory Limit:30000KB    64bit IO Format:%I64d & %I ...

  5. POJ2029:Get Many Persimmon Trees(二维树状数组)

    Description Seiji Hayashi had been a professor of the Nisshinkan Samurai School in the domain of Aiz ...

  6. POJ 2029 Get Many Persimmon Trees(DP||二维树状数组)

    题目链接 题意 : 给你每个柿子树的位置,给你已知长宽的矩形,让这个矩形包含最多的柿子树.输出数目 思路 :数据不是很大,暴力一下就行,也可以用二维树状数组来做. #include <stdio ...

  7. POJ 2029 Get Many Persimmon Trees (模板题)【二维树状数组】

    <题目链接> 题目大意: 给你一个H*W的矩阵,再告诉你有n个坐标有点,问你一个w*h的小矩阵最多能够包括多少个点. 解题分析:二维树状数组模板题. #include <cstdio ...

  8. POJ 2029 Get Many Persimmon Trees 【 二维树状数组 】

    题意:给出一个h*w的矩形,再给出n个坐标,在这n个坐标种树,再给出一个s*t大小的矩形,问在这个s*t的矩形里面最多能够得到多少棵树 二维的树状数组,求最多能够得到的树的时候,因为h,w都不超过50 ...

  9. Get Many Persimmon Trees_枚举&&二维树状数组

    Description Seiji Hayashi had been a professor of the Nisshinkan Samurai School in the domain of Aiz ...

随机推荐

  1. 我的前端故事----关于redux的一些思考

    背景 我一个前端,今年第一份工作就是接手一个 APP 的开发...一个线下 BD 人员用的推广 APP,为了让我这个一天原生开发都没有学过的人能快速开发上线,于是乎就选择了 react-native ...

  2. 网络协议TFTP

    TFTP(Trivial File Transfer Protocol,简单文件传输协议)是TCP/IP协议族中的一个用来在客户端与服务器之间进行简单文件传输的协议.和使用TCP的文件传输协议(FTP ...

  3. python程序之profile分析

    操作系统 : CentOS7.3.1611_x64 python版本:2.7.5 问题描述 1.Python开发的程序在使用过程中很慢,想确定下是哪段代码比较慢: 2.Python开发的程序在使用过程 ...

  4. C++内存布局详解

    一个由C/C++编译的程序除了存放函数二进制代码的程序代码段(code段)外,数据占用的内存大致分为以下几个部分: 1.栈区(stack) 存放局部变量.函数参数.返回数据.返回地址等.系统自动分配释 ...

  5. Java 高级特性: Lambda 表达式

    本文源代码上传到了码云,请点击 LambdaExpression 获取.Lambda 表达式是 java 8 的新特性,本文讲解了 lambda 表达式的所有知识.内容涉及到 lambda 表达式是什 ...

  6. 低版本浏览器支持HTML5标签的方法

    最近刷了一道面试题,是关于低版本浏览器支持HTM5标签的写法,在网上找了一些,都行之有效,但是缺少整体总结,所以在这里总结一下,方便其他人过来阅读. IE低版本需要支持HTML5标签: 方法1.传统引 ...

  7. spring 整合Mybatis 《报错集合,总结更新》

    错误:java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldExcepti ...

  8. my new day in CNblog

    感谢大家 今天正式在博客园平台开启我的第三个技术面博客 之前一直坚持在csdn平台撰文(http://blog.csdn.net/github_38885296)欢迎参观:) 因为觉得博客园知名度虽不 ...

  9. Color.js增强你对颜色的控制

    往逝之因 不要低头,皇冠会掉...  可你又没有皇冠 Color.js 增强你对颜色的控制 阅读目录 轻松管理颜色--color.js库 使用color.js Accessor Methods 你该知 ...

  10. 关于SVM数学细节逻辑的个人理解(一)

    网上,书上有很多的关于SVM的资料,但是我觉得一些细节的地方并没有讲的太清楚,下面是我对SVM的整个数学原理的推导过程,其中我理解的地方力求每一步都是有理有据,希望和大家讨论分享. 首先说明,目前我的 ...