POJ 2481 Cows
Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
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!
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.
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
解题思路:
题意:每一头牛都有一个测验值(S,E),对于牛i和牛j来说,如果它们的测验值满足下面的条件则
表示牛i比牛j强壮:Si <= Sjand Ei>= Ej,且“=”不能同时成立,问对于每头牛有多少牛比它强壮?
1,对于这种二维属性的区间求个数问题,如果其属性满足简单的大小关系,我们可以先对一维属性进行排序,
然后按照该顺序,在另一维上进行计数。
2,就这题而言,如果我们先按S属性的小大顺序排序,那么后插入线段树/树状数组的牛只受到先插入牛的影响。
然后我们分别用叶子代表E的值,维护区间牛的数量,那我们每次在[Ei,10^5]范围内查找当前牛的个数即可。
两个注意点:
1. 在按S属性排序的时候,如果两者S值相等,那我们优先把E值大的放在前面。
2. 在查询区间和的时候需要判断在它之前是否有和它属性一模一样的牛,统计的时候把之前的答案复制给他即可。
我用树状数组做的,其实核心思想就是用线段树/树状数组进行快速统计
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define LL long long
const int maxn = 100005;
struct node{
int id ;
int s ;
int e ;
int cnt ;
//int op ;
};
bool cmp(node a,node b){
if(a.s==b.s)return a.e>b.e ;
else return a.s<b.s ;
}
bool cmp2(node a,node b){
return a.id<b.id ;
}
node cow[maxn] ;
int C[maxn<<1] ;
int n ;
int nn = maxn ;
int lowbit(int x){return x&-x;}
int sum(int x){
int ret = 0 ;
while(x>0){
ret+=C[x] ;
x-=lowbit(x);
}
return ret ;
}
void add(int x,int d){
while(x<=nn){
C[x]+=d ;
x+=lowbit(x) ;
}
}
int main(){
// freopen("in.txt","r",stdin) ;
while(~scanf("%d",&n),n){
memset(cow,0,sizeof(cow)) ;
memset(C,0,sizeof(C)) ;
for(int i=1;i<=n;i++){
scanf("%d%d",&cow[i].s,&cow[i].e) ;
cow[i].e++ ;
cow[i].s++ ;
cow[i].cnt = 0 ;
cow[i].id = i ;
//cow[i].op = 0 ;
}
sort(cow+1,cow+n+1,cmp) ;
// for(int i=1;i<=n;i++){
// printf("i = %d %d %d\n",cow[i].s,cow[i].e,cow[i].cnt) ;
// }
for(int i=1;i<=n;i++){
//cow[i].op=1 ;
if(i>=2&&cow[i].s==cow[i-1].s&&cow[i].e==cow[i-1].e){
cow[i].cnt = cow[i-1].cnt ;
add(cow[i].e,1) ;
continue ;
}
//cow[i].cnt = sum(n)-sum(cow[i].e-1>=0?cow[i].e-1:0) ;
//printf("%d %d\n",sum(nn),(cow[i].e-1==0)?0:sum(cow[i].e-1)) ;
cow[i].cnt = sum(nn)-((cow[i].e-1==0)?0:sum(cow[i].e-1));
//printf("i = %d cnt = %d\n",i,cow[i].cnt) ;
add(cow[i].e,1) ;
}
sort(cow+1,cow+n+1,cmp2);
// for(int i=1;i<=n;i++){
// printf("i = %d %d %d\n",cow[i].s,cow[i].e,cow[i].cnt) ;
// }
for(int i=1;i<n;i++){
printf("%d ",cow[i].cnt) ;
}printf("%d\n",cow[n].cnt);
}
return 0;
}