Poj 2481 cows (tree array) is another unknown story between John and his cows.

Source: Internet
Author: User
Tags ranges

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: cow I and cow J, their favorite clover range is [Si, ei] and [SJ, EJ]. if Si <= SJ and EJ <= EI and EI-Si> ej-SJ, we say that cow I is stronger than cow J.

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 <= 10 5), which is the number of cows. then come n lines, the I-th of which contains two integers: S and E (0 <= S <e <= 10 5) 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 cow I.

Sample Input

31 20 33 40

Sample output

1 0 0

Hint

Huge input and output, scanf and printf is recommended. This is no exception. John's ox is more beautiful than us. Input n, n = 0 in the first line and then input N groups of data. Each ox has two parameters, s [] and e []. If Si <= SJ and EJ <= EI and EI-Si> is met, we can say that I is better than J, finally, the number of cows is better than the number of I cows. Idea: similar to the star question. AC code:
# Include <stdio. h> # include <string. h> # include <iostream> # include <algorithm> using namespace STD; int D1 [1000000], maxn, d [1000000]; // D1 [] is a tree array, d [] Number of cows strong than the current ox struct node {int G, H, Y;} t [1000000]; int CMP (struct node A, struct Node B) // sort by G from large to small {if (. H = B. h) return. G <B. g; return. h> B. h ;}int lowbit (int x) {return X & (-x) ;}int sum (int x) {int res = 0; while (x> 0) {res + = D1 [X]; X-= lowbit (x);} return res;} void add (int x) {While (x <= maxn) {D1 [x] ++; x + = lowbit (x) ;}} int main () {int A, B, I, J, N; while (scanf ("% d", & N) {maxn = 0; memset (D1, 0, sizeof (D1); memset (D, 0, sizeof (d); for (I = 0; I <n; I ++) {scanf ("% d", & T [I]. g, & T [I]. h); t [I]. y = I; t [I]. g ++, t [I]. h ++; // avoid entering the tree array if (T [I]. g> maxn) maxn = T [I]. g;} Sort (t, t + N, CMP); // after sorting out the order, the question is like d [T [0]. y] = sum (T [0]. g); add (T [0]. g); for (I = 1; I <n; I ++) {If (T [I]. G = T [I-1]. G & T [I]. H = T [I-1]. h) // if the current vertex is the same as the previous store, the number of cows that are stronger than him will be the same as the number of cows that go to the front. d [T [I]. y] = d [T [I-1]. y]; else d [T [I]. y] = sum (T [I]. g); add (T [I]. g);} printf ("% d", d [0]); // note the output format for (I = 1; I <n; I ++) printf ("% d", d [I]); printf ("\ n ");}}

Poj 2481 cows (tree array) is another unknown story between John and his cows.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.