B - Disharmony Trees HDU - 3015

B - Disharmony Trees HDU - 3015 

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. 

InputThere 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.OutputFor 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



题意:对于n棵树,给出所在位置和高度,然后分别对它的位置和高度做如下处理:

位置:将位置升序排序,最小的定义等级为 1,次小的定义等级为2,但是,要是位置相同的,则等级定义要相同;

例如:位置 1,2,1,5,2,3

        等级  1,3,1,6,3,5

对于高度也是做如上处理;

然后,定义f=两树之间的距离差的绝对值,s=两树中最小的高度,求所有树之间f*s和。

题解:树状数组。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdlib>
#include <vector>
#include<queue>
using namespace std;

#define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
const int maxn =  2e5+5;
const int  mod = 1e9+7;

struct node
{
    ll x,h;
}T[100005];
int n;

bool cmp(node s1,node s2)
{
    return s1.x<s2.x;
}
bool cmp1(node s1,node s2)
{
    return s1.h<s2.h;
}
int lowbit(int x)
{
    return x&(-x);
}
void add(int i,int val,int *s)
{
    while(i<=n)
    {
        s[i] += val;
        i += lowbit(i);
    }
}
int sum(int i,int *s)
{
    int res=0;
    while(i>0)
    {
        res += s[i];
        i -= lowbit(i);
    }
    return res;
}

int main()
{
    while(~scanf("%d",&n))
    {
        int c[100005],c1[100005];
        for (int i = 1; i <= n; i++)
            scanf("%lld %lld", &T[i].x, &T[i].h);
        sort(T + 1, T + n + 1, cmp);
        int xx = T[1].x;
        T[1].x = 1;
        for (int i = 2; i <= n; i++) //将T[i].x按照从小到大排序
        {
            if (T[i].x == xx)
                T[i].x = T[i - 1].x;
            else {
                xx = T[i].x;
                T[i].x = i;
            }
        }
        int xxmaxn = T[n].x;
        sort(T + 1,T + n +1,cmp1);
        xx = T[1].h;
        T[1].h = 1;
        for (int i = 2; i <= n; i++)   //将T[i].h按照从小到大排序
        {
            if (T[i].h == xx)
                T[i].h = T[i - 1].h;
            else {
                xx = T[i].h;
                T[i].h = i;
            }
        }
        ll ans = 0;
        sort(T+1,T+1+n,cmp1);
        memset(c,0,sizeof c);
        memset(c1,0,sizeof c1);
        for(int i=1;i<=n;i++)
        {
            add(T[i].x,T[i].x,c);   // 记录所有比这个数小的和,把每个等级的数放到对应的位置上
            add(T[i].x,1,c1);       //记录所有比这个数小的个数 每个点上记为1
        }
        int xiao,da;                //xiao 表示比对应的数a小,反之亦然
        for(int i = 1;i<n;i++)
        {
            xiao = sum(T[i].x-1,c1) * T[i].x - sum(T[i].x-1,c);     //找出比这个数小的个数*这个数-比这个数小的所有数之和
            da = (sum(xxmaxn,c) - sum(T[i].x,c)) - (sum(xxmaxn,c1) - sum(T[i].x,c1)) * T[i].x;      //找出比这个数大的数和-这个数*比这个数大的个数
            ans += (xiao + da) * T[i].h;
            add(T[i].x,-T[i].x,c);      //把这个用过的数删除
            add(T[i].x,-1,c1);          //把这个数位置上减去1
        }
        printf("%lld\n",ans);
    }

}
View Code

猜你喜欢

转载自www.cnblogs.com/smallhester/p/10300559.html