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

POJ 2481Cows(树状数组 + 好题)

时间:2015-11-19 16:24:42      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

Cows
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 15222   Accepted: 5070

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.
 
题意:线段起点s,终点e,问每一个线段包含在几个线段里面,样例中线段[1,2]包含在[0,3]里面所以第一个输出是1,
思路:按照e按照从大到小排序,e相同按照s从小到大排序。因为只有e从大到小排序后,s就可以从小到大更新了,
技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <map>
 5 #include <algorithm>
 6 #include <string.h>
 7 using namespace std;
 8 const int MAX = 100000 + 10;
 9 struct node
10 {
11     int s,e;
12     int index;
13 };
14 node a[MAX];
15 int c[MAX],cnt[MAX];
16 int cmp(node x,node y)
17 {
18     if(x.e == y.e)
19         return x.s < y.s;
20     return x.e > y.e;
21 }
22 int lowbit(int k)
23 {
24     return k & (-k);
25 }
26 void add (int k,int num)
27 {
28     for(int i = k; i < MAX; i += lowbit(i))
29         c[i] += num;
30 }
31 int sum(int k)
32 {
33     int s = 0;
34     for(int i = k; i > 0; i -= lowbit(i))
35         s += c[i];
36     return s;
37 }
38 int main()
39 {
40     int n;
41     while(scanf("%d", &n) != EOF && n)
42     {
43         for(int i = 0; i < n; i++)
44         {
45             scanf("%d%d", &a[i].s, &a[i].e);
46             a[i].index = i;
47         }
48         sort(a,a + n,cmp);
49         memset(c,0,sizeof(c));
50         memset(cnt,0,sizeof(cnt));
51         cnt[a[0].index] = 0;
52         add (a[0].s + 1, 1);
53         for(int i = 1; i < n; i++)
54         {
55             if(a[i].s == a[i - 1].s && a[i].e == a[i - 1].e)
56                 cnt[a[i].index] = cnt[a[i - 1].index];
57             else
58                 cnt[a[i].index] = sum(a[i].s + 1);
59             add(a[i].s + 1,1);
60         }
61         printf("%d",cnt[0]);
62         for(int i = 1; i < n; i++)
63             printf(" %d",cnt[i]);
64         printf("\n");
65     }
66 
67     return 0;
68 }
View Code

 

POJ 2481Cows(树状数组 + 好题)

标签:

原文地址:http://www.cnblogs.com/zhaopAC/p/4977738.html

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