Solution -「CERC 2016」「洛谷 P3684」机棚障碍
\(\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」机棚障碍的更多相关文章
- 「区间DP」「洛谷P1043」数字游戏
「洛谷P1043」数字游戏 日后再写 代码 /*#!/bin/sh dir=$GEDIT_CURRENT_DOCUMENT_DIR name=$GEDIT_CURRENT_DOCUMENT_NAME ...
- Solution -「APIO 2016」「洛谷 P3643」划艇
\(\mathcal{Description}\) Link & 双倍经验. 给定 \(n\) 个区间 \([a_i,b_i)\)(注意原题是闭区间,这里只为方便后文描述),求 \(\ ...
- Solution -「JSOI 2019」「洛谷 P5334」节日庆典
\(\mathscr{Description}\) Link. 给定字符串 \(S\),求 \(S\) 的每个前缀的最小表示法起始下标(若有多个,取最小的). \(|S|\le3\time ...
- Solution -「洛谷 P4372」Out of Sorts P
\(\mathcal{Description}\) OurOJ & 洛谷 P4372(几乎一致) 设计一个排序算法,设现在对 \(\{a_n\}\) 中 \([l,r]\) 内的元素排 ...
- Solution -「POI 2010」「洛谷 P3511」MOS-Bridges
\(\mathcal{Description}\) Link.(洛谷上这翻译真的一言难尽呐. 给定一个 \(n\) 个点 \(m\) 条边的无向图,一条边 \((u,v,a,b)\) 表示从 ...
- 「洛谷4197」「BZOJ3545」peak【线段树合并】
题目链接 [洛谷] [BZOJ]没有权限号嘤嘤嘤.题号:3545 题解 窝不会克鲁斯卡尔重构树怎么办??? 可以离线乱搞. 我们将所有的操作全都存下来. 为了解决小于等于\(x\)的操作,那么我们按照 ...
- 「洛谷3338」「ZJOI2014」力【FFT】
题目链接 [BZOJ] [洛谷] 题解 首先我们需要对这个式子进行化简,否则对着这么大一坨东西只能暴力... \[F_i=\sum_{j<i} \frac{q_iq_j}{(i-j)^2}-\s ...
- 「BZOJ2733」「洛谷3224」「HNOI2012」永无乡【线段树合并】
题目链接 [洛谷] 题解 很明显是要用线段树合并的. 对于当前的每一个连通块都建立一个权值线段树. 权值线段树处理操作中的\(k\)大的问题. 如果需要合并,那么就线段树暴力合并,时间复杂度是\(nl ...
- 「洛谷3870」「TJOI2009」开关【线段树】
题目链接 [洛谷] 题解 来做一下水题来掩饰ZJOI2019考炸的心情QwQ. 很明显可以线段树. 维护两个值,\(Lazy\)懒标记表示当前区间是否需要翻转,\(s\)表示区间还有多少灯是亮着的. ...
随机推荐
- 《剑指offer》面试题64. 求1+2+…+n
问题描述 求 1+2+...+n ,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 示例 1: 输入: n = 3 输出: 6 示 ...
- 网络编程-HTTP cookie
目录 1.cookie的起源 2.cookie是什么? 3.创建cookie 3.1.响应首部 Set-Cookie 3.2.请求首部 Cookie 3.3.Document.cookie 4.HTT ...
- 【记录一个问题】macos下使用opencl, clSetEventCallback不生效
一开始的调用顺序是这样: enqueueWriteBuffer enqueueNDRangeKernel enqueueReadBuffer SetEventCallback 执行后主程序用getch ...
- JQ的使用
1.hello,word <script type="text/javascript" src="js/jquery-1.10.1.js">< ...
- 解决Post请求中文乱码问题
解决Post请求中文乱码问题 req.setChracterEncoding()要在获取请求参数前调用才有效,不然还是乱码
- 集合框架-工具类-Collection-toArray方法
1 package cn.itcast.p3.toolclass.arrays.demo; 2 3 import java.util.ArrayList; 4 import java.util.Arr ...
- Flutter 多引擎支持 PlatformView 以及线程合并解决方案
作者:字节移动技术-李皓骅 摘要 本文介绍了 Flutter 多引擎下,使用 PlatformView 场景时不能绕开的一个线程合并问题,以及它最终的解决方案.最终 Pull Request 已经 m ...
- l线程池抓取lianjia
1. 线程池 的应用 from multiprocessing.dummy import Pool import requests from lxml import etree url="h ...
- vue之Better-Scroll组件 将滚动条滚到最底部
首先我们需要使用scrollTo这个方法: scrollTo(x, y, time, easing) 参数: {Number} x 横轴坐标(单位 px) {Number} y 纵轴坐标(单位 px) ...
- java中的线程是如何工作的。
来自对此文章的编辑. https://mp.weixin.qq.com/s?biz=MzA5NDg3MjAwMQ==&mid=2457103451&idx=1&sn=ba302 ...