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 ...
随机推荐
- Spring Cloud-Bus(十二)
说明 用于分布式上所有微服务都连接到消息总线上面.进行统一的通知 Config动态刷新 configClient configClient通过/actuator/bus-refresh端点通知消息总线 ...
- [bzoj4196][Noi2015]软件包管理器_树链剖分_线段树
软件包管理器 bzoj-4196 Noi-2015 题目大意:Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件 ...
- jQuery必知要点(一)
1. jQuery框架的显著特点. jQuery强调的理念是写的少,做的多(write less.do more). 其主要特点有:轻量级.强大的选择器.美丽的DOM操作封装.可靠的事件处理机制.完好 ...
- HDU3117-Fibonacci Numbers(矩阵高速幂+log)
题目链接 题意:斐波那契数列,当长度大于8时.要输出前四位和后四位 思路:后四位非常easy,矩阵高速幂取模,难度在于前四位的求解. 已知斐波那契数列的通项公式:f(n) = (1 / sqrt(5 ...
- [HDU 1421]搬寝室(富有新意的DP)
题目地址:pid=1421" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1421 题目大 ...
- android 构建GPS Provide步骤及信息
构建GPS Provide步骤及信息 1. 用GPS2GoogleEarth录制KML文件 2. KML文件 2.1 经纬度解析 <LineString> <coordina ...
- global cache cr request
当一个进程访问需要一个或者多个块时,它会首先检查自己的CACHE是否存在该块,如果发现没有,就会先通过global cache赋予这些块 共享访问的权限,然后再访问.假如,通过global cache ...
- [Pulgin] 利用swfupload实现java文件批量上传
URL:http://blog.csdn.net/xuweilinjijis/article/details/8876305 之前在网上找过很多相关资料,很多所谓的批量上传都是忽悠人的,真正的批量上传 ...
- js滚动
有选择性的重复造一些轮子,未必是件坏事.Aaron的博客上加了一个悬浮菜单,貌似显得很高大上了.虽然这类小把戏也不是头一次见了,但是从未自己写过.今天就选择性的拿这个功能写一写.下面是这个轮子的开发过 ...
- Ubuntu搭建docker环境
一丶自己搭建Ubuntu的虚拟机(网上很多教程) PS:下带图形化界面的Ubuntu镜像,这里只说一下要装那些工具和做那些配置 安装vim sudo apt-get install ...