HDU-3015 Disharmony Trees [数状数组]
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.
10 100
20 200
4
10 100
50 500
20 200
20 100
题意:求∑( abs(Xi-Xj)*min(Hi,Hj) )
解:离散化X和H,对H升序排序,由于计算对H取的是最小值,因此只要考虑每项对后续的贡献;
对于每一项i,∑( abs(Xi-Xj) )可以表达为 (前项个数*Xi - 前项总和) + (后项总和 - 后项个数*Xi );因此用两个树状数组分别维护个数和总和,遍历一遍求和,对每项计算完贡献后在数状数组中删去。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define lowbit(x) (x&(-x))
#define eps 0.00000001
#define PI acos(-1)
#define pn printf("\n");
using namespace std; const int maxn = 1e5 + ;
int n;
struct node{
ll x, h;
}arr[maxn]; ll cnt[maxn], sum[maxn]; void update(ll pos,ll val, ll *c)
{
while(pos <= maxn)
{
c[pos] += val;
pos += lowbit(pos);
}
} ll query(ll pos, ll*c)
{
ll ret = ;
while(pos > )
{
ret += c[pos];
pos -= lowbit(pos);
}
return ret;
} int main()
{
while(~scanf("%d", &n))
{
memset(cnt, , sizeof cnt);
memset(sum ,, sizeof sum);
for(int i=;i<n;i++)
scanf("%lld%lld",&arr[i].x, &arr[i].h);
sort(arr, arr+n, [](node a, node b){
return a.x < b.x;
});
for(int i=;i<n;i++)
{
int j = i+;
ll mk = arr[i].x;
arr[i].x = i+;
while(j < n && arr[j].x == mk)
{
arr[j].x = i+;
j ++;
}
i = j - ;
} sort(arr, arr+n, [](node a, node b){
return a.h < b.h;
});
for(int i=;i<n;i++)
{
int j = i+;
ll mk = arr[i].h;
arr[i].h = i+;
while(j < n && arr[j].h == mk)
{
arr[j].h = i+;
j ++;
}
i = j - ;
} for(int i=;i<n;i++)
{
update(arr[i].x, , cnt);
update(arr[i].x, arr[i].x, sum);
} ll ans = ;
for(int i=;i<n;i++)
{
ll pre_cnt = query(arr[i].x - , cnt);
ll pst_cnt = n-i - query(arr[i].x, cnt);
ll pre_sum = query(arr[i].x - , sum);
ll pst_sum = query(n, sum) - query(arr[i].x, sum); update(arr[i].x, -, cnt);
update(arr[i].x, -arr[i].x, sum); ans += ( pre_cnt * arr[i].x - pre_sum + pst_sum - pst_cnt * arr[i].x) * arr[i].h;
}
printf("%lld\n", ans);
}
}
HDU-3015 Disharmony Trees [数状数组]的更多相关文章
- hdu 3015 Disharmony Trees (离散化+树状数组)
Disharmony Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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 1394Minimum Inversion Number 数状数组 逆序对数量和
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- HDU 3015 Disharmony Trees
题解:在路边有一行树,给出它们的坐标和高度,先按X坐标排序.记录排名,记为rankx,再按它们的高度排序,记录排名,记为rankh.两颗树i,j的差异度为 fabs(rankx[i]-rankx[j] ...
- Disharmony Trees 树状数组
Disharmony Trees Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- HDU 3015 Disharmony Trees 【 树状数组 】
题意:给出n棵树,给出横坐标x,还有它们的高度h,先按照横坐标排序,则它们的横坐标记为xx, 再按照它们的高度排序,记为hh 两颗树的差异度为 abs(xx[i] - xx[j]) * min(hh[ ...
- HDU 1166 敌兵布阵 (数状数组,或线段树)
题意:... 析:可以直接用数状数组进行模拟,也可以用线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...
- poj 2481 Cows(数状数组 或 线段树)
题意:对于两个区间,[si,ei] 和 [sj,ej],若 si <= sj and ei >= ej and ei - si > ej - sj 则说明区间 [si,ei] 比 [ ...
- BZOJ2120:数颜色(数状数组套主席树)(带修改的莫对)
墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. 2. R P ...
随机推荐
- CodeForces - 445A - DZY Loves Chessboard
先上题目: A. DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input ...
- [SCSS] Convert SCSS Variable Arguments to JavaScript
We will learn how to convert variable arguments by using rest operator in JavaScript. .sass-btn { co ...
- iOS GCD使用指南
Grand Central Dispatch(GCD)是异步运行任务的技术之中的一个. 一般将应用程序中记述的线程管理用的代码在系统级中实现.开发人员仅仅须要定义想运行的任务并追加到适当的Dispat ...
- MFC画标尺
void CJjjView::OnPaint() { CPaintDC dc(this); //屏幕初始化 dc.SetMapMode(MM_LOENGLISH);//0.01in ;1英寸映射 dc ...
- [BZOJ 3387] Fence Obstacle Course
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3387 [算法] f[i][0]表示从第i个栅栏的左端点走到原点的最少移动步数 f[i ...
- 【转】寻找最好的笔记软件:海选篇 (v1.0)
原文网址:http://blog.sina.com.cn/s/blog_46dac66f01000b55.html 序言: 我见过的多数软件爱好者,无论是资深用户,还是初级用户,都有一个梦想:找到 ...
- spring框架spring之HibernateTemplate
转自:https://blog.csdn.net/acmman/article/details/44652207
- js设计模式-享元模式
享元模式实际上是一种优化模式,目的在于提高系统的性能和代码的效率. 使用享元模式的条件:最重要的条件是网页中必须使用了大量资源密集型对象,如果只会用到了少许这类对象,那么这种优化并不划算.第二个条件是 ...
- Ubuntu16.04下将hadoop2.7.3源代码导入到eclipse neon中
0.为什么会有这篇: 这篇文章的目的在于帮助想学习hadoop源码的内容,却在导入的过程中出现了各种问题的人. 或许你一定找了很多博客都无果,那么不用担心,我和你一样,这也是这篇文章存在的意义,废话少 ...
- Solr.NET快速入门(三)【高亮显示】
此功能会"高亮显示"匹配查询的字词(通常使用标记),包括匹配字词周围的文字片段. 要启用高亮显示,请包括HighlightingParameters QueryOptions对象, ...