POJ2481 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. 

Sample Input

3
1 2
0 3
3 4
0

Sample Output

1 0 0

Hint

Huge input and output,scanf and printf is recommended.

PS:可能很多人刚看到题,不知道如何下手,其实我也是。先说下题意吧,其实就是输入一些线段,问能找到这条线段最多包含几条线段If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj。这样问题就很简单了,我们可以按照后节点从大到小排序,如果后节点相等,前节点从小到大排序。这样排完序后,我们只需要找前面比前节点小的节点的个数就行了。

AC代码:

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<cmath>
#include<set>
#include<vector>
const int maxn=1e5+10;
const int mod=1e9+7;
#define me(a,b) memset(a,b,sizeof(a))
typedef long long ll;
using namespace std;
int n,bit[maxn];
struct node
{
    int s,e,i;
    bool friend operator<(node a,node b)
    {
        if(a.e==b.e)
            return a.s<b.s;
        return a.e>b.e;
    }
}a[maxn];
int lowbit(int x)
{
    return x&(-x);
}
void updata(int x)
{
    while(x<=n)
    {
        bit[x]++;
        x+=lowbit(x);
    }
}
int getsum(int x)
{
    int sum=0;
    while(x)
    {
        sum+=bit[x];
        x-=lowbit(x);
    }
    return sum;
}
int main()
{
    while(cin>>n&&n)
    {
        int s[maxn];me(bit,0);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&a[i].s,&a[i].e);
            a[i].i=i;a[i].s++;///因为s可能会为0,所以这里都加一
        }
        sort(a+1,a+n+1);
        for(int i=1;i<=n;i++)
        {
            if(a[i].s==a[i-1].s&&a[i].e==a[i-1].e)///若两区间相等,该值等于上一个的值
                s[a[i].i]=s[a[i-1].i];
            else
                s[a[i].i]=getsum(a[i].s);///更新节点信息
            updata(a[i].s);
        }
        for(int i=1;i<=n;i++)
	     {
	     	if(i!=n)
	     		printf("%d ",s[i]);
	     	else
	     		printf("%d\n",s[i]);
	     }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41292370/article/details/81297652
今日推荐