poj 2481 Cows(树状数组)又是john和他的母牛那点不为人知的故事

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: cow i and cow j, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cow i is stronger than cow j.

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 <= 10 5), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 10 5) 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 cow i.

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.

题意:每次看到John就知道又是关于牛的。这次也不例外,John的牛要比美。第一行输入n,n==0结束,接着输入n组数据,每头牛都有两个参数s[]和e[]。如果满足Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj我们就说i牛比j牛牛逼,最后输出有多少牛比第i个牛牛逼。

思路:和星星那个题差不多。

ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int d1[1000000],maxn,d[1000000];                          //d1[]为树状数组,d[]存比当前牛强壮的牛的个数
struct node
{
    int g,h,y;
}t[1000000];
int cmp(struct node a,struct node b)                      //排序以g从大到小排序
{
    if(a.h==b.h)
    return a.g<b.g;
    return a.h>b.h;
}
int lowbit(int x)
{
    return x&(-x);
}
int sum(int x)
{
    int res=0;
    while(x>0)
    {
        res+=d1[x];
        x-=lowbit(x);
    }
    return res;
}
void add(int x)
{
    while(x<=maxn)
    {
        d1[x]++;
        x+=lowbit(x);
    }
}
int main()
{
    int a,b,i,j,n;
    while(scanf("%d",&n)&&n)
    {
        maxn=0;
        memset(d1,0,sizeof(d1));
        memset(d,0,sizeof(d));
        for(i=0;i<n;i++)
        {
            scanf("%d%d",&t[i].g,&t[i].h);
            t[i].y=i;
            t[i].g++,t[i].h++;                                    //避免0进入树状数组
            if(t[i].g>maxn)
            maxn=t[i].g;
        }
        sort(t,t+n,cmp);                                           //排完序后就和星星那道题像了
        d[t[0].y]=sum(t[0].g);
        add(t[0].g);
        for(i=1;i<n;i++)
        {
            if(t[i].g==t[i-1].g&&t[i].h==t[i-1].h)                 //如果当前点与前一个店相同,比他强壮的牛数就和去前面的牛一样
            d[t[i].y]=d[t[i-1].y];
            else
            d[t[i].y]=sum(t[i].g);
            add(t[i].g);
        }
        printf("%d",d[0]);                                         //注意输出格式
        for(i=1;i<n;i++)
        printf(" %d",d[i]);
        printf("\n");
    }
}
时间: 2024-08-28 16:15:46

poj 2481 Cows(树状数组)又是john和他的母牛那点不为人知的故事的相关文章

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

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

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 2309 BST 树状数组基本操作

Description Consider an infinite full binary search tree (see the figure below), the numbers in the nodes are 1, 2, 3, .... In a subtree whose root node is X, we can get the minimum number in this subtree by repeating going down the left node until t

Poj 2299 Ultra-QuickSort 树状数组 解法

本题的树状数组稍微有点特点,就是需要所谓的离散化一下,开始听这个名称好像很神秘的,不过其实很简单. 就是把一个数组arr的值,其中的值是不连续的,变成一组连续的值,因为这样他们的顺序是不变的,所以,不影响结果. 例如:9 1 0 5 4 ->变为:5 2 1 4 3看出他们的相对位置不变的. 9和5为最大值在第一个位置,1和2为第二大的值在第二个位置,0和1在第一个位置等,看出对应顺序了吗? 对,就是这么简单的方法, 就叫做离散化. 如果你对counting sort熟悉的话,那么这样的思想理解

POJ2481 Cows 树状数组的简单应用

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

POJ 2481 Cows &amp;&amp; POJ 2352 Stars(树状数组妙用)

题目链接:POJ 2481 Cows POJ 2352 Stars 发现这两个题目都跟求逆序数有着异曲同工之妙,通过向树状数组中插入点的位置,赋值为1,或者++,然后通过求和来判断比当前 点 "小" 的有多少点. Cows需要自己排序, Stars题目已经给排好序. POJ 2352 Stars 题目大意为在二维坐标上给出一些星星的坐标,求某一个星星左方,下方,左下方的星星个数.题目已经把星星按照Y坐标从小到大,X从小到大排序.因此,在每次对一个星星进行统计时,之前出现过的星星,只要X

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 2182 Lost Cows (树状数组 &amp;&amp; 二分查找)

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