1. 程式人生 > >poj 2481 Cows 【樹狀陣列】

poj 2481 Cows 【樹狀陣列】

Cows
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 15386 Accepted: 5128

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.

題意:給定N個子區間,問包含第i個區間的區間有多少個。(只允許區間一個端點重合)

num[i].s記錄左端點,num[i].t記錄右端點

思路:考慮固定一個端點。假設固定s,那麼按s升序排列後,我們就保證了num[i].s <= num[i+1].s,這時發現只有當num[i].t > num[i+1].t時第i區間才包含第i+1區間,那麼我們再按t降序排下就好了。

這樣i+1和i區間的可能情況 (因為區間重合的情況可以直接判定,就不列出了)

一、num[i].s == num[i+1].s, num[i+1].t < num[i].t

二、num[i].s < num[i+1].s, num[i+1].t <= num[i].t

三、num[i].s < num[i+1].s, num[i+1].t > num[i].t

顯然,只有>=num[i+1].t的,才是合法區間。這樣統計>=右端點的區間數再對映右端點就可以了

此題還有一種問法,問第i個區間的(真)子區間有多少個?

解決方案:s降序再t升序,統計<=右端點的區間數,再對映右端點就好了

AC程式碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (100000+10)
#define MAXM (200000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
#define first fi
#define second se
using namespace std;
struct Node{
    int s, t, id;
};
Node num[MAXN];
bool cmp(Node a, Node b){
    if(a.s != b.s)
        return a.s < b.s;
    else
        return a.t > b.t;
}
int N = 100001;
int C1[MAXN], C2[MAXN];
int lowbit(int x){
    return x & (-x);
}
void Update(int *C, int x)
{
    while(x <= N)
    {
        C[x] += 1;
        x += lowbit(x);
    }
}
int sum(int *C, int x)
{
    int s = 0;
    while(x > 0)
    {
        s += C[x];
        x -= lowbit(x);
    }
    return s;
}
int ans[MAXN];
int main()
{
    int n;
    while(Ri(n), n)
    {
        CLR(C1, 0); CLR(C2, 0);
        for(int i = 0; i < n; i++)
        {
            Ri(num[i].s), Ri(num[i].t);
            //num[i].s++; num[i].t++;
            num[i].id = i;
        }
        sort(num, num+n, cmp);
        for(int i = 0; i < n; i++)
        {
            if(i && num[i].s == num[i-1].s && num[i].t == num[i-1].t)
                ans[num[i].id] = ans[num[i-1].id];
            else
                ans[num[i].id] = sum(C1, N) - sum(C1, num[i].t-1);
            Update(C1, num[i].t);
        }
        for(int i = 0; i < n; i++)
        {
            if(i) printf(" ");
            printf("%d", ans[i]);
        }
        printf("\n");
    }
    return 0;
}