天天看點

2018.07.08 POJ 2481 Cows(線段樹)

Cows

Time Limit: 3000MS Memory Limit: 65536K

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

Sample Output

1 0 0

Hint

Huge input and output,scanf and printf is recommended.

Source

POJ Contest,Author:[email protected]

事實上,我感覺這道題出出來是考察我們的英文水準的。讓我們用 OI O I 的文法來翻譯一下題目,有 n n <script type="math/tex" id="MathJax-Element-2">n</script>個區間,求出每個區間被包含的次數(重合不算)。顯然先按照左端點排序,然後所有區間的左端點就是單調不減的了。這樣的話答案就隻與右端點有關了。這個直接區間查詢單點插入就行了。

代碼如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#define lc (p<<1)
#define rc (p<<1|1)
#define mid (T[p].l+T[p].r>>1)
#define N 100005
using namespace std;
inline int read(){
    int ans=;
    char ch=getchar();
    while(!isdigit(ch))ch=getchar();
    while(isdigit(ch))ans=(ans<<)+(ans<<)+ch-'0',ch=getchar();
    return ans;
}
int n,ans[N];
struct Node{int s,t,id;}q[N];
struct st{int l,r,sum;}T[N<<];
inline void pushup(int p){T[p].sum=T[lc].sum+T[rc].sum;}
inline void build(int p,int l,int r){
    T[p].l=l,T[p].r=r,T[p].sum=;
    if(l==r)return;
    build(lc,l,mid);
    build(rc,mid+,r);
}
inline void update(int p,int k){
    if(T[p].l==T[p].r){
        ++T[p].sum;
        return;
    }
    if(k<=mid)update(lc,k);
    else update(rc,k);
    pushup(p);
}
inline int query(int p,int ql,int qr){
    if(ql>T[p].r||T[p].l>qr)return ;
    if(ql<=T[p].l&&T[p].r<=qr)return T[p].sum;
    if(qr<=mid)return query(lc,ql,qr);
    if(ql>mid)return query(rc,ql,qr);
    return query(lc,ql,mid)+query(rc,mid+,qr);
}
inline bool cmp(Node a,Node b){return a.s==b.s?a.t>b.t:a.s<b.s;}
int main(){
    while(scanf("%d",&n)&&n){
        for(int i=;i<=n;++i)q[i].s=read()+,q[i].t=read()+,q[i].id=i;
        build(,,);
        sort(q+,q+n+,cmp);
        for(int i=;i<=n;++i){
            if(q[i].s==q[i-1].s&&q[i].t==q[i-1].t){
                ans[q[i].id]=ans[q[i-1].id];
            }
            else ans[q[i].id]=query(,q[i].t,);
            update(,q[i].t);
        }
        for(int i=;i<=n;++i)printf("%d ",ans[i]);
        puts("");
    }
    return ;
}
           

繼續閱讀