POJ 2481 Cows simple tree array range coverage
Click Open Link
Cows
Time Limit:3000 MS |
|
Memory Limit:65536 K |
Total Submissions:13334 |
|
Accepted:4413 |
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-dimen1_number line) in his field is special 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 participates ularly 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 favorite 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 31 20 33 40 Sample Output 1 0 0 Hint Huge input and output, scanf and printf is recommended.Source POJ Contest, Author: Mathematica @ ZSU |
There is a range of [s, e] for each nheaded ox, so that the number of cows is stronger than it. Definition of stronger I-head ox than j-head OX: si <= sj & ei> = ej & ei-si> ej-si.
Sort the native cattle in ascending order of e and obtain the answer in a tree Array Based on the s value.
// 2156K1079MS # include
# Include
# Define M 100007 using namespace std; int c [M], ans [M], max_e; struct Cow {int s, e, id;} p [M]; int cmp (Cow a, Cow B) {if (. e = B. e) return. s
B. e;} int lowbit (int x) {return x & (-x);} void add (int pos, int val) {while (pos <= max_e + 1) {c [pos] + = val; pos + = lowbit (pos) ;}} int getsum (int pos) {int res = 0; while (pos> 0) {res + = c [pos]; pos-= lowbit (pos);} return res;} int main () {int n; while (scanf ("% d ", & n), n) {max_e = 0; for (int I = 0; I