首页 > 其他 > 详细

POJ 2481 Cows(树状数组)

时间:2014-08-20 14:10:52      阅读:411      评论:0      收藏:0      [点我收藏+]

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

Sample Input

3
1 2
0 3
3 4
0

Sample Output

1 0 0


树状数组的基础题:输入的s,e要做加1处理

策略:对y升序排序,从后往前遍历和递推。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
const int maxn=1e5+100;
struct node{
    int pos;
    int x;
    int y;
}p[maxn];
int f[maxn],a[maxn],n,m;
int lowbit(int k)
{
    return k&(-k);
}
void update(int num,int k)
{
    while(num<=m)
    {
        a[num]+=k;
        num+=lowbit(num);
    }
}
int sum(int num)
{
    int ans=0;
    if(num<=0)
        return 0;
    while(num)
    {
        ans+=a[num];
        num-=lowbit(num);
    }
    return ans;
}
int cmp(node l1,node l2)
{
    if(l1.y==l2.y)
        return l1.x>l2.x;
    return l1.y<l2.y;
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        m=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&p[i].x,&p[i].y);
            p[i].x++;
            p[i].y++;
            if(p[i].x>m)
                m=p[i].x;
            p[i].pos=i;
        }
        sort(p+1,p+n+1,cmp);//对于e按升序排序
        memset(f,0,sizeof(f));
        memset(a,0,sizeof(a));
        for(int i=n;i>=1;i--)
        {
            if(i!=n&&p[i].y==p[i+1].y&&p[i].x==p[i+1].x)//此处注意相等的情况
                f[p[i].pos]=f[p[i+1].pos];
            else
                f[p[i].pos]=sum(p[i].x);
            update(p[i].x,1);
        }
        for(int i=1;i<=n;i++)
            printf(i==n?"%d\n":"%d ",f[i]);
    }
    return 0;
}


POJ 2481 Cows(树状数组),布布扣,bubuko.com

POJ 2481 Cows(树状数组)

原文:http://blog.csdn.net/u013582254/article/details/38704685

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!