码迷,mamicode.com
首页 > 编程语言 > 详细

POJ-2481 Cows(树状数组)

时间:2016-08-12 13:05:58      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

Cows
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 16693   Accepted: 5583

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.

Source

题目大意就是要你求出每个集合是几个集合的真子集

然而我一开始以为要求每个集合有几个真子集,所以想了良久没搞定……此题类似Stars,只不过内题是已经排好序的,这题不是。

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <queue>
 6 #include <stack>
 7 #include <vector>
 8 #include <iostream>
 9 #include "algorithm"
10 using namespace std;
11 typedef long long LL;
12 const int MAX=100005;
13 int n;
14 int sum[MAX];
15 int ans[MAX];
16 struct Seg{
17     int a,b,c;
18 }seg[MAX];
19 void add(int x,int y){
20     for (;x<=n;sum[x]+=y,x+=(x&-x));
21 }
22 int search(int x){
23     int an(0);
24     for (;x>0;an+=sum[x],x-=(x&-x));
25     return an;
26 }
27 bool cmp(Seg x,Seg y){
28     if (x.b!=y.b)
29      return x.b>y.b;
30     else
31      return x.a<y.a;
32 }
33 int main(){
34     freopen ("cows.in","r",stdin);
35     freopen ("cows.out","w",stdout);
36     int i,j;
37     while (1)
38     {scanf("%d",&n);
39      if (n==0)
40       break;
41      for (i=1;i<=n;i++)
42      {scanf("%d%d",&seg[i].a,&seg[i].b);
43       seg[i].c=i;
44      }
45      sort(seg+1,seg+n+1,cmp);
46      memset(sum,0,sizeof(sum));
47      memset(ans,0,sizeof(ans));
48      for (i=1;i<=n;i++)
49      {if (i!=1 && seg[i].a==seg[i-1].a && seg[i].b==seg[i-1].b)
50        ans[seg[i].c]=ans[seg[i-1].c];
51       else
52        ans[seg[i].c]=search(seg[i].a+1);
53       add(seg[i].a+1,1);
54      }
55      for (i=1;i<n;i++)
56       printf("%d ",ans[i]);
57      printf("%d\n",ans[n]);
58     }
59     return 0;
60 }

 

POJ-2481 Cows(树状数组)

标签:

原文地址:http://www.cnblogs.com/Michaelzzn/p/5764272.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!