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 ...
随机推荐
- 洛谷 P1383 codevs 3333 高级打字机
题目描述 早苗入手了最新的高级打字机.最新款自然有着与以往不同的功能,那就是它具备撤销功能,厉害吧. 请为这种高级打字机设计一个程序,支持如下3种操作: 1.T x:在文章末尾打下一个小写字母x.(t ...
- nyoj 547 优先队列
#include<stdio.h> #include<string.h> #include<queue>//水杯盛水问题,用优先队列不断从最小的边缘开始 using ...
- hdu 2089 数位dp入门题
#include<stdio.h> //dp[i][0]代表不存在不吉利数字 //dp[i][1]代表不存在不吉利数字但是以2开头 //dp[i][2]代表存在不吉利数字 #define ...
- x86、Linux、GNU、GNOME是什么
一.指令集架构: 指令集架构(英语:Instruction Set Architecture,缩写为ISA),又称指令集或指令集体系,是计算机体系结构中与程序设计有关的部分,包含了基本数据类型,指令集 ...
- Dagger2使用攻略
Dagger2使用攻略 Dagger 2 是 Square 的 Dagger 分支,是一种依赖注入框架.眼下由 Google 接手进行开发,Dagger2是使用代码自己主动生成和手写代码来实现依赖注入 ...
- javascript中的Base64.UTF8编码与解码详解
javascript中的Base64.UTF8编码与解码详解 本文给大家介绍的是javascript中的Base64.UTF8编码与解码的函数源码分享以及使用范例,十分实用,推荐给小伙伴们,希望大家能 ...
- 调试中的step into step over step out
step into/step out/step over的差别 step into就是单步运行,遇到子函数就进入而且继续单步运行: step over是在单步运行时,在函数内遇到子函数时不会进入子函数 ...
- 手游server之数据IO进化
这里数据IO是指游戏数据存盘和读取. 假设IO处理不好.server在IO时会导致.游戏卡顿较长的时间,严重影响游戏体验. 近期服务端刚好对IO这一块做了优化,把优化过程记录一下. 一 原始版 刚開始 ...
- Linux实时查看日志,访问前10IP 和相关命令
Nginx日志分析可以获得很多有用的信息,现在来试试最基本的,获取最多访问的前10个IP地址及访问次数. 既然是统计,那么awk是必不可少的,好用而高效. 命令如下: awk '{a[$1] += 1 ...
- php pdo具体操作
0x01:测试PDO是否安装成功 运行如下代码,如果提示参数错误,说明PDO已经安装,如果说明对象不存在,则修改PHP配置文件php.ini,取消php_pdo_yourssqlserverhere. ...