poj 2481 Cows 树状数组

点击打开链接题目链接

Cows
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 13535 Accepted: 4486

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

题意: 两个区间:[Si, Ei] and [Sj, Ej].(0 <= S < E <= 105). 若 Si <= Sj and Ej <= Ei and Ei – Si > Ej – Sj, 则第i个区间覆盖第j个区间。给定N个区间(1 <= N <= 10^5),分别求出对于第i个区间,共有多少个区间能将它覆盖。
其实就是star的翻版  把区间[x,y]看成点(x,y)观察左上角有几个点
按y由大到小排序,再按x由小到大排序
代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 111111
using namespace std;
int bit[MAXN];
int lowbit(int x){
    return x&(-x);
}
void add(int x,int p){
    while(x<MAXN){
        bit[x]+=p;
        x+=lowbit(x);
    }
}
int getsum(int p){
    int cnt=0;
    while(p>0){
        cnt+=bit[p];
        p-=lowbit(p);
    }
    return cnt;
}
struct Node{
    int x,y,id,ans;
}node[MAXN];
bool cmp1(Node a,Node b){
    if(a.y==b.y)
        return a.x<b.x;
    return a.y>b.y;
}
bool cmp2(Node a,Node b){
    return a.id<b.id;
}
int main(){
    int n,x,y;
    while(scanf("%d",&n)!=EOF&&n){
        memset(bit,0,sizeof(bit));
        for(int i=1;i<=n;i++){
            scanf("%d %d",&x,&y);
            node[i].x=++x;
            node[i].y=++y;
            node[i].id=i;
            node[i].ans=0;
        }
        sort(node+1,node+n+1,cmp1);
        int fa=1;
        add(node[1].x,1);
        for(int i=2;i<=n;i++){
            if(node[i].x==node[fa].x&&node[i].y==node[fa].y){
                node[i].ans=node[fa].ans;
            }
            else {
                fa=i;
                node[i].ans=getsum(node[i].x);
            }
            add(node[i].x,1);
        }
        sort(node+1,node+1+n,cmp2);
        for(int i=1;i<=n;i++){
            if(i<n)
            printf("%d ",node[i].ans);
            else printf("%d\n",node[i].ans);
        }
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值