首页 > 其他 > 详细

poj 2481 Cows

时间:2014-08-03 23:02:16      阅读:377      评论:0      收藏:0      [点我收藏+]
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 }

 

poj 2481 Cows,布布扣,bubuko.com

poj 2481 Cows

原文:http://www.cnblogs.com/ainixu1314/p/3888917.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!