Problem 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

题意:求∑( 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 [数状数组]的更多相关文章

  1. hdu 3015 Disharmony Trees (离散化+树状数组)

    Disharmony Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. 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) 求出 ...

  3. HDU 1394Minimum Inversion Number 数状数组 逆序对数量和

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  4. HDU 3015 Disharmony Trees

    题解:在路边有一行树,给出它们的坐标和高度,先按X坐标排序.记录排名,记为rankx,再按它们的高度排序,记录排名,记为rankh.两颗树i,j的差异度为 fabs(rankx[i]-rankx[j] ...

  5. Disharmony Trees 树状数组

    Disharmony Trees Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  6. HDU 3015 Disharmony Trees 【 树状数组 】

    题意:给出n棵树,给出横坐标x,还有它们的高度h,先按照横坐标排序,则它们的横坐标记为xx, 再按照它们的高度排序,记为hh 两颗树的差异度为 abs(xx[i] - xx[j]) * min(hh[ ...

  7. HDU 1166 敌兵布阵 (数状数组,或线段树)

    题意:... 析:可以直接用数状数组进行模拟,也可以用线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...

  8. poj 2481 Cows(数状数组 或 线段树)

    题意:对于两个区间,[si,ei] 和 [sj,ej],若 si <= sj and ei >= ej and ei - si > ej - sj 则说明区间 [si,ei] 比 [ ...

  9. BZOJ2120:数颜色(数状数组套主席树)(带修改的莫对)

    墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. 2. R P ...

随机推荐

  1. 洛谷 P2805 BZOJ 1565 植物大战僵尸

    题目描述 Plants vs. Zombies(PVZ)是最近十分风靡的一款小游戏.Plants(植物)和Zombies(僵尸)是游戏的主角,其中Plants防守,而Zombies进攻.该款游戏包含多 ...

  2. 【ACM】hdu_1094_A+BVI_201307261731

    A+B for Input-Output Practice (VI)Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3276 ...

  3. ubuntu中安装hadoop集群

    hadoop是由java 语言编写的主从结构分布式计算存储架构 准备工作: 操作系统: Ubuntu16.04 软件安装包:jdk-8u171-linux-x64.tar.gz : hadoop-2. ...

  4. java内存结构(执行时数据区域)

    java虚拟机规范规定的java虚拟机内存事实上就是java虚拟机执行时数据区,其架构例如以下: 当中方法区和堆是由全部线程共享的数据区. Java虚拟机栈.本地方法栈和程序计数器是线程隔离的数据区. ...

  5. MRv1到MRv2

    概述 引入YARN作为通用资源调度平台后.Hadoop得以支持多种计算框架,如MapReduce.Spark.Storm等. MRv1是Hadoop1中的MapReduce,MRv2是Hadoop2中 ...

  6. Nova和Heat中的servergroup

    如今nova能够通过命令创建一个server group,在server group中的vm能够指定一些policy. 这些policy包含affinity和anti-affinity.affinit ...

  7. LINQ查询知识总结

    -------适合自己的才是最好的!!! LINQ查询知识总结:案例分析 案例:汽车表car,系列表brand,厂商表productor private MyCarDataContext  _Cont ...

  8. [Wikioi 1226]倒水问题

    题目描写叙述 Description 有两个无刻度标志的水壶.分别可装 x 升和 y 升 ( x,y 为整数且均不大于 100 )的水. 设另有一水 缸,可用来向水壶灌水或接从水壶中倒出的水, 两水壶 ...

  9. 报错configure:error: no acceptable C compiler found in $PATH。。

    报错configure:error: no acceptable C compiler found in $PATH.. 查看日志: 出错原因:新安装的linux系统,没有gcc库 解决方案:使用yu ...

  10. Zookeeper体系结构

    上面我们已经讨论了zookeeper在应用程序中的一些操作,以下我们须要理解一下服务端的工作的原理.client是怎样通过一个client的类库与服务端进行通信的,然后服务端又是怎样回应client的 ...