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

• u011345136
• 2014年04月07日 12:49
• 798

hdu 3015 Disharmony Trees

• cgl1079743846
• 2013年08月19日 14:45
• 1260

hdu3015 Disharmony Trees

Problem Description One day Sophia finds a very big square. There are n trees in the square. They...
• Kirito_Acmer
• 2015年06月10日 20:32
• 557

hdu3015 Disharmony Trees(树状数组）

• fire__ice
• 2012年09月30日 17:38
• 487

hdu 3015 Disharmony Trees

• gotoac
• 2012年05月08日 22:52
• 424

HDU3015 Disharmony Trees

Disharmony Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other...
• eeeaaaaa
• 2014年07月19日 16:59
• 201

HDU 3015 Disharmony Trees

• fjy4328286
• 2013年10月29日 17:25
• 508

HDU 3015 Disharmony Trees 树状数组

• wmn_wmn
• 2012年09月29日 21:38
• 1131

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

• u010089558
• 2014年08月13日 13:33
• 292

HDU 3015 Disharmony Trees (线段树)

• kbdwo
• 2013年09月26日 13:44
• 475

举报原因： 您举报文章：Disharmony Trees 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)