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

本文介绍了一种解决线段树区间覆盖问题的方法,通过按照终点从大到小排序,相同终点按起点从小到大排序的方式,实现了对于每个线段查询有多少个线段能完全覆盖它的需求。
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

 

转载于:https://www.cnblogs.com/zhaopAC/p/4977738.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值