Disharmony Trees
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
ps:在复习考试的过程中想了几天,最后发现也并不难。。
思路:首先把排名预处理好。看到题中有S=min(H1,H2),因此可以按照H从大到小排序,这样当遍历到这一项的时候,只需要乘以这一项的H就行了。
然后用两个树状数组维护前缀和,一个数组记录这一项前面有多少个小于这一项D的数,另一个数组记录这一项前面所有小于这一项D的数的和,然后计算就行了(具体详见代码)
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long LL;
const LL maxn=100000+10;
const LL N=100000;
LL n;
LL a[maxn],b[maxn];
struct node//记录树的位置和高度
{
LL dis,high;
LL index;
}q[maxn];
struct NODE//记录树按照两种排序方法排序后的排名
{
LL D,H;
}p[maxn];
bool cmp1(node A,node B)
{
return A.dis<B.dis;
}
bool cmp2(node A,node B)
{
return A.high<B.high;
}
bool cmp3(NODE A,NODE B)
{
if(A.H==B.H)
return A.D>B.D;
return A.H>B.H;
}
LL lowbit(LL x)
{
return x&-x;
}
void add(LL x,LL val,LL tmp[])
{
while(x<=N)
{
tmp[x]+=val;
x+=lowbit(x);
}
}
LL getsum(LL x,LL tmp[])
{
LL sum=0;
while(x)
{
sum+=tmp[x];
x-=lowbit(x);
}
return sum;
}
int main()
{
while(~scanf("%lld",&n))
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(LL i=1;i<=n;++i)
{
scanf("%lld%lld",&q[i].dis,&q[i].high);
q[i].index=i;
}
sort(q+1,q+n+1,cmp1);
LL k=0,tmp=0;
for(LL i=1;i<=n;++i)
{
if(q[i].dis==tmp)
++k;
else
k=0;
tmp=q[i].dis;
p[q[i].index].D=i-k;
}
sort(q+1,q+n+1,cmp2);
k=0,tmp=0;
for(LL i=1;i<=n;++i)
{
if(q[i].high==tmp)
++k;
else
k=0;
tmp=q[i].high;
p[q[i].index].H=i-k;
}
sort(p+1,p+1+n,cmp3);//按照H从大到小排序
LL ans=0,x1,y1,x2,y2;
for(LL i=1;i<=n;++i)
{
x1=getsum(p[i].D,a);//前面有多少个小于p[i].D的数
y1=getsum(p[i].D,b);//前面所有小于p[i].D的数的和
ans+=(x1*p[i].D-y1)*p[i].H;
x2=i-x1-1;//后面有多少个小于p[i].D的数
y2=getsum(N,b)-y1;//后面所有小于p[i].D的数的和
ans+=(y2-x2*p[i].D)*p[i].H;
add(p[i].D,1,a);
add(p[i].D,p[i].D,b);
}
printf("%lld\n",ans);
}
return 0;
}