POJ 2481 线段树

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: cow i and cow j, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cow i is stronger than cow j.

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 <= 10 5), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 10 5) 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 cow i.

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.

题意:

题目依次求出n个奶牛中比他们强大的个数。给出判定标准  [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cow i is stronger than cow j.

这个判定标准有两个变量, S和E , 我们通过这两个变量无法通过线段树进行比较, 因此我们需要将一个变量产生的影响固定下来, 这里我们将S产生的影响固定下来,我们可以通过排序的方法确定。

 可应先将S从小到大排序,如果相等的话,那么就按E从大到小的顺序排序。这样排序以来,S的影响几乎就没有了,这样只需要判断E了。可以通过建立一个线段树来维护E的值。这样只需要对线段树统计就行了。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=100005;
int num[maxn];
int tree[maxn<<2];
struct cow
{
    int left,right;
    int no;
};
cow c[maxn];
int n;
int compare (cow a,cow b)
{
    if(a.left!=b.left)
       return a.left<b.left;
    else
       return a.right>b.right;
}
void pushup (int re)
{
    tree[re]=tree[re<<1]+tree[re<<1|1];
}
void build (int l,int r,int re)
{

    if(l==r)
    {
        tree[re]=0;
        return;
    }
    int mid=(l+r)>>1;
    build (l,mid,re<<1);
    build (mid+1,r,re<<1|1);
    pushup(re);
}
void  update(int l,int r,int re,int loc)
{
    if(l==r)
    {
        tree[re]++;
        return ;
    }
    int mid=(l+r)>>1;
    if(loc<=mid)
    update (l,mid,re<<1,loc);
    else
    update (mid+1,r,re<<1|1,loc);
    pushup (re);
}
int query (int l,int r,int re,int left,int right)
{
    if(l>=left&&r<=right)
        {
            return tree[re];
        }
    int ans=0;
    int mid=(l+r)>>1;
    if(mid>=left)
        ans+=query (l,mid,re<<1,left,right);
    if(mid<right)
        ans+=query (mid+1,r,re<<1|1,left,right);
    //printf(":::: %d\n",ans);
    return ans;
}
int main()
{
    while (scanf("%d",&n)!=EOF&&n)
    {
        memset (num,0,sizeof(num));
        for (int i=1;i<=n;i++)
        {
            scanf("%d%d",&c[i].left,&c[i].right);
            c[i].left++;
            c[i].right++;
            c[i].no=i;
        }
        sort (c+1,c+n+1,compare);
        //for (int i=1;i<=n;i++)
            //printf("%d %d %d\n",c[i].no,c[i].left,c[i].right);
        build (1,100002,1);
        for (int i=1;i<=n;i++)
        {
             if(i>1&&c[i].left==c[i-1].left&&c[i].right==c[i-1].right)
                    {
                        num[c[i].no]=num[c[i-1].no];
                        //printf("=====\n");
                    }
             else
                    num[c[i].no]=query (1,100002,1,c[i].right,100002);
             update (1,100002,1,c[i].right);
        }
        for (int i=1;i<=n;i++)
            if(i!=n)
            printf("%d ",num[i]);
            else
            printf("%d\n",num[i]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/81675219