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 ...
随机推荐
- 【ACM】hdu_3782_xxx定律_201308011521
xxx定律Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 【ACM】hdu_1004_Let the Balloon Rise
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- I - Tunnel Warfare
I - Tunnel Warfare HDU - 1540 思路:原来以为自己已经完全理解了线段树,现在发现其实还差一些火候,做题的时候太拘泥于格式,思路不是很能放开. #include<cst ...
- Java上使用Lombok插件简化Getter、Setter方法
Maven引入依赖: <dependencies> <dependency> <groupId>org.projectlombok</groupId> ...
- HDU 4530
今天让人看不起了,话说好伤心,说我搞了ACM那么久都没获得拿得出手的奖.... 今晚爷爷我要狂刷2013腾讯马拉松的水题,奶奶滴,哈哈哈哈...T_T #include <iostream> ...
- RubyMine生成reader/writer方法
RubyMine生成reader/writer方法 在非类的ruby文件中,Alt+Insert会出现新建文件的选项: 在ruby文件的类中,Alt+Insert会出现get/set方法生成提示和重构 ...
- Win8下建立shortcut到開始界面
在win8前建立開始菜单都非常easy,但到win8就有点不一样了.它的開始菜单是metro风格的.以下我们来看下详细的实现代码.有兴趣的朋友能够自己測试下,它的作用是设置shortcut到metro ...
- Spring注解@RequestMapping请求路径映射问题
@RequestMapping请求路径映射,假设标注在某个controller的类级别上,则表明訪问此类路径下的方法都要加上其配置的路径.最经常使用是标注在方法上.表明哪个详细的方法来接受处理某次请求 ...
- 【cl】找不到火狐Cannot find firefox binary in PATH
org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is ins ...
- C++求解汉字字符串的最长公共子序列 动态规划
近期,我在网上看了一些动态规划求字符串最长公共子序列的代码.可是无一例外都是处理英文字符串,当处理汉字字符串时.常常会出现乱码或者不对的情况. 我对代码进行了改动.使用wchar_t类型存储字 ...