# 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.

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <functional>
#include <cmath>
#include <cctype>
#include <cfloat>
#include <climits>
#include <complex>
#include <deque>
#include <list>
#include <set>
#include <utility>
using namespace std;

int n;

#define BIT_SIZE 100011
class BIT
{
public:
BIT(){for(int i=1;i<BIT_SIZE;i++)c[i]=0;}
void update(int i,__int64 val){while(i<BIT_SIZE){c[i]+=val;i+=i&(-i);}}
__int64 sum(int i){__int64 ans=0;while(i>0){ans+=c[i];i-=i&(-i);}return ans;}
__int64 get(int i){if(i>0)return sum(i)-sum(i-1);return 0;}
void clear(){for(int i=1;i<BIT_SIZE;i++)c[i]=0;}
private:
__int64 c[BIT_SIZE];
};

struct tre{
int x;
int h;
}s[100010],st[100010];

bool cmp1(tre p,tre q)
{
return p.x<q.x;
}

bool cmp2(tre p,tre q)
{
return p.h<q.h;
}

int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d",&n))
{
BIT bita,bitb;
memset(s,0,sizeof s);
for(int i=1;i<=n;i++)
scanf("%d %d",&s[i].x,&s[i].h);
sort(s+1,s+n+1,cmp1);
memcpy(st,s,sizeof s);
for(int i=1;i<=n;i++){
if(s[i].x!=s[i-1].x)
st[i].x=i;
else
st[i].x=st[i-1].x;
}
sort(st+1,st+n+1,cmp2);
memcpy(s,st,sizeof s);
for(int i=1;i<=n;i++){
if(st[i].h!=st[i-1].h)
s[i].h=i;
else
s[i].h=s[i-1].h;
}
__int64 ans=0,temp=0;
for(int i=n;i>0;i--){
ans+=bitb.sum(s[i].x-1)*s[i].x*s[i].h;
ans-=s[i].h*bita.sum(s[i].x-1);
ans+=s[i].h*(bita.sum(BIT_SIZE-1)-bita.sum(s[i].x));
ans-=(bitb.sum(BIT_SIZE-1)-bitb.sum(s[i].x))*s[i].x*s[i].h;
bita.update(s[i].x,s[i].x);
bitb.update(s[i].x,1);
}
printf("%I64d\n",ans);
}
return 0;
}

#### hdu 3015 Disharmony Trees

2013-08-19 14:45:20

#### HDU 3015 Disharmony Trees

2013-10-29 17:25:44

#### HDU - 3015 Disharmony Trees

2014-04-07 12:49:29

#### hdu3015 Disharmony Trees

2015-06-10 20:32:10

#### hdu3015 Disharmony Trees(树状数组）

2012-09-30 17:38:21

#### hdu 3015 Disharmony Trees

2012-05-08 22:52:56

#### HDU3015 Disharmony Trees

2014-07-19 16:59:46

#### hdu3015 Disharmony Trees(树状数组+排序)

2015-08-21 10:31:55

#### hdu 3015 Disharmony Trees（树状数组）

2016-10-28 14:01:43

#### 【TOJ 3401.】Disharmony Trees【树状数组】

2014-08-13 13:33:14