1. 程式人生 > >poj 2481

poj 2481

為我 farmer end ace icu which ron border his

Language: Cows
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 21941 Accepted: 7419

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.

Source

POJ Contest,Author:Mathematica@ZSU

簡要說明題意:

給你一些區間,問你每個區間會被多少個區間完全覆蓋(並且不完全相同)。

首先根據左端點的位置排序,從小到大訪問,在最後把它對應的右端點位置的數+1,只求這個區間r到所有區間的最靠右位置的區間和就是答案,因為我們思考在它前面那些讓靠後位置加1的區間,因為排序的原因l一定是小於等於此處的l,那麽r也是滿足條件就可以計入了

註意相同區間還有 5,6包含 5,5

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+5;
struct A
{
    int l,r,id;
    bool operator<(const A &a)const
    {
        if(a.l!=l) return l<a.l;
        return r>a.r;
    }
}a[N];
int ans[N],c[N],mx;
void add(int x)
{
    for(;x<=mx;x+=x&-x)
        ++c[x];
}
int askk(int x)
{
    int re=0;
    for(;x;x-=x&-x)
        re+=c[x];
    return re;
}
int main()
{
    while(1)
    {
        int n;
        mx=0;
        scanf("%d",&n);
        if(!n) break;
        for(int i=1;i<=n;++i)
            scanf("%d%d",&a[a[i].id=i].l,&a[i].r),mx=max(a[i].r,mx);//取最大右端
        sort(a+1,a+n+1);
        memset(c,0,sizeof(c));
        for(int i=1;i<=n;++i)
        {
            if(a[i].l==a[i-1].l&&a[i].r==a[i-1].r) ans[a[i].id]=ans[a[i-1].id];//與前一個相同則不需要計算
            else ans[a[i].id]=askk(mx)-askk(a[i].r-1);//計算a[i].r到mx的區間和
            add(a[i].r);//對應位置添加
        }
        for(int i=1;i<=n;++i) printf("%d ",ans[i]);
        printf("\n");
    }
    return 0;
}

poj 2481