HDU 4189

原创 2013年12月05日 21:01:37
#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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Clover_v2.4k_r4189

  • 2017-09-11 08:51
  • 11.24MB
  • 下载

hdu1002大数相加

  • 2014-12-16 12:44
  • 877B
  • 下载

hdu6085 Rikka with Candies [bitset+想法]

hdu6085 Rikka with Candies [bitset+想法]

HDU 2000-2099 解题报告.CHM

  • 2013-08-08 20:51
  • 831KB
  • 下载

acm课件 HDU 算法大全

  • 2013-08-08 20:57
  • 6.87MB
  • 下载

2017 Multi-University Training Contest - Team 4:1009&hdu6075、Questionnaire

题目: Questionnaire Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Oth...

HDU2501解题报告

  • 2014-07-10 10:44
  • 12KB
  • 下载

hdu5909 Tree Cutting - FWT 加速集合异或(*)

http://acm.hdu.edu.cn/showproblem.PHP?pid=5909 问题描述 Byteasar有一棵nn个点的无根树,节点依次编号为11到nn,其中节点i...

HDU——hash及其应用

  • 2014-07-10 15:41
  • 313KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)