题意:n条线段(n <= 100000) (L<=R <= 1e9) ,m组询问(m <= 100000) 每次询问一个点的覆盖范围的最大值。一个点x对于一条包括其的线段,覆盖范围为min(x-L,R-x)

思路:考虑将线段一份为二,对于左边的那部分,以右端点排序,然后 二分找到右端点恰好满足的那个点为id,那么接下来要做的就是就是在[id,n]这个范围内找到L最小的那个点,能够通过求前缀最大来得到。那么左边最大距离为  seg[id].L-preLMax[id],右边最大的求法也是类似。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
const int maxn = 100000+10;
#define REP(_,a,b) for(int _ = (a); _ <= (b); _++)
struct seg{
int L,R;
seg(int L = 0,int R = 0):L(L),R(R){}
};
bool cmp1(seg a,seg b) {
if(a.R != b.R) return a.R < b.R;
else return a.L < b.L;
}
bool cmp2(seg a,seg b) {
if(a.L != b.L) return a.L < b.L;
else return a.R < b.R;
}
vector<seg>lft,rgt; int preLMax[maxn],preRMax[maxn];
int n,m; int main(){ int ncase,T=1;
cin >> ncase;
while(ncase--) {
lft.clear();
rgt.clear();
scanf("%d%d",&n,&m);
for(int i = 0; i < n; i++) {
int L,R;
scanf("%d%d",&L,&R);
int mid = (L+R)>>1;
lft.push_back(seg(L,mid));
rgt.push_back(seg(mid,R));
} sort(lft.begin(),lft.end(),cmp1);
preLMax[lft.size()] = 1e9;
for(int i = lft.size()-1; i >= 0; i--) {
preLMax[i] = min(preLMax[i+1],lft[i].L);
}
sort(rgt.begin(),rgt.end(),cmp2);
preRMax[0] = rgt[0].R;
for(int i = 1; i < rgt.size(); i++) {
preRMax[i] = max(preRMax[i-1],rgt[i].R);
}
printf("Case %d:\n",T++);
while(m--) {
int x,ans=0;
scanf("%d",&x);
int L = 0,R = lft.size()-1;
while(L <= R) {
int mid = (L+R) >>1;
if(lft[mid].R < x) {
L = mid+1;
}else{
R = mid-1;
}
}
ans = max(ans,x-preLMax[L]);
L = 0,R = rgt.size()-1;
while(L <= R) {
int mid = (L+R) >>1;
if(rgt[mid].L < x) {
L = mid+1;
}else{
R = mid-1;
}
}
ans = max(ans,preRMax[R]-x);
ans = max(ans,0);
printf("%d\n",ans);
}
}
return 0;
}

UVa 12715 Watching the Kangaroo(二分)的更多相关文章

  1. UVALive 6656 Watching the Kangaroo --二分

    题意:给你一些区间,再查询一些点,问这些点与所有区间形成的最小距离的最大值.最小距离定义为:如果点在区间内,那么最小距离为0,否则为min(pos-L[i],R[i]-pos). 解法:当然要排个序, ...

  2. 6656 Watching the Kangaroo

    6656 Watching the KangarooDay by day number of Kangaroos is decreasing just liketiger, whale or lion ...

  3. UVa 714 Copying Books(二分)

    题目链接: 传送门 Copying Books Time Limit: 3000MS     Memory Limit: 32768 KB Description Before the inventi ...

  4. UVA 10816 + HDU 1839 Dijstra + 二分 (待研究)

    UVA 题意:两个绿洲之间是沙漠,沙漠的温度不同,告诉起点,终点,求使得从起点到终点的最高温度最小的路径,如果有多条,输出长度最短的路径: 思路:用最小费用(最短路径)最大流(最小温度)也能搞吧,但因 ...

  5. UVA 10668 - Expanding Rods(数学+二分)

    UVA 10668 - Expanding Rods 题目链接 题意:给定一个铁棒,如图中加热会变成一段圆弧,长度为L′=(1+nc)l,问这时和原来位置的高度之差 思路:画一下图能够非常easy推出 ...

  6. uva 10566 Crossed Ladders (二分)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  7. UVa 10539 (筛素数、二分查找) Almost Prime Numbers

    题意: 求正整数L和U之间有多少个整数x满足形如x=pk 这种形式,其中p为素数,k>1 分析: 首先筛出1e6内的素数,枚举每个素数求出1e12内所有满足条件的数,然后排序. 对于L和U,二分 ...

  8. uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)

    Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...

  9. Uva 10339 - Watching Watches【数论,暴力】

    题目链接:10339 - Watching Watches 题意:两个时钟,一个每天慢a秒,一个每天慢b秒,问两钟重新相遇的时刻 1圈有12 * 60 * 60秒,然后1圈 / abs(a - b), ...

随机推荐

  1. The 6 inspectors in XCode

    Name Shortcut Key Description file helper Command + Option + 1 shows you all the file details relate ...

  2. poj 2391 (Floyd+最大流+二分)

    题意:有n块草地,一些奶牛在草地上吃草,草地间有m条路,一些草地上有避雨点,每个避雨点能容纳的奶牛是有限的,给出通过每条路的时间,问最少需要多少时间能让所有奶牛进入一个避雨点. 两个避雨点间可以相互到 ...

  3. 使用ADS1.2的注意事项及常用技巧

    如果创建的项目中有多个文件时(尤其是编译后的镜像大小超过4K时),一定要在link order栏下调整文件顺序,主要是前几个文件的顺序(2440init.s.2440slib.s.nand.c这三个文 ...

  4. HTML的GET方法传递参数样式。

    #HTML的GET方法传递参数样式.http://127.0.0.1:8080/web1/urlinfo/getobject.html?UserId=1&UserName=ad

  5. python之函数用法globals()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法globals() #globals() #说明:在当前作用域下,查看全局变量 ''' ...

  6. Opencv 实现图像的离散傅里叶变换(DFT)、卷积运算(相关滤波)

    我是做Tracking 的,对于速度要求非常高.发现傅里叶变换能够使用. 于是学习之. 核心: 最根本的一点就是将时域内的信号转移到频域里面.这样时域里的卷积能够转换为频域内的乘积! 在分析图像信号的 ...

  7. xmlhttp.readyState==4 && xmlhttp.status==200的探究

    作为一个后端人员,很惭愧,对Ajax的使用只局限在功能实现层面的交互,对底层通过XMLHttpRequest对象来使用的知识却没有仔细研究过.现总结如下 1. XMLHttpRequest 对象的相关 ...

  8. js replaceChild

    //父亲元素.replaceChild(新,旧) 1 <ul id="city"> <li id="bj">北京</li> ...

  9. 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  10. 常用JS技巧[转]

    作者:72妃 事件源对象 event.srcElement.tagName event.srcElement.type 捕获释放 event.srcElement.setCapture(); even ...