http://acm.hdu.edu.cn/showproblem.php?pid=3015
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
题目大意:给定n棵树的高度和宽度,进行离散化处理后(这个和平常的离散化不太一样 少了去重的操作),定义两棵树
u
、
v
u、v
u、v之间的神秘能量为
m
i
n
(
u
y
,
v
y
)
∗
a
b
s
(
u
x
−
v
x
)
min(u_{y},v_{y})*abs(u_{x}-v_{x})
min(uy,vy)∗abs(ux−vx),求出任意两棵树之间的神秘能量之和。
思路:这题和POJ 1990差不多。考虑对树按照纵坐标
y
y
y进行排序,从大到小开始计算答案,这样对于第
i
i
i棵树来说,最小的
y
y
y一定是
i
y
i_{y}
iy,因此只用考虑
a
b
s
(
j
y
−
i
y
)
,
j
<
i
abs(j_{y}-i_{y}),j<i
abs(jy−iy),j<i即可。
规定:
a
[
i
]
.
x
表
示
第
i
棵
树
的
横
坐
标
a
[
i
]
.
y
表
示
第
i
棵
树
的
纵
坐
标
a[i].x表示第i棵树的横坐标\quad a[i].y表示第i棵树的纵坐标
a[i].x表示第i棵树的横坐标a[i].y表示第i棵树的纵坐标已知:
t
m
p
1
=
前
i
−
1
棵
树
中
横
坐
标
<
=
a
[
i
]
.
x
的
树
的
个
数
tmp1=前i-1棵树中横坐标<=a[i].x的树的个数
tmp1=前i−1棵树中横坐标<=a[i].x的树的个数
t
m
p
2
=
前
i
−
1
棵
树
中
坐
标
<
=
a
[
i
]
.
x
的
树
的
横
坐
标
之
和
tmp2=前i-1棵树中坐标<=a[i].x的树的横坐标之和
tmp2=前i−1棵树中坐标<=a[i].x的树的横坐标之和
t
o
t
=
前
i
−
1
棵
树
的
横
坐
标
之
和
tot=前i-1棵树的横坐标之和
tot=前i−1棵树的横坐标之和那么我们可以推得:
t
m
p
3
=
i
−
t
m
p
1
=
前
i
−
1
棵
树
中
横
坐
标
>
a
[
i
]
.
x
的
树
的
个
数
tmp3=i-tmp1=前i-1棵树中横坐标>a[i].x的树的个数
tmp3=i−tmp1=前i−1棵树中横坐标>a[i].x的树的个数
t
m
p
4
=
t
o
t
−
t
m
p
2
=
前
i
−
1
棵
树
中
横
坐
标
>
a
[
i
]
.
x
的
树
的
横
坐
标
之
和
tmp4=tot-tmp2=前i-1棵树中横坐标>a[i].x的树的横坐标之和
tmp4=tot−tmp2=前i−1棵树中横坐标>a[i].x的树的横坐标之和我们分成两部分计算贡献:
横
坐
标
<
=
a
[
i
]
.
x
的
树
的
贡
献
=
a
[
i
]
.
y
∗
(
a
[
i
]
.
x
∗
t
m
p
1
−
t
m
p
2
)
横坐标<=a[i].x的树的贡献=a[i].y*(a[i].x*tmp1-tmp2)
横坐标<=a[i].x的树的贡献=a[i].y∗(a[i].x∗tmp1−tmp2)
横
坐
标
>
a
[
i
]
.
x
的
树
的
贡
献
=
a
[
i
]
.
y
∗
(
t
m
p
4
−
a
[
i
]
.
x
∗
t
m
p
3
)
横坐标>a[i].x的树的贡献=a[i].y*(tmp4-a[i].x*tmp3)
横坐标>a[i].x的树的贡献=a[i].y∗(tmp4−a[i].x∗tmp3)现在已经知道怎么计算答案了,关键在于如何求tmp1和tmp2,其实很简单,用两个树状数组分别维护tmp1和tmp2就好了。详情见代码。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
struct node
{
int x,y;
bool operator <(const node &a)const
{
return y>a.y;
}
}a[maxn];
int n;
int x[maxn];
int y[maxn];
ll tree1[maxn];//[1,i]中横坐标小于等于i的个数
ll tree2[maxn];//[1,i]中横坐标小于等于i的横坐标之和
inline int lowbit(int x)
{
return x&(-x);
}
inline void add(int flag,int i,ll v)
{
if(flag==1)
for(;i<=n;i+=lowbit(i))
tree1[i]+=v;
else
for(;i<=n;i+=lowbit(i))
tree2[i]+=v;
}
inline ll sum(int flag,int i)
{
ll ans=0;
if(flag==1)
for(;i;i-=lowbit(i))
ans+=tree1[i];
else
for(;i;i-=lowbit(i))
ans+=tree2[i];
return ans;
}
int main()
{
while(~scanf("%d",&n))
{
memset(tree1,0,sizeof(tree1));
memset(tree2,0,sizeof(tree2));
for(int i=0;i<n;i++)
scanf("%d%d",&x[i],&y[i]),a[i].x=x[i],a[i].y=y[i];
sort(x,x+n);
sort(y,y+n);
for(int i=0;i<n;i++)
a[i].x=lower_bound(x,x+n,a[i].x)-x+1,
a[i].y=lower_bound(y,y+n,a[i].y)-y+1;
sort(a,a+n);
ll ans=0,tot=0,tmp1,tmp2,tmp3,tmp4;
for(int i=0;i<n;i++)
{
tmp1=sum(1,a[i].x);//横坐标小于等于 a[i].x 的树的个数
tmp2=sum(2,a[i].x);//横坐标小于等于 a[i].x 的树的横坐标之和
tmp3=i-tmp1;//横坐标大于 a[i].x 的树的个数
tmp4=tot-tmp2;//横坐标大于 a[i].x 的树的横坐标之和
ans+=a[i].y*(a[i].x*tmp1-tmp2)+a[i].y*(tmp4-a[i].x*tmp3);
tot+=a[i].x;
add(1,a[i].x,1);
add(2,a[i].x,a[i].x);
}
printf("%lld\n",ans);
}
return 0;
}