poj 2481

Cows

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 13128   Accepted: 4360

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 cowiis 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.

Source

POJ Contest,Author:[email protected]

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<vector>
using namespace std;
#define N 1000010
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int n,num[N*4],sum[N*4];
struct line
{
    int x,y,nt;
}e[N];
bool cmp(line a,line b)
{
    if(a.x==b.x)
            return a.y>b.y;
    return a.x<b.x;
}
int update(int l,int r,int rt,int L,int R)
{
    if(L<=l&&r<=R)
        return sum[rt];
    int m=(l+r)>>1;
    int ret=0;
    if(L<=m)
        ret+=update(lson,L,R);
    if(R>m)
        ret+=update(rson,L,R);
    return ret;
}
void push(int l,int r,int rt,int x)
{
    sum[rt]++;
    if(l==r)
        return ;
    int m=(l+r)>>1;
    if(m>=x) push(lson,x);
    else push(rson,x);
}
int main()
{
    while(scanf("%d",&n),n)
    {
        memset(sum,0,sizeof(sum));
        memset(num,0,sizeof(num));
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&e[i].x,&e[i].y);
            e[i].nt=i;
        }
        sort(e+1,e+1+n,cmp);
        for(int i=1;i<=n;i++)
        {
            if(i!=1&&e[i].x==e[i-1].x&&e[i].y==e[i-1].y)
                num[e[i].nt]=num[e[i-1].nt];
            else num[e[i].nt]=update(1,N,1,e[i].y,N);
            push(1,N,1,e[i].y);
        }

        for(int i=1;i<=n-1;i++)
            printf("%d ",num[i]);
        printf("%d\n",num[n]);
    }
    return 0;
}

  

时间: 2024-10-10 08:08:42

poj 2481的相关文章

POJ 2481 Cows (线段树)

Cows 题目:http://poj.org/problem?id=2481 题意:有N头牛,每只牛有一个值[S,E],如果对于牛i和牛j来说,它们的值满足下面的条件则证明牛i比牛j强壮:Si <=Sjand Ej <= Ei and Ei - Si > Ej - Sj.现在已知每一头牛的测验值,要求输出每头牛有几头牛比其强壮. 思路:将牛按照S从小到大排序,S相同按照E从大到小排序,这就保证了排在后面的牛一定不比前面的牛强壮.再按照E值(离散化后)建立一颗线段树(这里最值只有1e5,所

树状数组 POJ 2481 Cows

题目传送门 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 const int MAX_N = 100000 + 10; 7 int cnt[MAX_N]; 8 int ans[MAX_N]; 9 int maxn = -1; 10 struct node 11 { 12 int s, e; 13 int id; 14 }cow[MAX_

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 &amp;&amp; POJ 2352 Stars(树状数组妙用)

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

poj 3067 poj 2481 树状数组变形+对区间排序

这种问题先对区间和线段进行排序,排序方法见代码cmp 然后分析其中一个点,用sum求值 poj 3067 Description Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coast and M cities on the West coast (M <

POJ 2481 Cows(树状数组)

http://poj.org/problem?id=2481 题意: 有n头牛,每头牛有一个区间[S,E],求每头牛比它区间大的牛的个数. 思路: 先对数据进行一下排序,先按右坐标按降序排列,那么接下来我们只需要比较左坐标的数值大小就可以了. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<vector> 6

每次输出有几条线段能完全覆盖大于自己和hdu5372相反 树状数组或线段树 poj 2481 Cows

http://poj.org/problem?id=2481 Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 14762   Accepted: 4886 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

poj 2481 Cows

Problem: 2481 User: shu_dayang Memory: 2112K Time: 1094MS Language: C++ Result: Accepted #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #define MAXN 100005 using namespace std; struct Cow { int x; int y;

POJ 2481 Cows 简单树状数组区间覆盖

点击打开链接 Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13334   Accepted: 4413 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

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