POJ 2481 Cows(树状数组)

本文介绍了一种解决区间覆盖问题的方法,通过树状数组来快速统计区间覆盖的数量。该问题来源于Farmer John的奶牛们对特定范围的优质三叶草的喜爱,需要计算每头奶牛喜爱的范围内有多少其他奶牛的范围能够完全覆盖它。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                                                  Cows
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 17626 Accepted: 5940

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
【分析】给你n个区间,问你对于每个区间,有多少个区间是完全覆盖它的。完全覆盖的意思是若Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj,那么j就被i完全覆盖。
n<=1e5,所以暴力肯定超时,而树状数组正好可以用于快速的统计个数。首先得排个序,然后模板统计。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 2e9
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = 1e5+5;
const int M = 24005;
int n,m;
ll tree[N],ans[N];
struct man{
    int s,e,no;
    bool operator< (const man &it)const{
        if(e==it.e)return s<it.s;
        return e>it.e;
    }
}a[N];
void add(int k,int num){
    while(k<=1e5+1){
        tree[k]+=num;
        //printf("####%lld\n",tree[k]);
        k+=k&(-k);
    }
}
ll Sum(int k){
    ll sum=0;
    while(k>0){
        sum+=tree[k];
        k-=k&(-k);
    }
    return sum;
}
int main() {
    while(~scanf("%d",&n)&&n){
        met(tree,0);met(ans,0);
        for(int i=1;i<=n;i++){
            scanf("%d%d",&a[i].s,&a[i].e);
            a[i].s++;a[i].e++;a[i].no=i;
        }
        sort(a+1,a+1+n);
        for(int i=1;i<=n;i++){
            ll Ans;
            if(a[i].s==a[i-1].s&&a[i].e==a[i-1].e)Ans=ans[a[i-1].no];
            else Ans=Sum(a[i].s);
            //printf("%d\n",Ans);
            ans[a[i].no]=Ans;
            add(a[i].s,1);
        }
        printf("%lld",ans[1]);
        for(int i=2;i<=n;i++){
            printf(" %lld",ans[i]);
        }
        printf("\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/jianrenfang/p/6088723.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值