Poj 2481 Cows (deformation + discretization of stars)

Source: Internet
Author: User

Cows

Time Limit: 3000 MS Memory Limit: 65536 K

Total Submissions: 6854 Accepted: 2211

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

3

1 2

0 3

3 4

0

Sample Output

1 0 0

 

Question: I want to give you the head S and end E of many line segments and ask how many lines are included in each line segment (S and E are the same ). This question is hard to solve because I have no idea how to solve it.

But! Tree Arrays can easily solve this problem !!! First, consider the s and e of their line segments as a point (s, e). In this way, all the points are drawn, and you will find a magical phenomenon, the question requirement becomes: how many points are there in the upper left corner of each point?

!!! Isn't it the same as the simplest stars ???!!!

The stars question is how many points are there in the lower left corner, and this question is about the upper left corner, and the points are not sorted in order. So there are some differences. Just take a special look.

If we do this normally, the y is incremental, so the sum and update directions will be the opposite. In fact, there is nothing to say. In the same way, when sorting, the y goes from big to small, y is the same as x, from small to large, so small processing will become the stars question !!!

Another thing I forgot is that this question also needs to be discretization. discretization is very important and powerful !!!

That's it! Don't miss it !!!

Link: http://poj.org/problem? Id = 2481

Code:

Cpp Code

# Include <iostream>

# Include <stdio. h>

# Include <memory. h>

# Include <algorithm>

Using namespace std;

 

Struct node

{

Int x, y, id;

} A [100005];

 

Int n, B [100005], val [100005];

 

Bool cmp1 (node a, node B) // sorting is very important !!!

{

If (a. y! = B. y) return a. y> B. y; // first from large to small y

Return a. x <B. x; // y is the same. x ranges from small to large.

}

 

Int lowbit (int I)

{

Return I & (-I );

}

 

Void update (int I, int x)

{

While (I <= 100005)

{

B [I] + = x;

I + = lowbit (I );

}

}

 

Int sum (int I)

{

Int sum = 0;

While (I> 0)

{

Sum + = B [I];

I-= lowbit (I );

}

Return sum;

}

 

Int main ()

{

Int I;

While (scanf ("% d", & n), n)

{

Memset (B, 0, sizeof (B ));

Memset (val, 0, sizeof (val ));

For (I = 0; I <n; I ++)

{

Scanf ("% d", & a [I]. x, & a [I]. y );

A [I]. id = I;

A [I]. x ++; a [I]. y ++; // both x and y may be 0, so both ++

}

Sort (a, a + n, cmp1 );

Val [a [0]. id] = sum (a [0]. x); // val [] indicates the sum () of each point ()

Update (a [0]. x, 1 );

For (I = 1; I <n; I ++)

{

If (a [I]. x = a [I-1]. x & a [I]. y = a [I-1]. y) // if the two zones are equal

Val [a [I]. id] = val [a [I-1]. id]; // This value is equal to the previous value

Else val [a [I]. id] = sum (a [I]. x );

Update (a [I]. x, 1); // update the x value of this point.

}

Printf ("% d", val [0]);

For (I = 1; I <n; I ++)

{

Printf ("% d", val [I]);

}

Printf ("\ n ");

}

 

Return 0;

}

Related Article

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.