\(\mathcal{Description}\)

  Link.

  给一个 \(n\times n\) 的网格图,每个点是空格或障碍。\(q\) 次询问,每次给定两个坐标 \((r_1,c_1),(r_2,c_2)\),问最大的正方形边长 \(k\),满足 \(k\) 是奇数,且中心点在 \((r_1,c_1)\) 的正方形能够移动成为中心点在 \((r_2,c_2)\) 的正方形。

  \(n\le1000\),\(q\le3\times10^5\)。

\(\mathcal{Solution}\)

  这题咋黑了呢 owo?

  令障碍为 \(1\),空格为 \(0\),则一个正方形合法等价于子矩阵和为 \(0\),单次判断用前缀和做到 \(\mathcal O(1)\)。然后整体二分即可。

  复杂度 \(\mathcal O((n^2+q)\log n)\)。

\(\mathcal{Code}\)

#include <queue>
#include <cstdio>
#include <vector> typedef std::pair<int, int> pii; const int MAXN = 1000, MAXQ = 3e5;
const int MOVE[4][2] = { { -1, 0 }, { 0, -1 }, { 1, 0 }, { 0, 1 } };
int n, q, sum[MAXN + 5][MAXN + 5];
int ans[MAXQ + 5], color[MAXN + 5][MAXN + 5];
std::vector<pii> arrived; struct Query { int r1, c1, r2, c2, id; };
std::vector<Query> qrys; inline int rint () {
int x = 0; char s = getchar ();
for ( ; s < '0' || '9' < s; s = getchar () );
for ( ; '0' <= s && s <= '9'; s = getchar () ) x = x * 10 + ( s ^ '0' );
return x;
} inline void wint ( const int x ) {
if ( 9 < x ) wint ( x / 10 );
putchar ( x % 10 ^ '0' );
} inline bool legal ( const int r, const int c, const int rad ) {
int r1 = r - rad, c1 = c - rad, r2 = r + rad - 1, c2 = c + rad - 1;
return 0 <= r1 && 0 <= c1 && r2 <= n && c2 <= n
&& ! ( sum[r2][c2] - sum[r2][c1] - sum[r1][c2] + sum[r1][c1] );
} inline void paint ( const int sr, const int sc, const int rad, const int c ) {
static std::queue<pii> que;
que.push ( { sr, sc } ), color[sr][sc] = c;
arrived.push_back ( { sr, sc } );
while ( ! que.empty () ) {
pii p = que.front (); que.pop ();
for ( int w = 0, tx, ty; w < 4; ++ w ) {
tx = p.first + MOVE[w][0], ty = p.second + MOVE[w][1];
if ( ! color[tx][ty] && legal ( tx, ty, rad ) ) {
que.push ( { tx, ty } ), color[tx][ty] = c;
arrived.push_back ( { tx, ty } );
}
}
}
} inline void solve ( std::vector<Query>& curq, const int al, const int ar ) {
if ( curq.empty () ) return ;
if ( al == ar ) {
for ( auto q: curq ) ans[q.id] = al ? ( al << 1 ) - 1 : 0;
return ;
}
int amid = al + ar + 1 >> 1, col = 0;
for ( int i = amid, ei = n - amid + 1; i <= ei; ++ i ) {
for ( int j = amid, ej = n - amid + 1; j <= ej; ++ j ) {
if ( ! color[i][j] && legal ( i, j, amid ) ) {
paint ( i, j, amid, ++ col );
}
}
}
std::vector<Query> vecL, vecR;
for ( auto q: curq ) {
if ( color[q.r1][q.c1] && color[q.r1][q.c1] == color[q.r2][q.c2] ) vecR.push_back ( q );
else vecL.push_back ( q );
}
for ( pii c: arrived ) color[c.first][c.second] = 0;
arrived.clear ();
solve ( vecL, al, amid - 1 ), solve ( vecR, amid, ar );
} int main () {
n = rint ();
char tmp[MAXN + 5];
for ( int i = 1; i <= n; ++ i ) {
scanf ( "%s", tmp + 1 );
for ( int j = 1; j <= n; ++ j ) {
sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + ( tmp[j] == '#' );
}
}
q = rint ();
for ( int i = 1, r1, c1, r2, c2; i <= q; ++ i ) {
r1 = rint (), c1 = rint (), r2 = rint (), c2 = rint ();
qrys.push_back ( { r1, c1, r2, c2, i } );
}
solve ( qrys, 0, n + 1 >> 1 );
for ( int i = 1; i <= q; ++ i ) wint ( ans[i] ), putchar ( '\n' );
return 0;
}

Solution -「CERC 2016」「洛谷 P3684」机棚障碍的更多相关文章

  1. 「区间DP」「洛谷P1043」数字游戏

    「洛谷P1043」数字游戏 日后再写 代码 /*#!/bin/sh dir=$GEDIT_CURRENT_DOCUMENT_DIR name=$GEDIT_CURRENT_DOCUMENT_NAME ...

  2. Solution -「APIO 2016」「洛谷 P3643」划艇

    \(\mathcal{Description}\)   Link & 双倍经验.   给定 \(n\) 个区间 \([a_i,b_i)\)(注意原题是闭区间,这里只为方便后文描述),求 \(\ ...

  3. Solution -「JSOI 2019」「洛谷 P5334」节日庆典

    \(\mathscr{Description}\)   Link.   给定字符串 \(S\),求 \(S\) 的每个前缀的最小表示法起始下标(若有多个,取最小的).   \(|S|\le3\time ...

  4. Solution -「洛谷 P4372」Out of Sorts P

    \(\mathcal{Description}\)   OurOJ & 洛谷 P4372(几乎一致)   设计一个排序算法,设现在对 \(\{a_n\}\) 中 \([l,r]\) 内的元素排 ...

  5. Solution -「POI 2010」「洛谷 P3511」MOS-Bridges

    \(\mathcal{Description}\)   Link.(洛谷上这翻译真的一言难尽呐.   给定一个 \(n\) 个点 \(m\) 条边的无向图,一条边 \((u,v,a,b)\) 表示从 ...

  6. 「洛谷4197」「BZOJ3545」peak【线段树合并】

    题目链接 [洛谷] [BZOJ]没有权限号嘤嘤嘤.题号:3545 题解 窝不会克鲁斯卡尔重构树怎么办??? 可以离线乱搞. 我们将所有的操作全都存下来. 为了解决小于等于\(x\)的操作,那么我们按照 ...

  7. 「洛谷3338」「ZJOI2014」力【FFT】

    题目链接 [BZOJ] [洛谷] 题解 首先我们需要对这个式子进行化简,否则对着这么大一坨东西只能暴力... \[F_i=\sum_{j<i} \frac{q_iq_j}{(i-j)^2}-\s ...

  8. 「BZOJ2733」「洛谷3224」「HNOI2012」永无乡【线段树合并】

    题目链接 [洛谷] 题解 很明显是要用线段树合并的. 对于当前的每一个连通块都建立一个权值线段树. 权值线段树处理操作中的\(k\)大的问题. 如果需要合并,那么就线段树暴力合并,时间复杂度是\(nl ...

  9. 「洛谷3870」「TJOI2009」开关【线段树】

    题目链接 [洛谷] 题解 来做一下水题来掩饰ZJOI2019考炸的心情QwQ. 很明显可以线段树. 维护两个值,\(Lazy\)懒标记表示当前区间是否需要翻转,\(s\)表示区间还有多少灯是亮着的. ...

随机推荐

  1. List<FieldModelBase> 转 DataTable

    // List<FieldModelBase> 转 DataTable private DataTable ListToDataTable(List<FieldModelBase&g ...

  2. spring security +MySQL + BCryptPasswordEncoder 单向加密验证 + 权限拦截 --- 心得

    1.前言 前面学习了 security的登录与登出 , 但是用户信息 是 application 配置 或内存直接注入进去的 ,不具有实用性,实际上的使用还需要权限管理,有些 访问接口需要某些权限才可 ...

  3. android入门--环境搭建

    运行环境 windows 7 下载地址 环境下载 最近开接触Android(安卓)嵌入式开发,首要问题是搭建Andoid开发环境,由于本人用的是windows7的笔记本,也就只能到Windows中搭建 ...

  4. 周末撸了个Excel框架,现已开源,yyds!!

    大家好,我是冰河~~ 不管是传统软件企业还是互联网企业,不管是管理软件还是面向C端的互联网应用.都不可避免的会涉及到报表操作,而对于报表业务来说,一个很重要的功能就是将数据导出到Excel. 如果我们 ...

  5. Mybatis配置解析(核心配置文件)

    4.配置解析 4.1.核心配置文件 Mybatis的配置文件包含了会深深影响mybatis行为的设置和属性信息 mybatis-config.xml properties(属性)重点 settings ...

  6. windows server 服务器安装jenkins 并通过git拉取代码实现自动发布到IIS

    Jenkins是一个开源软件,可以通过一定的配置进行自动构建,测试,部署等功能. 首先,服务器应安装好 .NET Core环境和JDK, 下载Jenkins安装包 https://www.jenkin ...

  7. 【记录一个问题】铁威马nas,噪音太大了,我老婆说在客厅放了一个电饭锅

    1.硬盘转动的噪音特别大,而且还是有很大IO的长时间猛转: 2.IO的管理算法,以及做IO的进程,都有很大问题.并未做任何操作,动不动就疯了一样硬盘猛转.

  8. ASP.NET 内联代码、内联表达式、数据绑定表达式使用方法罗列(形式就是常说的尖括号 百分号 等于号 井号)

    今天在做渭南电脑维修网的一个小功能时遇到了一些问题,因此特别列出,以备他日之用. 首先对ASP.NET 内联代码.内联表达式.数据绑定表达式的概念进行罗列,详细概念以及基本的用法我就不在这里罗嗦了,请 ...

  9. SSM项目使用拦截器实现登录验证功能

    SSM项目使用拦截器实现登录验证功能 登录接口实现 public User queryUser(String UserName, String Password,HttpServletRequest ...

  10. Linux下Makefile的编写及四个特殊符号的意义@、$@、$^、$

    转自:https://blog.csdn.net/runfarther/article/details/50036115# 我们先看三段C++程序: 一.line1的源码 line1.h #ifnde ...