hdoj 3015 Disharmony Trees(树状数组)

思路和poj1990一样,开两个树状数组,分别维护左右的数量和距离和。


注意要使用long long。


代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
ll tree_num[maxn], tree_dis[maxn];

struct node
{
    int x, h, rx, rh;
}a[maxn];

bool cmp(const node &x, const node &y)
{
    return x.rh > y.rh;
}

bool cmp1(const node &x, const node y)
{
    return x.x < y.x;
}

bool cmp2(const node &x, const node &y)
{
    return x.h < y.h;
}

int lowbit(int x) { return x&(-x); }

void update(ll *tree, int pos, int val)
{
    while(pos < maxn)
    {
        tree[pos] += val;
        pos += lowbit(pos);
    }
}

ll query(ll *tree, int pos)
{
    ll sum = 0;
    while(pos)
    {
        sum += tree[pos];
        pos -= lowbit(pos);
    }
    return sum;
}

int main(void)
{
    int n, k;
    while(cin >> n)
    {
        memset(tree_num, 0, sizeof(tree_num));
        memset(tree_dis, 0, sizeof(tree_dis));
        for(int i = 0; i < n; i++)
            scanf("%d%d", &a[i].x, &a[i].h);
        //
        sort(a, a+n, cmp1);
        k = 2;
        a[0].rx = 1;
        for(int i = 1; i < n; i++, k++)
            if(a[i].x == a[i-1].x) a[i].rx = a[i-1].rx;
            else a[i].rx = k;
        //
        sort(a, a+n, cmp2);
        a[0].rh = 1;
        k = 2;
        for(int i = 1; i < n; i++, k++)
            if(a[i].h == a[i-1].h) a[i].rh = a[i-1].rh;
            else a[i].rh = k;
        //
        sort(a, a+n, cmp);
        ll ans = 0;
        for(int i = 0; i < n; i++)
        {
            ll lnum = query(tree_num, a[i].rx-1);
            ll rnum = i-query(tree_num, a[i].rx);
            ll lsum = query(tree_dis, a[i].rx-1);
            ll rsum = query(tree_dis, maxn-1)-query(tree_dis, a[i].rx);
            ans += a[i].rh*((lnum*a[i].rx-lsum)+(rsum-rnum*a[i].rx));
            update(tree_num, a[i].rx, 1);
            update(tree_dis, a[i].rx, a[i].rx);
        }
        printf("%lld\n", ans);
    }
    return 0;
}


Disharmony Trees

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


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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值