http://acm.hdu.edu.cn/showproblem.php?pid=3015
Disharmony Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 783 Accepted Submission(s): 378
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.

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.
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
思路: 树状数组
分析:
1 题目给定n棵树的横坐标和高度,然后给定横坐标排序后的rank_f已及树的高度排序后的rank_s,规定两颗树的FAR为abs(rank_f[i] , rank_f[j]) , SHORT为min(rank_s[i] . rank[_sj]),那么i和j的组合的值为FAR*SHORT,求所有组合方式的总和
2 我们按照树的高度从大到小排序,然后我们分别求出每棵树的rank_f和rank_s , 然后我们就可以利用poj 1990
的思路去做
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef __int64 int64;
const int MAXN = 1e5+10;
struct Node{
int x;
int h;
int f;
int s;
bool operator<(const Node& s)const{
if(h > s.h)
return true;
else if(h == s.h && x > s.x)
return true;
return false;
}
};
Node node[MAXN];
int n , num[MAXN];
int64 treeNum[MAXN];
int64 treeRank[MAXN];
int lowbit(int x){
return x&(-x);
}
int64 getSum(int x , int64 *arr){
int64 sum = 0;
while(x){
sum += arr[x];
x -= lowbit(x);
}
return sum;
}
void add(int x , int val , int64 *arr){
while(x < MAXN){
arr[x] += val;
x += lowbit(x);
}
}
void init(){
int s = 1;
sort(num+1 , num+n+1);
for(int i = n ; i > 0 ; i--){
if(i < n && node[i].h == node[i+1].h)
node[i].s = node[i+1].s;
else
node[i].s = s;
s++;
node[i].f = lower_bound(num+1 , num+n+1 , node[i].x)-num;
}
}
int64 solve(){
int64 ans = 0;
sort(node+1 , node+n+1);
init();
memset(treeNum , 0 , sizeof(treeNum));
memset(treeRank , 0 , sizeof(treeRank));
int64 total = 0;
for(int i = 1 ; i <= n ; i++){
int64 f = node[i].f;
int64 s = node[i].s;
int64 count = getSum(f , treeNum);
int64 sum = getSum(f , treeRank);
ans += s*(count*f-sum);
ans += s*(total-sum-(i-1-count)*f);
add(f , 1 , treeNum);
add(f , f , treeRank);
total += f;
}
return ans;
}
int main(){
while(scanf("%d" , &n) != EOF){
for(int i = 1 ; i <= n ; i++){
scanf("%d%d" , &node[i].x , &node[i].h);
num[i] = node[i].x;
}
printf("%I64d\n" , solve());
}
return 0;
}