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 }

时间: 2024-08-07 13:53:25

POJ 2481Cows(树状数组 + 好题)的相关文章

poj 2299 树状数组求逆序数+离散化

http://poj.org/problem?id=2299 最初做离散化的时候没太确定但是写完发现对的---因为后缀数组学的时候,,这种思维习惯了吧 1.初始化as[i]=i:对as数组按照num[]的大小间接排序 2.bs[as[i]]=i:现在bs数组就是num[]数组的离散化后的结果 3.注意,树状数组中lowbit(i)  i是不可以为0的,0&(-0)=0,死循环... #include <cstdio> #include <cstring> #include

poj 2352 树状数组

Description Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. As

HDU1166 敌兵布阵 树状数组水题

中文题目,很简单的题目,区间求和,当然对于线段树来说也很水,为了练习一下树状数组,多做做水题吧,加深理解,并且打好基础,我算是被没打好基础给吓坏了,宁可多花几个小时 刷刷水题扎实点, 很裸的题目 操作也很裸,了解树状数组的肯定能做 #include<iostream> #include<cstdio> #include<list> #include<algorithm> #include<cstring> #include<string&g

POJ 3928 Ping pong 树状数组模板题

开始用瓜神说的方法撸了一发线段树,早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #includ

POJ 2481 树状数组 区间覆盖(POJ2352 Stars 的变形题)(线段化点)

0)学会将题目情景转化为自己熟悉的结构或模型. 题目大意: 每个奶牛有自己的一个区间,求每个奶牛的区间所覆盖的子区间个数(注意,真子集,相等的不算),按照输入的顺序输出. 转化: 要学会将题目情景转化为自己熟悉的模型或结构上.把每个区间的左端x值作为点的x坐标,右端x值作为点的y坐标,就可以把所有区间转化为一个二维坐标图上的点集,而此时每个点左上方的点(同Stars那道题目一样不包括自身)的个数,就是每个区间所覆盖的子区间的个数(对应题目要求,这里或许可以再变形). 同POJ2481 Stars

SYSU-5, POJ 2131, 树状数组+二分

题目大意:给出n个人,顺序对位置进行请求,如果第i个人请求的位置上有人,则让这个人顺延,如果顺延的位置继续有人,递归进行,问最后所有人的位置. 解:这题貌似可以用平衡树+并查集搞定,但是我队友强烈安利树状数组的做法.赛场上没出,赛后结合discuz想了一下,作一下处理. 首先如果是一个请求第a[i]个有空位置的问题,那么这个问题显然可以用树状数组维护前缀和即可.所以我们现在考虑将原问题转化成这个问题. 考虑终态,把没有人的位置去掉,剩下的n个座位排在一起,显然转化成上面模型的形式 第i个询问时,

POJ 2352Stars 树状数组

Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42898   Accepted: 18664 Description Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a st

【不可能的任务1/200】bzoj1103树状数组水题

(卧槽,居然规定了修改的两点直接相连,亏我想半天) 非常水的题,用dfs序(而且不用重复,应该是直接规模为n的dfs序)+树状数组可以轻松水 收获:树状数组一遍A(没啥好骄傲的,那么简单的东西) 1 #include <cstdio> 2 #include <iostream> 3 using namespace std; 4 int N=0,n,m,p,q,a[300000],l[300000],pos[300000],end[300000],son[300000],bro[30

POJ 1201 树状数组

链接: http://poj.org/problem?id=1201 题意: 给你n个区间,每个区间为[a,b],每个区间取c个数构成一个集合,求集合最小容量 题解: 把区间按b排序,从第一个区间开始取,从后往前取,这样尽可能和后面的区间重复 另外如果我们发现当前区间取得个数已经超过了c,那么只需要让之前区间换就行,而总数是不变的,所以不用更新答案 求当前区间已经取了多少个数用树状数组 代码: 1 #include <map> 2 #include <set> 3 #include

POJ 3321 树状数组(+dfs+重新建树)

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27092   Accepted: 8033 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been