HDU 5876 Disharmony Trees(离散化 树状数组)

版权声明:From: http://blog.csdn.net/tju_tahara https://blog.csdn.net/TJU_Tahara/article/details/75905476

Disharmony Trees

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


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
 
  
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   2838  





题目大意:求 所有 min(H1 , H2) * abs(D1 - D2)的和,其中H 和D rank值。

解法:按照题目要求离散化之后按照H从小到大树状数组求解即可。迷之写错WA了很多次。。



#include<cstdio>
#include <cmath>
#include <vector>
#include <complex>
#include<cstdlib>
#include<string.h>
#include <iostream>
#include <algorithm>
#define MAX 100100

using std::sort;
using std::lower_bound;

long long int arr[MAX], m1[MAX], m2[MAX];

struct node{
    long long int h, x;
}st[MAX], st2[MAX];

bool operator<(const node& a, const node& b){
    return a.h < b.h;
}

bool cmp(const node& a, const node& b){
    return a.x < b.x;
}

inline int lowbit(int n){
    return n & (-n);
}

void update(int p, int n, long long int m_arr[]){
    while(p < MAX){
        m_arr[p] += n;
        p += lowbit(p);
    }
}

long long int get(int p, long long int m_arr[]){
    long long int res = 0;
    while(p > 0){
        res += m_arr[p];
        p -= lowbit(p);
    }
    return res;
}


int main()
{
    int num, i;
    long long int res, tmp, s, t, lst;
    while(~scanf("%d", &num)){
        
        memset(m1, 0, sizeof(m1));
        memset(m2, 0, sizeof(m2));
        res = 0;

        for(i = 0; i < num; i++){
            scanf("%lld%lld", &st2[i].x, &st2[i].h);
        }
        sort(st2, st2 + num, cmp);
        
        arr[0] = 1; st[0].x = 1; st[0].h = st2[0].h;
        for(i = 1; i < num; i++){
            if(st2[i].x == st2[i - 1].x){
                arr[i] = st[i].x = arr[i - 1];
            }
            else{
                arr[i] = st[i].x = i + 1;
            }
            st[i].h = st2[i].h;
        }
        sort(st, st + num);
        lst = st[0].h;
        st[0].h = 1;
        for(i = 1; i < num; i++){
            if(st[i].h == lst){
                st[i].h = st[i - 1].h;
            }
            else{
                lst = st[i].h;
                st[i].h = i + 1;
            }
        }
        for(i = 0; i < num; i++){
            update(i + 1, arr[i], m1);
            update(i + 1, 1, m2);
        }
        for(i = 0; i < num; i++){
            tmp = lower_bound(arr, arr + num, st[i].x) - arr;
            s = t = tmp;
            while(arr[s] >= st[i].x && s >= 0) s--;
            while(arr[t] <= st[i].x && t < num) t++;
            s++;
            res += st[i].h * (get(s, m2) * st[i].x - get(s, m1) + get(num, m1) - get(t, m1) - (get(num, m2) - get(t, m2)) * st[i].x);
            update(tmp + 1, -st[i].x, m1);
            update(tmp + 1, -1, m2);
        }
        printf("%lld\n", res);
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/TJU_Tahara/article/details/75905476
今日推荐