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
题意:给你一些树的x坐标还有高度h,让你求任意两个树之间的价值的总和,两颗树之间的价值为v=min(h1,h2)*abs(x1,x2)
其中这颗树的x和h是经过有规律的变化的,用3 3 1 3 4来说,对这组数进行从小到大的排序之后—>1 3 3 3 4,此时1所在的位置是1,第一个3所在的位置是2,如果后面的数和前一位数相等,那这个数用的值是和这个数相等的数第1次出现的位置,否则它用现在它所处位置—>1 2 2 2 5,再变回来—>2 2 1 2 5,就和题目给的例子变化一样了。
思路:我们可以先对树的h进行排序使它每一次所用的h及为最小的,之后用x进行计算,计算此x之前有多少个树,总和是多少
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
#define lowbit(x) x&(-x)
#define ll long long
int n;
const int maxx=100005;
ll c1[maxx],c2[maxx];
struct cai
{
int x,h,xx,hh;
}tree[maxx];
int er1(cai q,cai p)
{
return q.x<p.x;
}
int er2(cai q,cai p)
{
return q.h<p.h;
}
int er3(cai q,cai p)
{
return q.hh>p.hh;
}
void update(ll c[],ll x,ll v)
{
for(int i=x;i<=maxx;i+=lowbit(i))
c[i]+=v;
}
ll gestsum(ll c[],ll x)
{
ll num=0;
for(int i=x;i;i-=lowbit(i))
num+=c[i];
return num;
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
scanf("%d%d",&tree[i].x,&tree[i].h);
}
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));
sort(tree+1,tree+1+n,er1);
tree[1].xx=1;
for(int i=2;i<=n;i++)
{
if(tree[i].x==tree[i-1].x)
tree[i].xx=tree[i-1].xx;
else
tree[i].xx=i;
}
sort(tree+1,tree+1+n,er2);
tree[1].hh=1;
for(int i=2;i<=n;i++)
{
if(tree[i].h==tree[i-1].h)
tree[i].hh=tree[i-1].hh;
else
tree[i].hh=i;
}
sort(tree+1,tree+1+n,er3);
ll a,sum=0,b,d;
for(int i=1;i<=n;i++)
{
a=gestsum(c1,tree[i].xx);//tree[i].xx之前的个数
b=gestsum(c2,tree[i].xx);//tree[i].xx之前的数目和
d=gestsum(c2,n);//总得和
sum+=tree[i].hh*(a*tree[i].xx-b+d-b-(i-a-1)*tree[i].xx);//求那一段的和
update(c1,tree[i].xx,1);
update(c2,tree[i].xx,tree[i].xx);//关键在于这里对树状数组的离散化
}
printf("%lld\n",sum);
}
}