Sicily 1088. Cows

1088. Cows

Constraints

Time Limit: 3 secs, Memory Limit: 32 MB

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 n lines containing n  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

// Problem#: 1088
// Submission#: 3410083
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <functional>
#include <map>
#include <string.h>
#include <math.h>
#include <list>
using namespace std;

const int MAX = 100005;

struct range {
    int s, e, id;
};

int N;
range ori[MAX];
int C[MAX];
int ans[MAX];

inline bool cmp(const range & ra1, const range & ra2) {
    if (ra1.e != ra2.e) return ra1.e < ra2.e;
    else return ra1.s > ra2.s;
}

inline int Lowbit(int m) {return m & (-m);}

inline int Getsum(int k) {
    int sum = 0;
    while (k > 0) {
        sum += C[k];
        k -= Lowbit(k);
    }
    return sum;
}

inline void Change(int k, int m, int n) {
    while (k <= n) {
        C[k] += m;
        k += Lowbit(k);
    }
}

int main() {

    //std::ios::sync_with_stdio(false);

    while (1) {

        scanf("%d", &N);
        if (!N) break;
        int maxS = -1;
        for (int i = 0; i < N; i++) {
            scanf("%d%d", &ori[i].s, &ori[i].e);
            ori[i].id = i;
            ori[i].s++;
            ori[i].e++;
            if (ori[i].s > maxS) maxS = ori[i].s;
        }
        sort(ori, ori + N, cmp);
        memset(C, 0, sizeof(int) * (maxS + 1));
        for (int i = N - 1; i >= 0; i--) {
            if (i != N - 1 && ori[i].e == ori[i + 1].e && ori[i].s == ori[i + 1].s)
                ans[ori[i].id] = ans[ori[i + 1].id];
            else
                ans[ori[i].id] = Getsum(ori[i].s);
            Change(ori[i].s, 1, maxS);
        }
        for (int i = 0; i < N; i++) printf("%d\n", ans[i]);
    }

    return 0;
}                                 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值