参考:http://www.cnblogs.com/slon/archive/2012/03/30/2426104.html

题意:给一个有n个点的点集,有q个询问,每个询问询问一个点p,求与p曼哈顿距离最小的点,并输出曼哈顿距离的最小值。

分析:使得abs(qx - xi) + abs(qy - yi)最小,因为带了个绝对值,所以没法直接套用一些数据结构中查询最值的操作,一时间也没什么头绪。后来看到上面的博客,才明白可以分情况讨论,把绝对值去掉。

一共四种情况:

qx >= xi && qy >= yi

qx >= xi && qy <= yi

qx <= xi && qy >= yi

qx <= xi && qy <= yi

以第一种情况为例分析,其他三种情况分析方法相同。

当 qx > xi && qy > yi 时,直接去绝对值得到:qx - xi + qy - yi = ( qx + qy ) - ( xi +yi ),因为对于每个查询qx + qy相当于一个常数,所以若使qx - xi + qy - yi最小,则 (xi + yi) 最大即可。

于是就转换成了单点更新+区间查最值问题。

线段树离线处理:将点集和查询一块考虑,按上述四种情况分别对点排序,x为第一优先级,y为第二优先级。

将y坐标离散化。

对于每个询问,查询其所属的区间中的最大值。

PS1.其实不用搞四次的,不过这样比较好理解……

PS2.sort函数要仔细考虑一下,跟循环正着跑还是倒着跑有关,之前这里没考虑清楚,样例都跑不对。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> #define LL long long int
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1 using namespace std; const int MAXN = + ;
const LL INF = 1LL << ; struct node
{
LL x, y;
int id;
void readNode()
{
scanf( "%I64d%I64d", &x, &y );
return;
}
}; bool cmp1( node a, node b )
{
if ( a.x != b.x ) return a.x < b.x;
if ( a.y != b.y ) return a.y < b.y;
return a.id < b.id;
} bool cmp2( node a, node b )
{
if ( a.x != b.x ) return b.x < a.x;
if ( a.y != b.y ) return a.y < b.y;
return b.id < a.id;
} bool cmp3( node a, node b )
{
if ( a.x != b.x ) return b.x < a.x;
if ( a.y != b.y ) return a.y < b.y;
return a.id < b.id;
} bool cmp4( node a, node b )
{
if ( a.x != b.x ) return a.x < b.x;
if ( a.y != b.y ) return a.y < b.y;
return b.id < a.id;
} int N, Q;
int all, cntY;
node D[MAXN];
LL maxi[ MAXN << ];
LL ans[MAXN];
LL hashY[MAXN]; void build( int l, int r, int rt )
{
maxi[rt] = -INF;
if ( l == r ) return;
int m = ( l + r ) >> ;
build( lson );
build( rson );
return;
} void PushUp( int rt )
{
maxi[rt] = max( maxi[rt << ], maxi[rt << | ] );
} void update( LL val, int pos, int l, int r, int rt )
{
if ( l == pos && r == pos )
{
maxi[rt] = max( maxi[rt], val );
return;
}
int m = ( l + r ) >> ;
if ( pos <= m ) update( val, pos, lson );
else update( val, pos, rson );
PushUp( rt );
return;
} LL query( int L, int R, int l, int r, int rt )
{
if ( L <= l && r <= R )
{
return maxi[rt];
} LL res = -INF;
int m = ( l + r ) >> ;
if ( L <= m ) res = max( res, query( L, R, lson ) );
if ( R > m ) res = max( res, query( L, R, rson ) );
return res;
} void init()
{
for ( int i = ; i < N; ++i )
{
D[i].readNode();
D[i].id = -;
hashY[i] = D[i].y;
}
scanf( "%d", &Q );
for ( int i = ; i < Q; ++i )
{
D[ N + i ].readNode();
D[ N + i ].id = i;
hashY[ N + i ] = D[N + i].y;
ans[i] = INF;
} all = N + Q;
sort( hashY, hashY + all );
cntY = unique( hashY, hashY + all ) - hashY;
return;
} int main()
{int cas = ;
while ( scanf( "%d", &N ), N != - )
{
init();
build( , cntY, );
sort( D, D + all, cmp1 ); for ( int i = ; i < all; ++i )
{
int id = lower_bound( hashY, hashY + cntY, D[i].y ) - hashY;
++id;
if ( D[i].id == - ) update( D[i].x + D[i].y, id, , cntY, );
else
{
ans[ D[i].id ] = min( ans[ D[i].id ], D[i].x + D[i].y - query( , id, , cntY, ) );
}
} build( , cntY, );
sort( D, D + all, cmp2 );
for ( int i = all - ; i >= ; --i )
{
int id = lower_bound( hashY, hashY + cntY, D[i].y ) - hashY;
++id;
if ( D[i].id == - ) update( D[i].x - D[i].y, id, , cntY, );
else
{
ans[ D[i].id ] = min( ans[ D[i].id ], D[i].x - D[i].y - query( id, cntY, , cntY, ) );
}
} build( , cntY, );
sort( D, D + all, cmp3 );
for ( int i = ; i < all; ++i )
{
int id = lower_bound( hashY, hashY + cntY, D[i].y ) - hashY;
++id;
if ( D[i].id == - ) update( D[i].y - D[i].x, id, , cntY, );
else
{
ans[ D[i].id ] = min( ans[ D[i].id ], D[i].y - D[i].x - query( , id, , cntY, ) );
}
} build( , cntY, );
sort( D, D + all, cmp4 );
for ( int i = all - ; i >= ; --i )
{
int id = lower_bound( hashY, hashY + cntY, D[i].y ) - hashY;
++id;
if ( D[i].id == - ) update( -D[i].x - D[i].y, id, , cntY, );
else
{
ans[ D[i].id ] = min( ans[ D[i].id ], -D[i].x - D[i].y - query( id, cntY, , cntY, ) );
}
} if ( cas ) puts("");
for ( int i = ; i < Q; ++i )
printf( "%I64d\n", ans[i] );
++cas;
}
return ;
}

HDU 4189 Cybercrime Donut Investigation 线段树+思路的更多相关文章

  1. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  2. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  3. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  4. HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)

    HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...

  5. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  6. HDU.1689 Just a Hook (线段树 区间替换 区间总和)

    HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...

  7. hdu 1754 I Hate It 线段树 点改动

    // hdu 1754 I Hate It 线段树 点改动 // // 不多说,裸的点改动 // // 继续练 #include <algorithm> #include <bits ...

  8. hdu 1166 敌兵布阵 线段树 点更新

    // hdu 1166 敌兵布阵 线段树 点更新 // // 这道题裸的线段树的点更新,直接写就能够了 // // 一直以来想要进线段树的坑,结果一直没有跳进去,今天算是跳进去吧, // 尽管十分简单 ...

  9. R - Weak Pair HDU - 5877 离散化+权值线段树+dfs序 区间种类数

    R - Weak Pair HDU - 5877 离散化+权值线段树 这个题目的初步想法,首先用dfs序建一颗树,然后判断对于每一个节点进行遍历,判断他的子节点和他相乘是不是小于等于k, 这么暴力的算 ...

随机推荐

  1. idea中不重启服务器更改代码(使用jrebel)

    http://139.199.89.239:1008/88414687-3b91-4286-89ba-2dc813b107ce 第一步 第二步:下载jrebel 第三步(这里有些有有些没有) 下载完后 ...

  2. 大白话讲解BP算法(转载)

    最近在看深度学习的东西,一开始看的吴恩达的UFLDL教程,有中文版就直接看了,后来发现有些地方总是不是很明确,又去看英文版,然后又找了些资料看,才发现,中文版的译者在翻译的时候会对省略的公式推导过程进 ...

  3. Integer和int使用==比较的总结

    public static void main(String[] args) { int i1 = 128; Integer i2 = 128; Integer i3 = new Integer(12 ...

  4. 并排打印多个图案(C++实现)

    在练习循环控制语句时,经常会遇到一类问题:使用循环控制打印星号(*)来形成各种各样的图案,并强调所有的星号(*)都要用单条的输出语句cout<<"*";来打印. 例如打 ...

  5. JS下载文件常用的方式

    下载附件(image,doc,docx, excel,zip,pdf),应该是实际工作中经常遇到一个问题:这里使用过几种方式分享出来仅供参考; 初次写可能存在问题,有问题望指出 ​ 主要了解的几个知识 ...

  6. Guava Cache 工具类 [ GuavaCacheUtil ]

    pom.xml <dependency> <groupId>com.google.guava</groupId> <artifactId>guava&l ...

  7. html基础之遗忘篇

    a链接: ①a的href指向压缩文件可以下载压缩文件. ②a链接的打开方式可以在head内使用<base target="_blank">来整体控制打开方式. 字符实体 ...

  8. scrapy--matplotlib

    昨天晚上看了一些关于保存文件的相关资料,早早的睡了,白天根据网上查找的资料,自己再捡起来.弄了一上午就爬取出来了,开心!!!好吧,让我们开始 老规矩,先上图.大家也赶快行动起来 分类文件: 文件内co ...

  9. Python文件IO(普通文件读写)

    ## 打开一个文件 - fileobj = open(filename, mode) 其中: fileobj是open()返回的文件对象 filename是该文件的字符串名 mode是指明文件类型和操 ...

  10. 【Hadoop/Hive/mapreduce】系列之如何删除HIVE 表格的分区

    今天的一个业务场景就是要把三年的数据从第一天不停的融合起来,每一天作为表格一个新的分区.由于空间有限,数据量很大,可能每天数据都是几十个G的大小.所以我需要做的一点就是在融合这一天之后,删除一天的分区 ...