poj2481 Cows(树状数组)

Cows

Description

Farmer John’s cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in his field is particularly good.

Farmer John has N cows (we number the cows from 1 to N). Each of Farmer John’s N cows has a range of clover that she particularly likes (these ranges might overlap). The ranges are defined by a closed interval [S,E].

But some cows are strong and some are weak. Given two cows: cowi and cowj, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cowi is stronger than cowj.

For each cow, how many cows are stronger than her? Farmer John needs your help!
Input

The input contains multiple test cases.
For each test case, the first line is an integer N (1 <= N <= 105), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 105) specifying the start end location respectively of a range preferred by some cow. Locations are given as distance from the start of the ridge.

The end of the input contains a single 0.
Output

For each test case, output one line containing n space-separated integers, the i-th of which specifying the number of cows that are stronger than cowi.

题意:给出若干个区间[Si,Ei],定义一个区间比另一个区间“strong”当且仅当Si<=Sj and Ei>=Ej and Ei-Si>Ej-Sj。输出对于每一个区间,有多少个区间比它strong。区间最多100000个,区间坐标不超过100000。

分析:树状数组离散化,按照题目要求构造,排序方法为:以y坐标(e)从大到小排序,相同y以x从小到大排序,利用树状数组统计比当前cow猛的牛,并把当前牛加入树状数组。注意牛的s和e都相等的时候需要特殊处理。离散化应该注意使得一维排序,另一维能够从小到大计算(树状数组特性)。

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#define max 200000
using namespace std;

struct edge
{
    int x,y,z;
}a[max];
int c[max],ans[max],n;

int cmp(edge p,edge q)
{
    if (p.y!=q.y) return p.y>q.y;
    return p.x<q.x;
}

void insert(int x)
{
    while (x<=100005)
    {
        c[x]++;
        x+=x&(-x);
    }
}

int sum(int x)
{
    int s=0;
    while (x>0)
    {
        s+=c[x];
        x-=x&(-x);
    }
    return s;
}

int main()
{
    while(scanf("%d",&n)&&n!=0)
    {
        for (int i=1;i<=n;i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
            a[i].z=i;
            a[i].x++;
            a[i].y++;
        }
        sort(a+1,a+n+1,cmp);
        memset(c,0,sizeof(c));
        memset(ans,0,sizeof(ans));
        ans[a[1].z]=sum(a[1].x);
        insert(a[1].x);
        for (int i=2;i<=n;i++)
        if (a[i].x==a[i-1].x&&a[i].y==a[i-1].y)
        {
            ans[a[i].z]=ans[a[i-1].z];
            insert(a[i].x);
        }
        else
        {
            ans[a[i].z]=sum(a[i].x);
            insert(a[i].x);
        }
        for (int i=1;i<=n;i++)
            printf("%d ",ans[i]);
        printf("\n");
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值