POJ 2481 Cows(线段树[单点更新])

Cows
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 0 Accepted: 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: 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个区间,对于每个区间,问有多少个区间覆盖此区间,不包括重叠

解题思路:在求解此问题之前,我们可以将区间排个序,优先按照右端点从大到小排,若右端点相同则按照左端点从小到大排

这样排序的好处是,对于当前区间,能覆盖它的区间都已经对线段树进行更新过了,而之所以对线段树只需要单点更新是因为按照排序顺序,先判断的必定是右端点值大的,我们只需判断比当前区间右端点小的区间有多少个就是覆盖当前区间的区间个数

需要另外处理的是重叠的情况,对于此种情况,我们无需再对线段树进行查询,直接复制前一个区间的解就可以了

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 100005;
const int inf = 100000000;
const int mod = 2009;
void get_val(int &ret)
{
    int sgn=1;
    char c;
    while(((c=getchar())<'0'||c>'9')&&c!='-');
    if(c=='-')
        sgn=-1,ret=0;
    else
        ret=c-'0';
    while((c=getchar())>='0'&&c<='9')
        ret=ret*10+c-'0';
    ret*=sgn;
}
int ans[N],sum[3*N];
void update(int l,int r,int p,int x)
{
    if(l==r)
    {
        sum[p]++;
        return ;
    }
    int mid=(l+r)/2;
    if(x>mid)
        update(mid+1,r,p*2+1,x);
    else
        update(l,mid,p*2,x);
    sum[p]=sum[p*2]+sum[p*2+1];
}
int query(int l,int r,int p,int L,int R)
{

    if(L<=l&&r<=R)
        return sum[p];
    int mid=(l+r)/2;
    if(L>mid)
        return query(mid+1,r,p*2+1,L,R);
    else if(R<=mid)
        return query(l,mid,p*2,L,R);
    else
        return query(l,mid,p*2,L,mid)+query(mid+1,r,p*2+1,mid+1,R);
}
struct cow
{
    int x,y,id;
}w[N];
bool cmp(cow x,cow y)
{
    if(x.y!=y.y)
        return x.y>y.y;
    return x.x<y.x;
}
int main()
{
    int n,i;
    while(scanf("%d",&n)&&n)
    {
        memset(sum,0,sizeof(sum));
        for(i=0;i<n;i++)
        {
            get_val(w[i].x),get_val(w[i].y);//快速读入,此题可以不使用,用scanf("%d%d",&w[i].x,&w[i].y);替换
            w[i].id=i;
        }
        sort(w,w+n,cmp);
        for(i=0;i<n;i++)
        {
            if(i&&w[i].x==w[i-1].x&&w[i].y==w[i-1].y)
                ans[w[i].id]=ans[w[i-1].id];
            else
                ans[w[i].id]=query(0,100000,1,0,w[i].x);
            update(0,100000,1,w[i].x);
        }
        for(i=0;i<n;i++)
            printf("%d%c",ans[i],i==n-1?'\n':' ');
    }
    return 0;
}
菜鸟成长记

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值