码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 3015 Disharmony Trees (离散化+树状数组)

时间:2014-05-05 10:16:18      阅读:486      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   java   

Disharmony Trees

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


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.
bubuko.com,布布扣

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
 

 

Source
 

 

Recommend
gaojie   |   We have carefully selected several similar problems for you:  3016 3011 3013 3018 3030 
 

 

bubuko.com,布布扣
 1 //203MS    4548K    1948 B    C++ 
 2 /*
 3     题意:
 4         给出n个树的位置和高度,求每两棵数的位置排名差绝对值与高度排名较小的的积的和。
 5      
 6     离散化+树状数组:
 7         不错的一道题,我卡在结果的计算,的确对树状数组计算的能力的理解不够! 
 8         用两个排序排出位置和高度的排名,然后再对高度排名在进行计算,计算公式:
 9             ans+=p[i].hid*(cnt*p[i].xid-sum+tsum-sum-(i-cnt-1)*p[i].xid); //每个高度 
10     算法时间复杂度为O(n*lgn)
11  
12 */ 
13 #include<stdio.h>
14 #include<string.h>
15 #include<stdlib.h>
16 #define N 100005
17 struct node{
18     int x,h;
19     int xid,hid;
20     int id;
21 }p[N],p1[N],p2[N];
22 __int64 cnum[N],clen[N];
23 int cmpx(const void *a,const void*b)
24 {
25     return (*(node*)a).x-(*(node*)b).x;
26 }
27 int cmph(const void*a,const void*b)
28 { 
29     return (*(node*)a).h-(*(node*)b).h;
30 }
31 int cmp0(const void*a,const void*b)
32 {
33     return (*(node*)b).hid-(*(node*)a).hid;
34 }
35 int lowbit(int k)
36 {
37     return (-k)&k;
38 }
39 void update(__int64 c[],int k,int detal)
40 {
41     for(;k<N;k+=lowbit(k))
42         c[k]+=detal;
43 }
44 __int64 getsum(__int64 c[],int k)
45 {
46     __int64 s=0;
47     for(;k>0;k-=lowbit(k))
48         s+=c[k];
49     return s;
50 }
51 int main(void)
52 {
53     int n,x,h;
54     while(scanf("%d",&n)!=EOF)
55     {
56         memset(cnum,0,sizeof(cnum));
57         memset(clen,0,sizeof(clen));
58         for(int i=1;i<=n;i++){ 
59             scanf("%d%d",&p[i].x,&p[i].h);
60             p[i].id=i;
61             p1[i]=p2[i]=p[i];
62         }
63         qsort(p1+1,n,sizeof(p1[0]),cmpx);
64         for(int i=1,j=1;i<=n;i++){
65             if(i>1 && p1[i].x!=p1[i-1].x) j=i;
66             p[p1[i].id].xid=j;
67         }
68         qsort(p2+1,n,sizeof(p2[0]),cmph);
69         for(int i=1,j=1;i<=n;i++){
70             if(i>1 && p2[i].h!=p2[i-1].h) j=i;
71             p[p2[i].id].hid=j;
72         }
73         qsort(p+1,n,sizeof(p[0]),cmp0);
74         __int64 ans=0;
75         //for(int i=1;i<=n;i++) printf("*%d %d\n",p[i].hid,p[i].xid);
76         update(cnum,p[1].xid,1);
77         update(clen,p[1].xid,p[1].xid);
78         for(int i=2;i<=n;i++){
79             __int64 cnt=getsum(cnum,p[i].xid);
80             __int64 sum=getsum(clen,p[i].xid); 
81             __int64 tsum=getsum(clen,N-1);  
82             ans+=p[i].hid*(cnt*p[i].xid-sum+tsum-sum-(i-cnt-1)*p[i].xid);
83             update(cnum,p[i].xid,1);
84             update(clen,p[i].xid,p[i].xid);
85         }
86         printf("%I64d\n",ans);
87     }
88     return 0;
89 }
bubuko.com,布布扣

 

 

hdu 3015 Disharmony Trees (离散化+树状数组),布布扣,bubuko.com

hdu 3015 Disharmony Trees (离散化+树状数组)

标签:des   style   blog   class   code   java   

原文地址:http://www.cnblogs.com/GO-NO-1/p/3707853.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!