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. JavaScript 30 - 1 学习笔记

    学习JavaScirpt30的笔记! ...虽然英语不是很好,但是跟着来还是学到了一些东西. 1------->   JavaScirpt Drum Kit 功能是这样的 ,敲击键盘上面的按钮, ...

  2. CSS边框外的小三角形+阴影效果的实现。

    ...虽然是一个很小的问题,但其实还是挺实用的. 实现一个边框外的角. 用纯CSS来写. 一开始实现的效果是这个样子的. 但是这个效果没有办法给他附带阴影,所以换了一种写法.实现成了这个样子 想要实现 ...

  3. 【详细资料】ICN6211:MIPI DSI转RGB芯片简介

    ICN6211功能MIPI DSI转RGB,分辨率1920*1200,封装QFN48

  4. Python Requests: Invalid Header Name 解决方法

    这几天在练习python,并且用到了Requests,不得不说真的比urllib 方便了很多啊,简直有点事半功倍的感觉 言归正传,(好像上面的话也没多歪啦~~~~~) 简单叙述下我的script 流程 ...

  5. java设计模式系列之设计模式概要(1)

    一.什么是设计模式 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. ...

  6. plsql developer 恢复默认布局界面

    tools-preferences-appearance-(reset docking,reset toolbars)

  7. 最近找java实习面试被问到的东西总结(Java方向)

    时间,就是这么很悄悄的溜走了将近两个年华,不知不觉的,研二了,作为一个一般学校的研究生,不知道该说自己是不学无术,还是说有过努力,反正,这两年里,有过坚持,有过堕落,这不,突然间,有种开窍的急迫感,寻 ...

  8. 为什么会需要消息队列(MQ)?

    为什么会需要消息队列(MQ)? #################################################################################### ...

  9. FormData 上传多种格式的文件

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  10. 结对编程1 (四则运算基于GUI)

    https://git.coding.net/Luo_yujie/sizeyunsuan.app.git 201421123034 201421123032 1. 需求分析 这次作业新引用了语言选择, ...