POJ 2481Cows

来源:互联网 发布:南京江北新区网络问政 编辑:程序博客网 时间:2024/06/04 22:24

Cows

Time Limit: 3000MSMemory Limit: 65536KTotal Submissions: 15437Accepted: 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

31 20 33 40

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: chilumanxi@xiyoulinux.org> 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/

0 0
原创粉丝点击