Cows(树状数组)

Cows

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 19673   Accepted: 6699

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来找就可以。

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdio>
 5 #define N 100005
 6 #define mem(a) memset(a,0,sizeof(a))
 7 using namespace std;
 8 struct Node{
 9     int x,y,id;
10 };
11 int val[N];
12 int lowbit(int a){
13     return a&(-a);
14 }
15 int tree[N];
16 Node node[N];
17
18 void update(int x){
19     while(x<=N){
20         tree[x]++;
21         x+=lowbit(x);
22     }
23 }
24
25 bool cmp(Node a,Node b){
26     if(a.y==b.y)
27         return a.x<b.x;
28     return a.y>b.y;
29 }
30 int query(int x){
31     int sum=0;
32     while(x>0){
33         sum+=tree[x];
34         x-=lowbit(x);
35     }
36     return sum;
37 }
38//void test(){
39//   cout<<"***"<<endl;
40// }
41 int main() {
42     int n;
43     while(scanf("%d",&n)&&n){
44         mem(tree);
45        // mem(node);
46         mem(val);
47         for(int i=0;i<n;i++){
48             scanf("%d%d",&node[i].x,&node[i].y);
49             node[i].id=i;
50             node[i].x++;
51             node[i].y++;
52         }
53         sort(node,node+n,cmp);
54         val[node[0].id]=query(node[0].x);
55         update(node[0].x);
56
57         for(int i=1;i<n;i++){
58             if(node[i].x==node[i-1].x&&node[i].y==node[i-1].y){
59                 val[node[i].id]=val[node[i-1].id];
60             }else{
61                 val[node[i].id]=query(node[i].x);
62             }update(node[i].x);
63         }
64
65        // test();
66         for(int i=0;i<n;i++){
67             if(i!=n-1)
68                 printf("%d ",val[i]);
69             else
70                 printf("%d\n",val[i]);
71         }
72     }
73     return 0;
74 }
时间: 2024-10-24 18:51:42

Cows(树状数组)的相关文章

POJ 2481 Cows(树状数组)

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). Ea

POJ2481 Cows 树状数组的简单应用

题意给了你N头牛,每头牛的强壮值是一个区间[s,e],如果第 i 头牛比第 j 头牛强壮那么必须满足 Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj: 为了满足这三个条件我们进行排序,先以e降序排为先决条件,若e相等则让s升序排列,如此即可满足三个条件,这道题目任意两头牛的强壮值区间有可能完全一样,这样就不需要重新用树状数组求一次和了,直接赋值即可,这样可以省很多时间,因为已经排序处理了,所以即便区间相等的  肯定就是相邻的,所以直接扫一遍即可,若

POJ2481:Cows(树状数组)

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). Ea

POJ 2182 Lost Cows (树状数组)

Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9660   Accepted: 6219 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, they visited the neighborhood 'waterin

poj 2481 Cows 树状数组解法,详细解析。

Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13445   Accepted: 4448 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 hi

soj 1088. Cows(树状数组)

可以用树状数组解决. 先按左端点递增排序,左端点相等的按右端点降序排列. 然后从左往有扫,更新答案同时更新sum数组. 对于一只Cow i,ans[i]为f(i)-g(i). f(i)为满足p[j].s<=p[i].s&&p[j].e>=p[i].e(0<=j<n,j!=i)的Cow只数. g(i)为满足p[j].s==p[i].s&&p[j].e==p[i].e(0<=j<n,j!=i)的Cow只数. 开个d数组维护一下,d[i]即为g

POJ 2481 COWS(树状数组)

题目大意: 说给你n个线段的,告诉你每个线段的起始点S_i,和终止点E_i, 问这n条线段里有多少线段是相互包含的,如果两个端点重合不算包含. 解题思路: 用树状数组搞就可以了,这道题是star的变形题,star是让我们求出有多个星星在这个星星的左下角,而这道题是让我们求出有多少个星星在这个星星的左上角. 这样分析,对于(S_i,E_i)我们把他封装成为一个点的坐标,那么(S_j,E_j)同样也可以被封装成为一个点的坐标,而题目中说的S_i<S_j&&E_j<E_i&&

POJ 2182 Lost Cows (树状数组 &amp;&amp; 二分查找)

题意:给出数n, 代表有多少头牛, 这些牛的编号为1~n, 再给出含有n-1个数的序列, 每个序列的数 ai 代表前面还有多少头比 ai 编号要小的牛, 叫你根据上述信息还原出原始的牛的编号序列 分析:如果倒着看这个序列的话, 那序列的最后一个元素就能够确定一个编号.举个例子:如果序列的最后一个元素为0, 那就说明这头牛前面再也没有比它编号更小的牛了, 所以这头牛的编号肯定是最大的, 我们只要给它所在的编号加个标记, 然后继续根据倒数第二个.第三个--来依次确定便可还原整个序列, 这里可以使用树

poj2182Lost Cows——树状数组快速查找

题目:http://poj.org/problem?id=2182 从后往前确定,自己位置之前没有被确定的且比自己编号小的个数+1即为自己的编号: 利用树状数组快速查找,可另外开一个b数组,角标为编号大小,而其值为是否使用,二分查找到恰好满足条件的位置,向后一直找到没被用过的第一个编号即为此位置编号. 代码如下: #include<iostream> #include<cstdio> using namespace std; int n,a[8005],f[8005],ans[80