#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define VI vector<int>
#define pii pair<int,int>
#define LL __int64
#define DB double
using namespace std;
const int MAXN = 200100;
//const LL INF = 0x3f3f3f3f3f3f3f3fL;
LL INF;
const int MOD = 1e9 + 7;
const double eps = 1e-10;
int n,m,topx,topy;
struct Point{
LL x,y,id;
}p[MAXN],q[MAXN];
map<LL,LL> xid,yid;
LL cntx[MAXN],cnty[MAXN];
LL tree[MAXN],Ans[MAXN];
LL lowbit(int x){
return x & (-x);
}
void update(int x,LL val,int Limit){
while(x <= Limit){
tree[x] = max(tree[x],val);
x += lowbit(x);
}
}
LL query(int x){
LL res = -INF;
while(x > 0){
res = max(tree[x],res);
x -= lowbit(x);
}
return res;
}
bool cmpx(Point a,Point b){
return a.x < b.x;
}
bool cmpy(Point a,Point b){
return a.y < b.y;
}
void solve(){
int k;
sort(p,p + n,cmpx);
sort(q,q + m,cmpx);
memset(tree,-INF,sizeof(tree));
k = 0;
for(int i = 0; i < m;i++){
while(k < n && p[k].x <= q[i].x){
update(yid[p[k].y],p[k].x + p[k].y,topy + 1);
k++;
}
LL res = query(yid[q[i].y]);
Ans[q[i].id] = min(q[i].x + q[i].y - res,Ans[q[i].id]);
}
memset(tree,-INF,sizeof(tree));
k = 0;
for(int i = 0; i < m;i++){
while(k < n && p[k].x <= q[i].x){
update(topy - yid[p[k].y] + 1,p[k].x - p[k].y,topy + 1);
k++;
}
LL res = query(topy - yid[q[i].y] + 1);
Ans[q[i].id] = min(q[i].x - q[i].y - res,Ans[q[i].id]);
}
memset(tree,-INF,sizeof(tree));
k = n - 1;
for(int i = m - 1; i >= 0;i--){
while(k >= 0 && p[k].x >= q[i].x){
update(yid[p[k].y],p[k].y - p[k].x,topy + 1);
k--;
}
LL res = query(yid[q[i].y]);
Ans[q[i].id] = min(q[i].y - q[i].x - res,Ans[q[i].id]);
}
memset(tree,-INF,sizeof(tree));
k = n - 1;
for(int i = m - 1; i >= 0;i--){
while(k >= 0 && p[k].x >= q[i].x){
update(topy - yid[p[k].y] + 1,0 - (p[k].y + p[k].x),topy + 1);
k--;
}
LL res = query(topy - yid[q[i].y] + 1);
Ans[q[i].id] = min(0 - (q[i].y + q[i].x) - res,Ans[q[i].id]);
}
}
int main()
{
INF = 0x3f3f3f3f;
INF = (INF << 32) + INF;
bool flag = false;
while(scanf("%d",&n) != EOF){
if(n == -1) break;
if(flag) printf("\n");
else flag = true;
topx = topy = 0;
for(int i = 0; i < n;i++){
scanf("%I64d%I64d",&p[i].x,&p[i].y);
// cntx[topx++] = p[i].x;
cnty[topy++] = p[i].y;
}
scanf("%d",&m);
for(int i = 0; i < m;i++){
scanf("%I64d%I64d",&q[i].x,&q[i].y);
q[i].id = i;
// cntx[topx++] = q[i].x;
cnty[topy++] = q[i].y;
}
// sort(cntx,cntx + topx);
sort(cnty,cnty + topy);
// topx = unique(cntx,cntx + topx) - cntx;
topy = unique(cnty,cnty + topy) - cnty;
// xid.clear();
yid.clear();
// for(int i = 1; i <= topx; i++)
// xid[cntx[i-1]] = i;
for(int i = 1; i <= topy; i++)
yid[cnty[i-1]] = i;
memset(Ans,INF,sizeof(Ans));
solve();
for(int i = 0; i < m; i++)
printf("%I64d\n",Ans[i]);
}
return 0;
}
HDU 4189
最新推荐文章于 2017-07-01 23:33:30 发布