UVALive-6656-Watching the Kangaroo(二分)

来源:互联网 发布:怎么把mac升级到10.11 编辑:程序博客网 时间:2024/05/17 00:09

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 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 x =10. Because x = 10 does not belong to the range (14, 100).

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

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

So which one is better? Obviously 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


思路:把区间从中点分开分成两个区间,然后分别查询左边和右边的最大值。详见代码。


#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(a.l==b.l) return a.r>b.r;    return a.l<b.l;}bool cmp2(const struct S &a,const struct S &b){    if(a.r==b.r) return a.l>b.l;    return a.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%d",&n,&m);        for(i=0;i<n;i++)        {            scanf("%d%d",&a,&b);            mid=(a+b)>>1;                        ls[i].l=a;//左边            ls[i].r=mid;                        rs[i].l=mid;//右边            rs[i].r=b;            if((a+b)%2) rs[i].l++;//注意        }        sort(ls,ls+n,cmp1);        sort(rs,rs+n,cmp2);        cnt1=0;        temp=-1;        for(i=0;i<n;i++)//把无用的元素去掉        {            if(ls[i].r>temp)            {                idx1[cnt1++]=i;                temp=ls[i].r;            }        }        cnt2=0;        temp=1000000007;        for(i=n-1;i>=0;i--)//把无用的元素去掉        {            if(rs[i].l<temp)            {                idx2[cnt2++]=i;                temp=rs[i].l;            }        }        printf("Case %d:\n",cases++);        while(m--)        {            scanf("%d",&a);            if(a>=rs[idx2[0]].r || a<=ls[idx1[0]].l)//如果没有被包含到任何区间            {                printf("0\n");                continue;            }            ans=0;            s=0,e=cnt1-1;            while(s<=e)//找左边最大值            {                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)//找右边最大值            {                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);        }    }}


3 0
原创粉丝点击