POJ 2481Cows

Cows

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 15437 Accepted: 5146

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 locationrespectively(分別地) 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.
題意是給定一些區間,求每個區間的真子區間個數。
顯然如果對S進行排序的話,E是遞減的,需要反向進行樹狀數組操作,所以索性直接按照E遞減排序,S遞增,爲滿足樹狀數組要求對S進行+1處理。多維護一個數組作爲編號和輸出答案的方便。
最後注意一下相等的邊界問題即可,代碼如下:
/*************************************************************************
	> File Name: Cows.cpp
	> Author: Zhanghaoran
	> Mail: [email protected]
	> Created Time: 2016年02月03日 星期三 23時03分51秒
 ************************************************************************/

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>

using namespace std;

int N;
struct node{
    int S, E;
    int number;
}cow[100010];

int tree[100010];
int ans[100010];

bool cmp(node a, node b){
    if(a.E == b.E){
        return a.S < b.S;
    }
    else
        return a.E > b.E;
}

void add(int x){
    while(x < 100010){
        tree[x] ++;
        x += x & -x;
    }
}

int check(int x){
    int sum = 0;
    while(x){
        sum += tree[x];
        x -= x & -x;
    }
    return sum;
}

int main(void){
    while(cin >> N, N){
        memset(tree, 0, sizeof(tree));
        memset(ans, 0, sizeof(ans));
        for(int i = 1; i <= N; i ++){
            scanf("%d%d", &cow[i].S, &cow[i].E);
            cow[i].number = i;
        }
        sort(cow + 1, cow + N + 1, cmp);
        ans[cow[1].number] = 0;
        add(cow[1].S + 1);
        for(int i = 2; i <= N; i ++){
            if(cow[i].S == cow[i - 1].S){
                if(cow[i].E == cow[i - 1].E){
                    ans[cow[i].number] = ans[cow[i - 1].number];
                }
                else
                    ans[cow[i].number] = check(cow[i].S + 1);
            }
            else
                ans[cow[i].number] = check(cow[i].S + 1);
            add(cow[i].S + 1);
        }
	cout << ans[1];
        for(int i = 2; i <= N; i ++)
            cout << " " << ans[i];
        cout << endl;
    }
    return 0;
}

 

查看原文:http://chilumanxi.org/2016/02/03/poj-2481cows/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章