UVALive-6656-Watching the Kangaroo (Binary), uvalive

Source: Internet
Author: User

UVALive-6656-Watching the Kangaroo (Binary), uvalive

Day by day number of Kangaroos is decreasing justlike tiger, whale or lions. so I decided to make them a sanctuary where theywill live peacefully. I do not let visitors go near them. so I planted somedisplay screen outside the sanctuary. for this problem, you may assume thesanctuary to be a long line of 1000000000 unit distance. the leftmost positionis marked with 0 and the rightmost position with 1000000000. there are at most100000 cameras on this line. each of the cameras is described with (L, R) which means that camera covers a range from position L to position R inclusive. each of the cameras has their associated display screens outside where the visitorscan see the Kangaroos.

 

Now for the convenience of the spectators weannounce time to time: "Kangaroo appears in Screen 3", "Kangaroo appearsin Screen 7" and so on. I have some employees who continuously monitor theposition of Kangaroos and inform the system (here position is a marker ). thesystem chooses the best screen to display that animal based on the coverage. the coverage of a screen for a position x is defined as follows:

 

If the position x is outside the range of thatscreen (I. e. x <L or x> R) then the coverage is zero. otherwise thecoverage is min (x-L, R-x ).

 

An example will make it clear:

Suppose there are four screens covering the range (7, 15), (14,100), (8, 10) and (1, 11). Now say one Kangaroo appears at x = 10.

First screen has coverage of 3 unit around und x = 10. because x = 10 is within (7, 15) and min (10-7; 15-10) = min (3, 5) = 3.

Second screen has coverage of 0 unit around und x = 10. Because x = 10 does not belong to the range (14,100 ).

Third screen has coverage of 0 unit around und x = 10. Because though x = 10 is within (8, 10) but min (10-8-10) = 0.

Fourth screen has coverage of 1 unit around und x = 10. Because x = 10 is within (1, 11) and min (10-1, 11-10) = 1.

So which one is better? Obviusly the first one, as it has the maximum coverage.

 

So you are given the ranges of the screens andthe positions the kangaroo appears. For each position of the Kangaroo you areto tell me the maximum coverage you can have with any of the screens.


Input

First line of the test file contains T (T <= 3), number of test cases. hence T cases follow. for each case you are given N, M in the first line; N is the number of screens and M is the number of Kangarooappearance (1 <= N, M <= 100000 ). in the next N lines you are given the rangeof screens l r (0 <= L <= R <= 1000000000 ). next M lines contain theposition of the Kangaroo | an integer x (0 <= x <= 1000000000 ).

Output
For each case print the case number. Hence print M lines, I-th containing the maximum coverage you
Can have around I-th Kangaroo.
Warning: The judge input le is around 6 MB. So use faster I/O functions.


Sample Input
1
3 2
7 15
14 100
1 11
10
120
Sample Output
Case 1:
3
0


Idea: divide the interval into two separate intervals, and then query the maximum values on the left and right respectively. For details, see the code.


# Include <cstdio> # include <algorithm> using namespace std; struct S {int l, r;} ls [100000], rs [100000]; int idx1 [100000], idx2 [100000]; bool cmp1 (const struct S & a, const struct S & B) {if (. l = B. l) return. r> B. r; return. l <B. l;} bool cmp2 (const struct S & a, const struct S & B) {if (. r = B. r) return. l> B. l; return. r <B. r;} int main () {int T, n, m, I, a, B, s, e, mid, cnt1, cnt2, temp, ans, cases = 1; scanf ("% d", & T); while (T --) {scanf ("% d", & n, & m); for (I = 0; I <n; I ++) {scanf ("% d", & a, & B); mid = (a + B)> 1; ls [I]. l = a; // LEFT ls [I]. r = mid; rs [I]. l = mid; // rs [I] on the right. r = B; if (a + B) % 2) rs [I]. l ++; // note} sort (ls, ls + n, cmp1); sort (rs, rs + n, cmp2); cnt1 = 0; temp =-1; for (I = 0; I <n; I ++) // Remove useless elements {if (ls [I]. r> temp) {idx1 [cnt1 ++] = I; temp = ls [I]. r ;}} cnt2 = 0; temp = 1000000007; for (I = n-1; I> = 0; I --) // Remove useless elements {if (rs [I]. l <temp) {idx2 [cnt2 ++] = I; temp = rs [I]. l ;}} printf ("Case % d: \ n", cases ++); while (m --) {scanf ("% d", & ); if (a> = rs [idx2 [0]. r | a <= ls [idx1 [0]. l) // if not included in any range {printf ("0 \ n"); continue;} ans = 0; s = 0, e = cnt1-1; while (s <= e) // find the maximum value on the left {mid = (s + e)> 1; if (ls [idx1 [mid]. r> = a) {ans = max (ans, a-ls [idx1 [mid]. l); e = mid-1;} else s = mid + 1;} s = 0, e = cnt2-1; while (s <= e) // find the maximum value on the right {mid = (s + e)> 1; if (rs [idx2 [mid]. l <= a) {ans = max (ans, rs [idx2 [mid]. r-a); e = mid-1;} else s = mid + 1;} printf ("% d \ n", ans );}}}



Describe something special you want to buy in the future

Electrical Devices: iphone, computers, and so on, can be associated with topics such as gifts and things.

Life type: bed, cupboard, you think your home is bad, like changing

As long as you speak English well

Don't ask me again

The piano guys let it go, which is the violin music troupe that started at 2 minutes 7 seconds.

Winter of Vivaldi? Let it go is the winter and let it go mash-up of Vivaldi.

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.