poj 2481 Cows

E - Cows
Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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
x 按升序,y按降序 sort一下
x1,y1
x2,y2
如果x1==x2,y1==y2的话,sum(x2,y2)=sum(x1,y1),不能用getnum()去求覆盖(x2,y2)的区间个数
1 2
1 2
1 2
用getnum()去求第三个(1,2)的话,会得到2,但是结果应该为0
 1 #include<iostream>
 2 #include<string>
 3 #include<cstdio>
 4 #include<vector>
 5 #include<queue>
 6 #include<stack>
 7 #include<algorithm>
 8 #include<cstring>
 9 #include<stdlib.h>
10 #include<string>
11 #include<cmath>
12 using namespace std;
13 #define pb push_back
14 int n,m,k,mmax;
15 int p[100100],check[100100];
16 struct node{
17     int x,y,id;
18 }num[101000];
19 int cmp(node a,node b){
20     if(a.x==b.x) return a.y>b.y;
21     return a.x<b.x;
22 }
23 void update(int pos){
24     while(pos>=1){
25         p[pos]+=1;
26         pos-=pos&(-pos);
27     }
28 }
29 int getnum(int pos){
30     int sum=0;
31     while(pos<=mmax){
32         sum+=p[pos];
33         pos+=pos&(-pos);
34     }
35     return sum;
36 }
37 int main(){
38     while(scanf("%d",&n),n){
39         memset(p,0,sizeof(p));
40         memset(check,0,sizeof(check));
41         mmax=0;
42         for(int i=1;i<=n;i++){
43              scanf("%d%d",&num[i].x,&num[i].y);
44              num[i].x++,num[i].y++;
45              num[i].id=i;
46              mmax=max(mmax,num[i].y);
47         }
48         sort(num+1,num+1+n,cmp);
49         check[num[1].id]=0;
50         update(num[1].y);
51         for(int i=2;i<=n;i++){
52             if(num[i].x==num[i-1].x&&num[i].y==num[i-1].y)
53                 check[num[i].id ]=check[num[i-1].id ];
54             else
55                 check[num[i].id ]=getnum(num[i].y);
56             update(num[i].y);
57         }
58         for(int i=1;i<=n;i++){
59             if(i!=1) printf(" ");
60             printf("%d",check[i]);
61         }
62         printf("\n");
63     }
64 }

 

posted on 2014-08-03 21:46  天凉了  阅读(123)  评论(0编辑  收藏  举报

导航