Cows
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 15437 | Accepted: 5146 |
Description
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
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
Sample Input
3 1 2 0 3 3 4 0
Sample Output
1 0 0
Hint
/************************************************************************* > 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; }