# poj2481 Cows

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.

本题和poj2352 Stars有很大的相似之处。都是用树状数组维护坐标。Stars是给定一系列的点（并按照纵坐标从小到大依次排好序），求每一个点左下角有多少个点。本题是给出无序的坐标然后求每一个点的左上角有多少个点。由于是左上角，可以先按照纵坐标的从大到小排序，然后依次求每个点左上角的点数目，更新每一个点。（利用每个点之前的点一定比当前点高）。

#include<iostream>
#include <stdio.h>
#include <memory.h>
#include <algorithm>
#define lowbit(i) (i&(-i))
using namespace std;
const int maxn=1e5+5;

int n,bit[maxn],ans[maxn];
struct node{
int x,y,id;			//(x,y)表示每一个点的坐标，id排序时用来记录每个点原来顺序
}a[maxn];

bool cmp(node a,node b){
return (a.y==b.y?a.x<b.x:a.y>b.y);   //按照纵坐标从高到低排序，相同时按照横坐标由小到大排序
}

while(i<=maxn){
bit[i]+=x;
i+=lowbit(i);
}
}

int query(int i){
int sum=0;
while(i>0){
sum+=bit[i];
i-=lowbit(i);
}
return sum;
}

int main(){
while(~scanf("%d",&n) && n){
memset(bit,0,sizeof(bit));
memset(ans,0,sizeof(ans));
for(int i=0;i<n;i++){
scanf("%d%d",&a[i].x,&a[i].y);
a[i].id=i;
a[i].x++;a[i].y++;		//树状数组下标从1开始，输入的点横纵坐标都++
}
sort(a,a+n,cmp);
ans[a[0].id]=0;
for(int i=1;i<n;i++){
if(a[i].x==a[i-1].x && a[i].y==a[i-1].y)	//若两个连续点重复，则当前点的值等于前一个点的值
ans[a[i].id]=ans[a[i-1].id];
else
ans[a[i].id]=query(a[i].x);		//否则看当前点之前（左边）的点（都比当前点高）的和（个数）
}
for(int i=0;i<n;i++){                   //按输入顺序输出每个点的值
if(i) printf(" ");
printf("%d",ans[i]);
}
cout<<endl;
}
return 0;
}

• 本文已收录于以下专栏：

## poj 2481 Cows 树状数组解法,详细解析。

Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can...
• Lionel_D
• 2015年02月05日 19:30
• 1148

## poj2481 Cows

Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ...
• Kirito_Acmer
• 2015年06月03日 19:21
• 591

## POJ2481 Cows

Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11946   Accepted...
• eeeaaaaa
• 2014年06月20日 18:42
• 228

## poj2481——Cows

• k1246195917
• 2011年04月30日 15:50
• 305

## POJ2481——Cows

Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ...
• Guard_Mine
• 2014年08月23日 20:27
• 365

## [树状数组]poj2481 Cows

poj2481 Cows, 题目链接 题意： 好吧又是熟悉的牛，一条河岸，每只牛都有吃草范围[S,E]，它们或强壮或虚弱。我们这样定义强壮：牛i和牛j，它们的吃草范围分别是[Si，Ei]和[S...
• soundwave_
• 2016年09月24日 13:07
• 110

## 【POJ2481】cows 线段树

• qq_25554407
• 2017年03月18日 19:07
• 94

## poj2481 Cows 树状数组 解题报告

Cows Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 15519 Accepted: 5182 ...
• Dafang_Xu
• 2016年03月16日 13:16
• 222

## poj2481 Cows（数状数组）

• hill_555
• 2013年04月27日 19:09
• 383

## poj2481 Cows (树状数组)

• u014325920
• 2016年05月05日 20:37
• 119

举报原因： 您举报文章：poj2481 Cows 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)