poj 2481 Cows 线段树

题目

Cows

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 19518 Accepted: 6625
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.


题意

有N头牛,每只牛有一个测试值[S,E],如果对于牛i和牛j来说,它们的测验值满足下面的条件则证明牛i比牛j强壮:Si <=Sjand Ej <= Ei and Ei - Si > Ej - Sj。现在已知每一头牛的测验值,要求输出每头牛有几头牛比其强壮。


N、E和S值范围是[0,1e5],


思路


分析题意可知,如果牛i的E值大于等于牛j、S值小于等于牛J,并且E和S值不能同时相当,说明牛i强于牛j。


很明显的思路,根据E值排序(我喜欢由小到大,如果E值相等,那么S由大到小),线段树里面存储元素下标为S值,存储的内容为当前值的个数。

将元素按序一个一个插入线段树,对于当前元素i,查询线段树中有多少元素S值大于等于i,现在的问题是如何去掉E和S值相同的情况。

只需在扫描时要用一个变量记录之前有多少个连续的元素E和S值与元素i的E和S相等即可。


代码

一年以前

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<iomanip>
#define all(x) (x).begin(), (x).end()
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define  lson   ind<<1,le,mid
#define rson    ind<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
using namespace std;
typedef long long ll;
const int INF =0x3f3f3f3f;
const int maxn=100000+5    ;
int n,ans[maxn+10];
struct P
{
    int x,y,ind;
    P(){}
    P(int x,int y,int ind):x(x),y(y),ind(ind){}
    bool operator<(const P c)const
    {
        if(x!=c.x) return x<c.x;
        return y>c.y;
    }
}a[maxn+10];
struct SegmentTree
{
    int sum[(maxn+5)*4];
    void build(int ind,int le,int ri)
    {
        if(le==ri)
        {
            sum[ind]=0;
            return;
        }
        MID;
        build(lson);
        build(rson);
        sum[ind]=sum[ind<<1]+sum[ind<<1|1];
    }

    void update(int ind,int le,int ri,int pos)
    {
        if(le==ri)
        {
            sum[ind]++;
            return;
        }
        MID;
        if(pos<=mid) update(lson,pos);
        else update(rson,pos);
        sum[ind]=sum[ind<<1]+sum[ind<<1|1];
    }
    int query(int ind,int le,int ri,int x,int y)
    {
        if(x<=le&&ri<=y)
        {
            return sum[ind];
        }
        MID;
        int ret=0;
        if(x<=mid) ret+=query(lson,x,y);
        if(y>mid)  ret+=query(rson,x,y);
        return ret;


    }
}sgt;
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        int x,y;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&x,&y);
            a[i]=P(x,y,i);
        }
        sort(a+1,a+1+n);
        sgt.build(1,0,maxn);
        int px=-1,py=-1,cnt=0;
        for(int i=1;i<=n;i++)
        {
            int x=a[i].x;
            int y=a[i].y;
            int ind=a[i].ind;
            if(x==px&&y==py) cnt++;
            else cnt=0;

//            cout<<x<<" "<<y<<endl;
            ans[ind]=sgt.query( 1,0,maxn,y ,maxn)-cnt;
//            cout<<ind<<" "<<ans[ind]<<endl;
            sgt.update(1,0,maxn,y);
            px=x,py=y;
        }
        for1(i,n)
        {
            if(i!=1)  putchar(' ');
            printf("%d",ans[i]);
        }
        putchar('\n');
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值