Disharmony Trees

来源:互联网 发布:qq群发信息软件 编辑:程序博客网 时间:2024/05/19 09:16

Disharmony Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 959    Accepted Submission(s): 480


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
210 10020 200410 10050 50020 20020 100
 

Sample Output
113
 

两个一维树状数组维护,存下代码

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <vector>#include <map>using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 100000+10;typedef long long LL;LL cnum[maxn];//维护x等级比他小的个数LL csum[maxn];//维护x等级比他小的x等级和struct node{    LL h,x,idx,idh;}a[maxn];bool cmp(node a,node b){    if(a.idh == b.idh)        return a.idx<b.idx;    return a.idh>b.idh;}bool cmp2(node a,node b){    return a.x<b.x;}bool cmp3(node a,node b){    return a.h<b.h;}int n;int lowbit(int x){    return x&-x;}void updata(LL x,LL add,LL c[]){    for(int i = x; i <= n; i += lowbit(i))        c[i] += add;}LL sum(LL x,LL c[]){    LL ans = 0;    for(int i = x; i > 0; i -= lowbit(i))        ans += c[i];    return ans;}int main(){    LL ans;    while(~scanf("%d",&n)){        ans = 0;        memset(a,0,sizeof(a));        memset(cnum,0,sizeof(cnum));        memset(csum,0,sizeof(csum));        for(int i = 0; i < n; i++)            scanf("%lld %lld",&a[i].x,&a[i].h);        sort(a,a+n,cmp2);        LL m = 1;        a[0].idx = m;        for(int i = 1; i < n; i++){            m++;            if(a[i].x == a[i-1].x)                a[i].idx = a[i-1].idx;            else                a[i].idx = m;        }        sort(a,a+n,cmp3);        m = 1;        a[0].idh = m;        for(int i = 1; i < n; i++){            m++;            if(a[i].h == a[i-1].h)                a[i].idh = a[i-1].idh;            else                a[i].idh = m;        }        sort(a,a+n,cmp);        for(int i = 0; i < n; i++){            LL num = sum(a[i].idx,cnum);//x等级比他小的个数            LL Sum = sum(a[i].idx,csum);//x等级比他小的等级和            LL Sumall = sum(n,csum);//所有高度等级比他小的x等级和            ans += a[i].idh*(a[i].idx*num-Sum + (Sumall-Sum)-a[i].idx*(i-num));            updata(a[i].idx,1,cnum);            updata(a[i].idx,a[i].idx,csum);        }        printf("%lld\n",ans);    }    return 0;}


0 0
原创粉丝点击